I'm trying to get gdb to run programs with input redirection to stdin. For example, without gdb I would run a program like this:
prog < input.txt
Now in gdb, the usual way to do this is run < input.txt
. However, it doesn't work for me and when doing this nothing gets redirected into stdin.
I'm using Windows with MinGW. What could be the problem?
As far back as the late '90s, broken command line redirection was a known and assumed limitation. My suspicion is that it remains that way, since the
mingw32
port ofgdb
still gleefully passes on verbatim allrun
arguments (including redirects) to the debugee.Several possible workarounds:
bbadour
's suggestionotherwise, if you have symbols for the debugee (
gcc -g
) or you know the address ofmain()
(gcc -Wl,-Map,mapfile
) and can set a breakpoint there, proceed in the following manner (tested withmingw gdb 6.8.0
):I ran into the same issue here, and I just got into the habit of adding a command-line argument to allow grabbing input from a file.
e.g. 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.
Then I just use those arguments instead of redirects.
The tools that come with MinGW often are not the latest versions and often have features omitted. ::shrug::
Input redirection is supported starting with GDB 8.0. From the NEWS file:
Native debugging on MS-Windows supports command-line redirection
Command-line arguments used for starting programs on MS-Windows can now include redirection symbols supported by native Windows shells, such as '<', '>', '>>', '2>&1', etc. This affects GDB commands such as "run", "start", and "set args", as well as the corresponding MI features.