I have a program that takes in piped input:
bash> echo "something" | ./program 'seomthingelse'
How can I redirect this input into gdb, WITHOUT creating a new file?
How can I redirect this input into gdb, WITHOUT creating a new file?
echo "something" | ./program 'seomthingelse', and attach to it from another window while it is still being in the delay.The second solution is often quite useful for programs that are very particular about how they are invoked, and I find the following implementation to work well:
int main(int argc, char *argv[])
{
if (getenv("WAIT_FOR_GDB") != NULL) {
int done = 0;
while (!done) sleep(1);
}
/* rest of main */
}
Then you set WAIT_FOR_GDB in the environment, and can take arbitrary time to attach the process. Once attached, step up from sleep, set var done = 1, set any other breakpoints you need, and continue.
You can use gdbserver for this. stdin will be available, unmodified, to the target program.
In one terminal session:
bash> echo "something" | gdbserver ./program 'seomthingelse'
Process ./program created; pid = 1591
Listening on port 1234
At this point, ./program has been paused, just after it was started.
In another terminal session:
bash> gdb ./program
(gdb) target remote localhost:1234
Remote debugging using localhost:1234
Loaded symbols for /lib/ld-linux.so.2
0x48611020 in _start () from /lib/ld-linux.so.2
(gdb)
gdbserver may be in your distro's gdb package, or you may need to install another package. On Fedora, it's gdb-gdbserver.