java - 如何通过Java输出控制台运行GDB并接受GDB命令?

标签 java c++ debugging gcc gdb

我正在尝试为GCC构建GUI,该GUI具有一些基本功能,例如使用Java对C++程序进行编译,链接,执行,调试等。我正在创建命令字符串,然后将其传递给ProcessBuilder并通过命令提示符和GCC运行它。

command = "cd src & cd Resources & g++ " + compileFile.cpp +" -Wall "+ " -o "+ "tempOut";

这是用于编译文件的示例代码。

其中一部分是我正在使用GDB的调试功能。现在的问题是GDB需要额外的输入来添加断点,删除断点等等。我在如何通过Java终端将这些必要的输入传递给GDB时遇到麻烦。如果我在命令提示符下传递命令,则它工作正常,并且得到了所需的输出。
enter image description here

但是,每当我从Java程序中触发GDB命令时,就无法传递来自IDE终端的任何输入。我知道每个GDB命令使用一个不同的进程,我尝试将Java的进程ID附加到GDB,但是我只得到一个空白的输出控制台。 GDB session 似乎已经开始,但是无法通过IDE的输出控制台与该过程进行交互。
int pid = Integer.parseInt(ManagementFactory.getRuntimeMXBean().getName().split("@")[0]);
command = "gdb attach "+ pid;
fireCommand();

编辑

这是与命令提示符进行交互以在IDE的输出控制台中进行输入并显示输出的方法:
public void fireCommand() {
        String line;
        String os = System.getProperty("os.name");
        ProcessBuilder builder;
        if(os.startsWith("Win")) {
            builder = new ProcessBuilder("cmd.exe", "/c", command);
        }
        else{
            builder = new ProcessBuilder("bash", "-c", command);
        }
        try {

            process = builder.start();

            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(process.getInputStream()));

            while ((line = reader.readLine()) != null) {
                if(line.contains("Input the value")) {
                    //any other user input in non debug execution
                    String value = JOptionPane.showInputDialog(line);
                    BufferedWriter writer = new BufferedWriter(
                            new OutputStreamWriter(process.getOutputStream()));
                    writer.write(value, 0, value.length());
                    writer.newLine();
                    writer.close();
                }
                else {
                    output.append(line).append("\n");
                }
            }
            int exitVal = process.waitFor();
            if (exitVal == 0) {
                //display("Success!");
                display(output.toString());
            } else {
                String lineErr;
                BufferedReader readerErr = new BufferedReader(
                        new InputStreamReader(process.getErrorStream()));
                while ((lineErr = readerErr.readLine()) != null) {
                    outputErr.append(lineErr).append("\n");
                }
                //display(exitVal);
                display(outputErr.toString()); //Display the uncatched errors
            }

        } catch (IOException e) {
            display("There was a problem with the I/O");
            e.printStackTrace();
        } catch (InterruptedException e) {
            display("There was a interruption with the execution");
            e.printStackTrace();
        }
        if(!outputErr.toString().isEmpty())
        errorFormatDisplay();                                                   //display Error output function
    }

任何线索对此将是非常有帮助的。谢谢。

最佳答案

I am aware that each GDB command uses a different process



不,gdb作为一个进程运行。您是说每次尝试向其传递gdb命令时都在创建一个新的gdb进程吗?

It seems that the GDB session has started but there is no way to interact with that process through the IDE's output console.



也许您应该使用The GDB/MI Interface,例如Emacs调试器模式gud使用。

关于java - 如何通过Java输出控制台运行GDB并接受GDB命令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61034937/

相关文章:

c - perlbench 在 SPEC 2006 线束之外导致段错误

java - 上传文件时如何在 Google Drive API 中使用 newRevision 参数

c# - 如何查看 .NET 应用程序的堆栈跟踪

java - Statement.executeQuery() 花费太多时间。有什么办法可以优化这个吗?我正在使用雅典娜数据库

c++ - objected映射到unordered_map中,是系统初始化的吗?

c++ - static_pointer_cast<Derived> pReallyABase = static_pointer_cast<Derived>(pBase) 有效!为什么?

c++ - 如何删除分配的内存并仍然从方法返回它的值

javascript - Firefox 中的 Bootstrap Scrollspy 错误

java - 如何在 docker 容器中运行瘦 jar

java - 服务和 Activity 通信...从服务接收到数据时更新 UI