Named pipes for debugging

UPDATE 2021/03/04: After reviewing some posts (like this one) I’ve come to the conclusion that this is a pretty dumb idea but I will keep it up because there’s no need to hide it.

During a project for school, I came across the worst problem I could ever have: not having tools to debug.

I had to write a program that would be executed by a PHP script in command line, I decided to write the program in C++.
The program was supposed to receive parameters on STDIN:

  • Money you start with;
  • Number of cycles;
  • Loop: number.

The STDOUT and STDERR was thrown into a pipe to be used by the PHP script, so I couldn’t write in the console any calculation I need to check.
The only way to get an output was to write somewhere else than on the STDOUT and STDERR.

Writing to files

I thought about writing my debugging output into a file, but then I encountered a problem: what if I get an infinite loop ?
I could kill the program.

Writing to files wasn’t really helping with real-time debugging.
And I could easily fill my hard-drive

Writing to a named pipe

Named pipes are the best for real-time debugging !
I could nearly make tea and pour it into a cup.

Since I was running Fedora 18, I could use named pipes so I decided to give it a try, I wrote a simple script:

#!/bin/sh

if [ -e "./debug.pipe" ]
then
        rm -f "./debug.pipe"
fi

mkfifo "./debug.pipe"
while (cat "./debug.pipe")
do
        cat "./debug.pipe"
done

I then needed to write the debugging data directly into the pipe like if I was writing a normal file.