tags:

views:

447

answers:

1

If i have a two programs named blah and ret.. I want to debug blah program which receives input from ret program via pipes .. how do I debug the blah program in the following case using gdb

bash> ret | blah

+6  A: 

At first, you can run the program and debug it by pid. This solution, of course, doesn't cover all cases.

Another approach is to use linux capabilities of inter-process communication. Outline is that you redirect your output of ret to a FIFO and then read from that FIFO via debugger. Here's how it's done. 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 syntax echo "Hello" >foo), the writing program will block until someone reads the data from the file (cat <foo). And the program reading data could be gdb's proces... well, you're getting the idea, aren't you?

After you created a fifo, run from bash:

ret > foo &   # ampersand because it blocks when trying to write to fifo
gdb blah

Then, in gdb prompt, you may run

run <foo

And get the desired effect. Note that you can't read fifo (and the usual pipe as well) twice: when you've read all the data, the blah process dies and you should repeat the command writing to foo (you may do it from the other shell window).

When you're done, remove the fifo with the usual rm foo (or place it into the directory where it will automatically be removed upon system restart).

Pavel Shved
If you can afford the disk space you could also just pipe it into a regular file `foo`, instead of a FIFO `foo` (saves you one command :).