Gdb - pipe debugging

Let's say I have two programs named blah and ret . I want to debug a blah program that receives input from a ret program through I / O redirection. How to debug blah program in the following case using gdb?

bash> ret | blah 
+34
c gdb
Sep 21 '09 at 19:05
source share
2 answers

First you can run the program and debug it using pid. This decision, of course, does not apply to all cases.

Another approach is to use the power of Linux for interprocess communication. In short, you redirect the output of ret to a special FIFO file ("named pipe"), and then read from this FIFO through the debugger. Here's how to do it. From bash, run:

 mkfifo foo 

This creates a special file in your directory that will serve as a named pipe. When you write text to this file (using the same echo "Hello" >foo syntax echo "Hello" >foo ), the writer will block until someone reads the data from the file (for example, cat <foo ). In our case, a process controlled by gdb will be read from this file.

Once you have created fifo, start with bash:

 ret > foo & # ampersand because it may block as nobody is reading from foo gdb blah 

Then at gdb command prompt run

 run <foo 

And get the desired effect. Please note that you cannot read data from fifo (as well as from a regular channel) twice: when you read all the data, the blah process dies and you must repeat the command written in foo (you can do this from another shell window).

When you're done, delete fifo with rm foo (or put it in a directory where it will be automatically deleted when the system reboots, for example /tmp ).

+41
Sep 21 '09 at 19:25
source share
โ€” -

The GDB run command uses bash to perform redirection. A simple way to achieve the equivalent of ret | blah ret | blah is the use of the bash function overriding the process .

 $ gdb blah ... (gdb) run < <(ret) 



Explanation: bash replaces <(ret) with something like /dev/fd/123 , which is the stdout ret file descriptor. We can use this fd similarly to the named FIFO, as described in another answer, except that we do not need to manually create it ourselves and not worry about the lifetime of the ret process.

+9
Jan 05 '16 at 21:26
source share



All Articles