3

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?

2 Answers 2

1

How can I redirect this input into gdb, WITHOUT creating a new file?

  • You can create a named pipe (not sure whether this counts as a "new file" or not -- why are you trying to avoid a new file?)
  • You can put a delay into the program, start it with 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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

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.