I'm trying to write some script in perl for control of GDB debugging process. In following code I'm able to run GDB and pass some MI2 command, but I cannot catch the output from GDB:
open(G, "| gdb -n -q --interpreter=mi2") || die("error opening gdb");
syswrite(G, "-file-exec-and-symbols ./a.out\n");
$x = sysread(G, $resp, 4096);
printf("x: %u\n", $x);
printf("resp: '%s'\n", $resp)
and the result is:
x: 0
resp: ''
=thread-group-added,id="i1"
(gdb)
^done
(gdb)
The problem here is that GDB prints output '=thread-group-added,id="i1"' directly to the tty so I cannot catch it in my perl script for further interpretation. I'd like to catch the output from GDB to some variable and interprete it. As can be seen in the output sysread() procedure didn't read anything ("x: 0"). How can I do this?
Edit: After a while of thinking I achieved some solution to catch the output from GDB. It requires named pipe (below it is "/tmp/pipe"):
open (G, "| gdb -n -q --interpreter=mi2 >& /tmp/pipe") || die("error opening gdb");
And now I am able to read from perl script the output from GDB and parse it.