title: Programmatically interacting with gdb using java
body: <p>I'm writing an application that uses GDB to access information through java.</p>
  
  <p>Using Runtime.getRuntime.exec, I'm able to attach GDB to any processes.</p>
  
  <p>The problem is that I am unable to send inputs to GDB after it has been started.</p>
  
  <p>**EDIT (19/8/2011): </p>
  
  <p>At the line "out.println(gdbcommand)", gdb is started. How do I get the stdout of the newly spawned gdb, write inputs to it and then read the stdin. So far, I'm only able get the output up till "out.println(gdbcommand)". All attempts at trying to programmatically send inputs to gdb have not been working so far.**</p>
  
  <p>Refer to trojanfoe's comments below my question.
  Below is an edited sample of my code:</p>
  
  <pre><code>try {   
      String gdbcommand = "/data/bin/gdb " + pidS + " " + pidS;
      String line;
  
      Process process = Runtime.getRuntime().exec("su");
  
      if (process != null) {
          BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
          PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(process.getOutputStream())),true);
  
          out.println(gdbcommand);
  
          //Note this line does not get sent to gdb's interface after starting gdb.
          //Is it possible to connect to the new stdout of gdb's interface?
          out.println("info registers");
  
          out.flush();
          out.close();
  
          while ((line = in.readLine()) != null) {
              System.out.println(line);
          } 
  
          process.destroy();
      }
  
  } catch (Exception e) {
      e.printStackTrace();
  }
  </code></pre>
  
  <p>Note that "info registers" is not sent to GDB's interface and after calling out.close(), gdb's interface quits. (As seen below)</p>
  
  <pre><code>08-18 14:40:36.595: INFO/System.out(4408): 0xafd0c51c in epoll_wait () from /system/lib/libc.so
  
  08-18 14:40:36.595: INFO/System.out(4408): (gdb) Hangup detected on fd 0
  
  08-18 14:40:36.595: INFO/System.out(4408): error detected on stdin
  
  08-18 14:40:36.595: INFO/System.out(4408): The program is running.  Quit anyway (and detach it)? (y or n) [answered Y; input not from terminal]
  </code></pre>