Shuzheng
Shuzheng

Reputation: 13840

How to redirect STDOUT of a program to STDIN of a GDB-debugged program?

I normally redirect STDOUT to another program using:

python -c 'print("HelloWorld")' | ./myprog

I know that I can supply the contents of a file as STDIN for a debugged program in GDB:

(gdb) run myprog < input.txt

However, how can I do something like:

(gdb) run mypprog < python -c 'print("HelloWorld")'

without first having to create a file with the output of python -c '...'?

Upvotes: 2

Views: 681

Answers (1)

nmichaels
nmichaels

Reputation: 50941

One way is to attach gdb to your already-running process. Find its pid with ps or top. Let's say that it's 37. Then run

(gdb) attach 37

That probably won't work for your case with very short run time though. Another approach is to use a fifo.

mkfifo fifo
python -c 'print("Hello World")' > fifo &
gdb myprog
run < fifo

Upvotes: 1

Related Questions