17 April 2012

Input Redirection in GDB (MinGW)

When using GDB to debug my program, I ran into a problem that GDB cannot accept my input redirection, because the GDB provided with MinGW passes on verbatim all arguments including redirects to the debugee.

I have found several solutions to solve this problem on the Internet, which I decide to write down here in case that I forget them.
  1. Parsing a -i ifile argument using argc and argv to get input from ifile instead of stdin and parsing a -o ofile to write output to ofile instead of stdout.
  2. Setting symbols for the debugee while compiling (gcc -g), and proceeding in the following manner:
    (gdb) b main
    (gdb) r non-redirect-arguments-if-any
    (gdb) p dup2(open("input.txt", 0), 0)
    (gdb) c

No comments:

Post a Comment