title: Writing tests that use GDB - how to capture output?
body: <p>I am trying to write tests that interact with GDB but am having trouble capturing the output. I would like for a log file to be generated which looks like what would have been seen in a terminal had the test been executed by hand. GDB is proving to be very stubborn when it comes to capturing its output however.</p>
  
  <p>I've been able to write Expect scripts which are able to interact with GDB and whose output can be redirected to a log file but I don't want to write my tests in TCL. I'm hoping to use Groovy, which is compatible with Java. For some reason with Perl's Expect and ExpectJ the program output always goes to the terminal and can't be redirected to a file.</p>
  
  <p>I tried starting a GDB process from Java using ProcessBuilder and it mostly works but the output of print statements never appear on stdout and can't be captured. I thought if Expect works then I'd launch expect from Java and have it interact with GDB but in this case most of the program output is lost, never appearing in the stdout of the created process.</p>
  
  <p>So my question is, how can I write a test in Groovy (Java would be fine as well) which interacts with GDB and can capture all of the output?</p>
  
  <p>Pseudo-code:</p>
  
  <pre><code>process = "gdb -q".execute()
  waitForPrompt()
  send("file exec")
  waitForPrompt()
  send("run")
  send("quit")
  </code></pre>
  
  <p>Log file:</p>
  
  <pre><code>(gdb) file exec
  Reading symbols from exec...done.
  (gdb) run
  Starting program: exec
  &lt;... output ...&gt;
  
  Program exited normally.
  (gdb) quit
  </code></pre>