1
votes

I am trying to build a GUI for GCC which has some basic functionalities like compile, link, execute, debug, etc for C++ programs using Java. I am creating strings of command which I pass to the ProcessBuilder and run it via command prompt and GCC.

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

This is a sample code for compiling the file.

Part of this is the debug functionality for which I am using GDB. Now the problem is GDB needs additional input to add breakpoints, remove breakpoints and so on. I am having trouble on how to pass these necessary inputs to GDB via Java terminal. If I pass the commands in the command prompt, it is working fine and I get the desired output. enter image description here

But whenever I fire the GDB command from the Java program, I cannot pass any inputs from the IDE's terminal. I am aware that each GDB command uses a different process and I tried attaching Java's process ID to GDB but I just get a blank output console. It seems that the GDB session has started but there is no way to interact with that process through the IDE's output console.

int pid = Integer.parseInt(ManagementFactory.getRuntimeMXBean().getName().split("@")[0]);
command = "gdb attach "+ pid;
fireCommand();

EDIT

This is the method that interacts with command prompt to take input and display output in the IDE's output console:

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
    }

Any leads on this would be very helpful. Thank you.

1
Can you show the code that runs gdb from Java? - Joni
@Joni I have added the code that I am using to pass commands to the command prompt and display the results. When I pass gdb -version to the above method, it shows the information. However, if I try to start a gdb session using just gdb command, I can type in the output console but it does not process. - Aayush Lamichhane

1 Answers

0
votes

I am aware that each GDB command uses a different process

No, gdb runs as one process. Did you mean to say that you are creating a new gdb process every time you try to pass it a gdb command ?

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

Maybe you should use The GDB/MI Interface, which e.g. the Emacs debugger mode gud uses.