2

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 '...'?

0

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

4 Comments

Say, I attach to the process. How would I then send input to the process?
If you run something like python generate_lots_of_data.py | ./process-to-debug it's already got input going to it. If you only want to send input to the process when you're ready for it, use the fifo approach.
Thank you - so you cannot interact with the application unless you use a FIFO? (I.e. send input at different points in the execution)
If you interact with it directly, you can attach gdb to the running process. For example, you could run cat in one terminal and attach gdb to it in another. Since cat is just sitting there waiting for input, you can manually send it something and watch it get processed. For programmatic output, though, you'll want a FIFO.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.