1

我正在为一个 uni 项目使用 JDI 编写我自己的调试器。使用此调试器,我想记录已执行的代码行和目标程序的属性。稍后我想可视化日志文件。

我得到了调试器,它记录了行和属性的值,但是当我调试 Java 控制台应用程序并且控制台应用程序需要 cmd 中的输入时,它会卡住,因为我无法给出一个输入。

现在一些代码:

这是我的调试器卡住的部分

    public static void options() throws IOException {
        System.out.println("Choose an Option:");
        System.out.println("Type \"count\" to start the counting function.");
        System.out.println("Type \"vowels\" to count the vowels of a String.");
        System.out.println("Type \"consonants\" to count the consonants of a String.");
        System.out.println("Type \"both\" to count vowels and consonants.");
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String entry = reader.readLine();

当调试器到达 reader.readline() 部分时,它会卡住,因为被调试的程序正在等待输入,但我无法将输入转发给它,因为我的 cmd 正在运行调试器程序并且调试器没有处理输入。

这是我的调试器的主要方法:

        VirtualMachine vm = null;
        try {
            vm = log.connectAndLaunchVM();
            log.enableClassPrepareRequests(vm);
            EventSet eventSet;
            while ((eventSet = vm.eventQueue().remove()) != null){
                for (Event e : eventSet){
                    if(e instanceof ClassPrepareEvent) {
                        log.handleClassPrepareEvents(vm, (ClassPrepareEvent) e);
                    }
                    if(e instanceof StepEvent){
                        log.printLocalVariables((StepEvent) e);
                    }
                    vm.resume();
                }
            }
        }catch (VMDisconnectedException e){
            System.out.println("VM disconnected.");
        } catch (Exception e){
            e.printStackTrace();

我想到了两种可能的解决方案:

  • 在它自己的 cmd 窗口中打开目标调试对象。
  • 不知何故让调试器转发并接收被调试者的输入和输出

我试着让它与 vm 一起工作。处理.getInputStream() 和 .getOutputStream(),但无法使其正常工作,因为我不知道在调试器中处理 getOutputStream 的位置。

提前感谢您提供有关如何执行此操作的任何答案和提示!如果您对我的问题有任何疑问,我很乐意回答。

4

0 回答 0