Hey everyone! I'm currently working with GDB, and I'm trying to send commands directly to it through stdin. I've been experimenting with the following command to send 'i r' to GDB:
`echo -e "i rn" > /proc/$(ps aux | grep "gdb ./args2" | awk 'NR == 1 {print $2}')/fd/0`
I also tried this variant:
`echo -e "i rrn" > /proc/$(ps aux | grep "gdb ./args2" | awk 'NR == 1 {print $2}')/fd/0`
While I can see the string 'i r' in GDB, it doesn't execute the command until I manually press Enter. Is there a way to make this happen automatically without hitting Enter? Just to clarify, I'm looking to do it using only echo and stdin, avoiding tools like expect. It's become a bit tricky, especially since I noticed that GDB reads one character at a time during syscalls. Any advice would be greatly appreciated!
4 Answers
It looks like you might have redirected the stdout from your echo command. If you haven’t already, give this a shot:
`printf "i rn" > /proc/$(ps aux | grep "[g]db ./args2" | awk 'NR==1 {print $2}')/fd/0`. Just a tweak to the syntax!
It seems like writing to `/proc/$pid/fd/0` simply outputs to the connected terminal instead of providing input to GDB. You might need a different approach altogether, like executing GDB and regularly piping commands into it. Alternatively, check out the GDB documentation on using interpreters if you're looking for other ways to control it programmatically.
Debugging your debugger sounds interesting! If I get it right, you're trying to send commands to GDB from a separate session, but using `r` or `n` in your echo command doesn't seem to work as you hoped. Have you looked into whether GDB has any built-in methods for this? Like starting it with stdin from a pipe or something? Just a thought!
Instead of using `ps` along with `grep` and `awk`, why not simplify with `pgrep`? You could try this instead:
`pgrep -of "gdb ./args2"`. It might save you some hassle!
I tried it, but it's still the same issue. I need to go to GDB and press Enter every time.