12

It seems that input redirection in gdb does not work in Cygwin e.g

(gdb) run < input.txt

Is there other way to redirect input in gdb of Cygwin??

1
  • 3
    This is a limitation of Cygwin, there is various workarounds for that, you can try solutions of this related question : stackoverflow.com/questions/3544325/… Commented May 5, 2012 at 17:20

1 Answer 1

12
+100

Unfortunately this is not possible when running gdb in cygwin. The bug exists for a quote long time, but apparently it's a hard one to fix - and probably the gdb devs prefer spending time on features/issues relevant to more common environments (such as Linux).

There are various possible workarounds; I'd prefer the first one since it's the cleanest and also useful while not debugging / running on cygwin:

  • Add a command line argument, e.g. -f whatever with whatever being the filename to read from. If the argument is not present or set to -, read from stdin. The -f - option is optional of course but for arguments accepting filenames it's a common standard (as long as it makes sense) to handle - as "use stdin/out".
  • Use the gdb hack mentioned here to remap stdin to a manually opened file inside the application:

    > gdb yourexecutable
    (gdb) break main
    (gdb) run
    (gdb) call dup2(open("input.txt", 0), 0)
    (gdb) continue
    

    This sets a breakpoint on the main function, then executes the program which will break right after entering main. Then dup2 is used to replace the stdin fd (0) with a file descriptor of the input file.

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

1 Comment

Alas, this results in Segmentation fault (core dumped) with "GNU gdb (GDB) (Cygwin 8.1.1-1) 8.1.1" :(

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.