commandinterpreter.java

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

JAVA
1,488
字号
		try {		    String vmArgs = context.getVmArguments();		    runtime.run(suspended,				vmArgs,				clname,				context.getProgramArguments());		    return true;		} catch (VMLaunchFailureException e) {		    env.failure("Attempt to launch main class \"" + clname + "\" failed.");		}	    } else {		env.failure("No main class specifed and no current default defined.");	    }        } else {	    clname = t.nextToken();	    StringBuffer sbuf = new StringBuffer();	    // Allow VM arguments to be specified here?	    while (t.hasMoreTokens()) {		String tok = t.nextToken();		sbuf.append(tok);		if (t.hasMoreTokens()) {		    sbuf.append(' ');		}	    }	    String args = sbuf.toString();	    try {		String vmArgs = context.getVmArguments();		runtime.run(suspended, vmArgs, clname, args);		context.setMainClassName(clname);		//context.setVmArguments(vmArgs);		context.setProgramArguments(args);		return true;	    } catch (VMLaunchFailureException e) {                env.failure("Attempt to launch main class \"" + clname + "\" failed.");            }	}	return false;    }    // Command: connect    private void commandConnect(StringTokenizer t) {        try {            LaunchTool.queryAndLaunchVM(runtime);        } catch (VMLaunchFailureException e) {            env.failure("Attempt to connect failed.");        }    }    // Command: attach    private void commandAttach(StringTokenizer t) {	String portName;	if (!t.hasMoreTokens()) {	    portName = context.getRemotePort();	    if (!portName.equals("")) {		try {		    runtime.attach(portName);		} catch (VMLaunchFailureException e) {		    env.failure("Attempt to attach to port \"" + portName + "\" failed.");		}	    } else {		env.failure("No port specifed and no current default defined.");	    }	} else {	    portName = t.nextToken();	    try {		runtime.attach(portName);	    } catch (VMLaunchFailureException e) {		env.failure("Attempt to attach to port \"" + portName + "\" failed.");	    }	    context.setRemotePort(portName);	}    }    // Command: detach    private void commandDetach(StringTokenizer t) throws NoSessionException {	runtime.detach();    }    // Command: interrupt    private void commandInterrupt(StringTokenizer t) throws NoSessionException {	runtime.interrupt();    }    // Command: suspend    private void commandSuspend(StringTokenizer t) throws NoSessionException {        if (!t.hasMoreTokens()) {	    // Suspend all threads in the current thread group.	    //### Issue: help message says default is all threads.	    //### Behavior here agrees with 'jdb', however.            ThreadIterator ti = currentThreadGroupThreads();            while (ti.hasNext()) {                // TODO - don't suspend debugger threads                ti.nextThread().suspend();            }            env.notice("All (non-system) threads suspended.");        } else {            while (t.hasMoreTokens()) {                ThreadReference thread = findThread(t.nextToken());                if (thread != null) {                    //thread.suspend();		    runtime.suspendThread(thread);                }                            }        }    }    // Command: resume    private void commandResume(StringTokenizer t) throws NoSessionException {         if (!t.hasMoreTokens()) {	    // Suspend all threads in the current thread group.	    //### Issue: help message says default is all threads.	    //### Behavior here agrees with 'jdb', however.            ThreadIterator ti = currentThreadGroupThreads();            while (ti.hasNext()) {                // TODO - don't suspend debugger threads                ti.nextThread().resume();            }	    env.notice("All threads resumed.");         } else {             while (t.hasMoreTokens()) {                ThreadReference thread = findThread(t.nextToken());                if (thread != null) {                    //thread.resume();		    runtime.resumeThread(thread);		}	     }	 }    }    // Command: cont    private void commandCont() throws NoSessionException {	try {	    runtime.go();	} catch (VMNotInterruptedException e) {	    //### failure?	    env.notice("Target VM is already running.");	}    }    // Command: step    private void commandStep(StringTokenizer t) throws NoSessionException{	ThreadReference current = context.getCurrentThread();        if (current == null) {            env.failure("No current thread.");            return;        }        try {            if (t.hasMoreTokens() &&		t.nextToken().toLowerCase().equals("up")) {		runtime.stepOut(current);            } else {                runtime.stepIntoLine(current);            }        } catch (AbsentInformationException e) {            env.failure("No linenumber information available -- " +			    "Try \"stepi\" to step by instructions.");        }    }    // Command: stepi    private void commandStepi() throws NoSessionException {	ThreadReference current = context.getCurrentThread();	if (current == null) {            env.failure("No current thread.");            return;        }        runtime.stepIntoInstruction(current);    }    // Command: next    private void commandNext() throws NoSessionException {	ThreadReference current = context.getCurrentThread();        if (current == null) {            env.failure("No current thread.");            return;        }        try {	    runtime.stepOverLine(current);        } catch (AbsentInformationException e) {            env.failure("No linenumber information available -- " +                             "Try \"nexti\" to step by instructions.");        }    }    // Command: nexti  (NEW)    private void commandNexti() throws NoSessionException {	ThreadReference current = context.getCurrentThread();        if (current == null) {            env.failure("No current thread.");            return;        }	runtime.stepOverInstruction(current);    }    // Command: kill    private void commandKill(StringTokenizer t) throws NoSessionException {	//### Should change the way in which thread ids and threadgroup names	//### are distinguished.         if (!t.hasMoreTokens()) {            env.error("Usage: kill <threadgroup name> or <thread id>");            return;        }        while (t.hasMoreTokens()) {            String idToken = t.nextToken();            ThreadReference thread = findThread(idToken);            if (thread != null) {		runtime.stopThread(thread);                env.notice("Thread " + thread.name() + " killed.");                return;            } else {                /* Check for threadgroup name, NOT skipping "system". */		//### Should skip "system"?  Classic 'jdb' does this.		//### Should deal with possible non-uniqueness of threadgroup names.		ThreadGroupIterator itg = allThreadGroups();                while (itg.hasNext()) {                    ThreadGroupReference tg = itg.nextThreadGroup();                    if (tg.name().equals(idToken)) {                        ThreadIterator it = new ThreadIterator(tg);                        while (it.hasNext()) {                            runtime.stopThread(it.nextThread());			}			env.notice("Threadgroup " + tg.name() + "killed.");                        return;                    }                }                env.failure("\"" + idToken +			    "\" is not a valid threadgroup or id.");            }        }    }    /*************    // TODO    private void commandCatchException(StringTokenizer t) throws NoSessionException {}    // TODO    private void commandIgnoreException(StringTokenizer t) throws NoSessionException {}    *************/    // Command: up    //### Print current frame after command?        int readCount(StringTokenizer t) {        int cnt = 1;        if (t.hasMoreTokens()) {            String idToken = t.nextToken();            int n;            try {                cnt = Integer.valueOf(idToken).intValue();            } catch (NumberFormatException e) {                cnt = -1;            }        }        return cnt;    }        void commandUp(StringTokenizer t) throws NoSessionException {	ThreadReference current = context.getCurrentThread();        if (current == null) {            env.failure("No current thread.");            return;        }        int nLevels = readCount(t);        if (nLevels <= 0) {            env.error("usage: up [n frames]");            return;        }            	try {	    int delta = context.moveCurrentFrameIndex(current, -nLevels);	    if (delta == 0) {		env.notice("Already at top of stack.");	    } else if (-delta < nLevels) {		env.notice("Moved up " + delta + " frames to top of stack.");	    }	} catch (VMNotInterruptedException e) {	    env.failure("Target VM must be in interrupted state.");	}    }        private void commandDown(StringTokenizer t) throws NoSessionException {	ThreadReference current = context.getCurrentThread();        if (current == null) {            env.failure("No current thread.");            return;        }        int nLevels = readCount(t);        if (nLevels <= 0) {            env.error("usage: down [n frames]");            return;        }            	try {	    int delta = context.moveCurrentFrameIndex(current, nLevels);	    if (delta == 0) {		env.notice("Already at bottom of stack.");	    } else if (delta < nLevels) {		env.notice("Moved down " + delta + " frames to bottom of stack.");	    }	} catch (VMNotInterruptedException e) {	    env.failure("Target VM must be in interrupted state.");	}    }        // Command: frame        private void commandFrame(StringTokenizer t) throws NoSessionException {	ThreadReference current = context.getCurrentThread();        if (current == null) {            env.failure("No current thread.");            return;        }        if (!t.hasMoreTokens()) {            env.error("usage: frame <frame-index>");            return;        }                    String idToken = t.nextToken();        int n;        try {            n = Integer.valueOf(idToken).intValue();        } catch (NumberFormatException e) {            n = 0;        }        if (n <= 0) {            env.error("use positive frame index");            return;        }	try {	    int delta = context.setCurrentFrameIndex(current, n);	    if (delta == 0) {		env.notice("Frame unchanged.");	    } else if (delta < 0) {		env.notice("Moved up " + -delta + " frames.");	    } else {		env.notice("Moved down " + delta + " frames.");	    }	} catch (VMNotInterruptedException e) {	    env.failure("Target VM must be in interrupted state.");	}    }    // Command: where    //### Should we insist that VM be interrupted here?    //### There is an inconsistency between the 'where' command    //### and 'up' and 'down' in this respect.    private void commandWhere(StringTokenizer t, boolean showPC)						throws NoSessionException {	ThreadReference current = context.getCurrentThread();        if (!t.hasMoreTokens()) {            if (current == null) {                env.error("No thread specified.");                return;            }            dumpStack(current, showPC);        } else {            String token = t.nextToken();            if (token.toLowerCase().equals("all")) {		ThreadIterator it = allThreads();		while (it.hasNext()) {

⌨️ 快捷键说明

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