commandinterpreter.java

来自「jpda例子文件」· Java 代码 · 共 1,488 行 · 第 1/4 页

JAVA
1,488
字号
        if (current == null) {            env.failure("No default thread specified: " +			"use the \"thread\" command first.");            return;        }	StackFrame frame;	try {	    frame = context.getCurrentFrame(current);	    if (frame == null) {		env.failure("Thread has not yet created any stack frames.");		return;	    }	} catch (VMNotInterruptedException e) {	    env.failure("Target VM must be in interrupted state.");	    return;	}        List vars;        try {            vars = frame.visibleVariables();            if (vars == null || vars.size() == 0) {                env.failure("No local variables");                return;            }        } catch (AbsentInformationException e) {            env.failure("Local variable information not available." +                        " Compile with -g to generate variable information");            return;        }	OutputSink out = env.getOutputSink();        out.println("Method arguments:");        for (Iterator it = vars.iterator(); it.hasNext(); ) {            LocalVariable var = (LocalVariable)it.next();            if (var.isArgument()) {                printVar(out, var, frame);            }        }        out.println("Local variables:");        for (Iterator it = vars.iterator(); it.hasNext(); ) {            LocalVariable var = (LocalVariable)it.next();            if (!var.isArgument()) {                printVar(out, var, frame);            }        }	out.show();        return;    }    /**      * Command: monitor     * Monitor an expression     */    private void commandMonitor(StringTokenizer t) throws NoSessionException {        if (!t.hasMoreTokens()) {	    env.error("Argument required");        } else {	    env.getMonitorListModel().add(t.nextToken(""));        }    }    /**      * Command: unmonitor     * Unmonitor an expression     */    private void commandUnmonitor(StringTokenizer t) throws NoSessionException {        if (!t.hasMoreTokens()) {	    env.error("Argument required");        } else {	    env.getMonitorListModel().remove(t.nextToken(""));        }    }    // Print a stack variable.    private void printVar(OutputSink out, LocalVariable var, StackFrame frame) {        out.print("  " + var.name());        if (var.isVisible(frame)) {            Value val = frame.getValue(var);            out.println(" = " + val.toString());        } else {            out.println(" is not in scope");        }    }    // Command: print    // Evaluate an expression.    private void commandPrint(StringTokenizer t, boolean dumpObject) throws NoSessionException {        if (!t.hasMoreTokens()) {	    //### Probably confused if expresion contains whitespace.	    env.error("No expression specified.");            return;        }	ThreadReference current = context.getCurrentThread();        if (current == null) {            env.failure("No default thread specified: " +			"use the \"thread\" command first.");            return;        }	StackFrame frame;	try {	    frame = context.getCurrentFrame(current);	    if (frame == null) {		env.failure("Thread has not yet created any stack frames.");		return;	    }	} catch (VMNotInterruptedException e) {	    env.failure("Target VM must be in interrupted state.");	    return;	}        while (t.hasMoreTokens()) {            String expr = t.nextToken("");	    Value val = null;	    try {		val = runtime.evaluate(frame, expr);	    } catch(Exception e) {                env.error("Exception: " + e);		//### Fix this!	    }            if (val == null) {                return;  // Error message already printed            }	    OutputSink out = env.getOutputSink();            if (dumpObject && (val instanceof ObjectReference) &&                                 !(val instanceof StringReference)) {                ObjectReference obj = (ObjectReference)val;                ReferenceType refType = obj.referenceType();                out.println(expr + " = " + val.toString() + " {");                dump(out, obj, refType, refType);                out.println("}");            } else {                out.println(expr + " = " + val.toString());            }	    out.show();        }    }    private void dump(OutputSink out, 		      ObjectReference obj, ReferenceType refType,                      ReferenceType refTypeBase) {        for (Iterator it = refType.fields().iterator(); it.hasNext(); ) {            Field field = (Field)it.next();            out.print("    ");            if (!refType.equals(refTypeBase)) {                out.print(refType.name() + ".");            }            out.print(field.name() + ": ");            Object o = obj.getValue(field);            out.println((o == null) ? "null" : o.toString()); // Bug ID 4374471        }        if (refType instanceof ClassType) {            ClassType sup = ((ClassType)refType).superclass();            if (sup != null) {                dump(out, obj, sup, refTypeBase);            }        } else if (refType instanceof InterfaceType) {            List sups = ((InterfaceType)refType).superinterfaces();            for (Iterator it = sups.iterator(); it.hasNext(); ) {                dump(out, obj, (ReferenceType)it.next(), refTypeBase);            }        }    }    /*     * Display help message.     */        private void help() {	out.println("** command list **");	out.println("threads [threadgroup]     -- list threads");	out.println("thread <thread id>        -- set default thread");	out.println("suspend [thread id(s)]    -- suspend threads (default: all)");        out.println("resume [thread id(s)]     -- resume threads (default: all)");        out.println("where [thread id] | all   -- dump a thread's stack");        out.println("wherei [thread id] | all  -- dump a thread's stack, with pc info");        out.println("threadgroups              -- list threadgroups");        out.println("threadgroup <name>        -- set current threadgroup\n");//	out.println("print <expression>        -- print value of expression");	out.println("dump <expression>         -- print all object information\n");//	out.println("eval <expression>         -- evaluate expression (same as print)");        out.println("locals                    -- print all local variables in current stack frame\n");        out.println("classes                   -- list currently known classes");	out.println("methods <class id>        -- list a class's methods\n");        out.println("stop [in] <class id>.<method>[(argument_type,...)] -- set a breakpoint in a method");        out.println("stop [at] <class id>:<line> -- set a breakpoint at a line");        out.println("up [n frames]             -- move up a thread's stack");        out.println("down [n frames]           -- move down a thread's stack");        out.println("frame <frame-id>           -- to a frame");        out.println("clear <class id>.<method>[(argument_type,...)]   -- clear a breakpoint in a method");        out.println("clear <class id>:<line>   -- clear a breakpoint at a line");        out.println("clear                     -- list breakpoints");        out.println("step                      -- execute current line");        out.println("step up                   -- execute until the current method returns to its caller");        out.println("stepi                     -- execute current instruction");        out.println("next                      -- step one line (step OVER calls)");        out.println("nexti                     -- step one instruction (step OVER calls)");        out.println("cont                      -- continue execution from breakpoint\n");//	out.println("catch <class id>          -- break for the specified exception");//	out.println("ignore <class id>         -- ignore when the specified exception\n");        out.println("view classname|filename   -- display source file");        out.println("list [line number|method] -- print source code context at line or method");        out.println("use <source file path>    -- display or change the source path\n");//### new	out.println("sourcepath <source file path>    -- display or change the source path\n");//### new	out.println("classpath <class file path>    -- display or change the class path\n");	out.println("monitor <expression>      -- evaluate an expression each time the program stops\n");	out.println("unmonitor <monitor#>      -- delete a monitor\n");        out.println("read <filename>           -- read and execute a command file\n");//	out.println("memory                    -- report memory usage");//	out.println("gc                        -- free unused objects\n");        out.println("run <class> [args]        -- start execution of a Java class");        out.println("run                       -- re-execute last class run");        out.println("load <class> [args]       -- start execution of a Java class, initially suspended");        out.println("load                      -- re-execute last class run, initially suspended");        out.println("attach <portname>         -- debug existing process\n");        out.println("detach                    -- detach from debuggee process\n");        out.println("kill <thread(group)>      -- kill a thread or threadgroup\n");        out.println("!!                        -- repeat last command");        out.println("help (or ?)               -- list commands");        out.println("exit (or quit)            -- exit debugger");    }        /*     * Execute a command.     */        public void executeCommand(String command) {	//### Treatment of 'out' here is dirty...	out = env.getOutputSink();	if (echo) {	    out.println(">>> " + command);	}	StringTokenizer t = new StringTokenizer(command);	try {	    String cmd;            if (t.hasMoreTokens()) {                cmd = t.nextToken().toLowerCase();                lastCommand = cmd;            } else {                cmd = lastCommand;            }                	    if (cmd.equals("print")) {		commandPrint(t, false);	    } else if (cmd.equals("eval")) {		commandPrint(t, false);	    } else if (cmd.equals("dump")) {		commandPrint(t, true);	    } else if (cmd.equals("locals")) {		commandLocals();	    } else if (cmd.equals("classes")) {		commandClasses();	    } else if (cmd.equals("methods")) {		commandMethods(t);	    } else if (cmd.equals("threads")) {		commandThreads(t);	    } else if (cmd.equals("thread")) {		commandThread(t);	    } else if (cmd.equals("suspend")) {		commandSuspend(t);	    } else if (cmd.equals("resume")) {		commandResume(t);	    } else if (cmd.equals("cont")) {		commandCont();	    } else if (cmd.equals("threadgroups")) {		commandThreadGroups();	    } else if (cmd.equals("threadgroup")) {		commandThreadGroup(t);	    } else if (cmd.equals("run")) {		commandRun(t);	    } else if (cmd.equals("load")) {		commandLoad(t);	    } else if (cmd.equals("connect")) {		commandConnect(t);	    } else if (cmd.equals("attach")) {		commandAttach(t);	    } else if (cmd.equals("detach")) {		commandDetach(t);	    } else if (cmd.equals("interrupt")) {		commandInterrupt(t);//### Not implemented.//	    } else if (cmd.equals("catch")) {//		commandCatchException(t);//### Not implemented.//	    } else if (cmd.equals("ignore")) {//		commandIgnoreException(t);	    } else if (cmd.equals("step")) {		commandStep(t);	    } else if (cmd.equals("stepi")) {		commandStepi();	    } else if (cmd.equals("next")) {		commandNext();	    } else if (cmd.equals("nexti")) {		commandNexti();            } else if (cmd.equals("kill")) {                commandKill(t);	    } else if (cmd.equals("where")) {		commandWhere(t, false);	    } else if (cmd.equals("wherei")) {		commandWhere(t, true);	    } else if (cmd.equals("up")) {		commandUp(t);	    } else if (cmd.equals("down")) {		commandDown(t);	    } else if (cmd.equals("frame")) {		commandFrame(t);	    } else if (cmd.equals("stop")) {		commandStop(t);	    } else if (cmd.equals("clear")) {		commandClear(t);	    } else if (cmd.equals("list")) {		commandList(t);	    } else if (cmd.equals("use")) {		commandUse(t);	    } else if (cmd.equals("sourcepath")) {		commandSourcepath(t);	    } else if (cmd.equals("classpath")) {		commandClasspath(t);	    } else if (cmd.equals("monitor")) {		commandMonitor(t);	    } else if (cmd.equals("unmonitor")) {		commandUnmonitor(t);	    } else if (cmd.equals("view")) {		commandView(t);//	    } else if (cmd.equals("read")) {//		readCommand(t);	    } else if (cmd.equals("help") || cmd.equals("?")) {		help();	    } else if (cmd.equals("quit") || cmd.equals("exit")) {		try {		    runtime.detach();		} catch (NoSessionException e) {		    // ignore		}		env.terminate();	    } else {		//### Dubious repeat-count feature inherited from 'jdb'                if (t.hasMoreTokens()) {                    try {                                    int repeat = Integer.parseInt(cmd);                        String subcom = t.nextToken("");                        while (repeat-- > 0) {                            executeCommand(subcom);                        }                        return;                    } catch (NumberFormatException exc) {                    }                }		out.println("huh? Try help...");		out.flush();	    }	} catch (NoSessionException e) {	    out.println("There is no currently attached VM session.");	    out.flush();	} catch (Exception e) {	    out.println("Internal exception: " + e.toString());	    out.flush();	    System.out.println("JDB internal exception: " + e.toString());	    e.printStackTrace();	}	out.show();    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?