Output redirection

Eli Zaretskii eliz@delorie.com
Sat Jul 22 02:22:00 GMT 2000

> From: Chris Faylor <cgf@cygnus.com>
> Date: Fri, 21 Jul 2000 19:36:27 -0400
> On Fri, Jul 21, 2000 at 04:05:17PM -0700, Sashan Govender wrote:
> >Why can't I get output redirection work with gdb 4.18 (configured as
> >'i386-mingw32') using Win95?
> >This is what i do after starting gdb:
> >
> >	file testprog.exe
> >
> >	run > out.txt
> This kind of redirection doesn't work on Windows gdb.  Sorry.
> >Also does gdb separate its output from the output from the program
> >that is being debugged internally? Does it send the debugee's output
> >to another stream? As far as I can tell both outputs go to the same
> >stream (stdout), even with a new console set. I'm interested in being
> >able to separate the output from gdb from the output of program being
> >debugged because I have to write a GUI for Windows for gdb.
> If you are debugging a console application with the console version of
> gdb, the output goes to the same place as the gdb output.

I was annoyed by similar misbehavior in the DJGPP (a.k.a. MS-DOS) port
of GDB for many years, until I finally fixed this a year or so ago.

The solution involves a bunch of functions that redirect standard
handles (using dup and dup2) between the debugger's own and debuggee's
streams.  That is, the target-specific module in GDB (go32-nat.c, in
my case) switches the standard handles to the debugger's streams when
the debuggee stops, and switches them back again when the debuggee is
about to be resumed.

This creates an illusion of two separate sets of standard handles that
works well enough.  It even supports the case when the debuggee closes
one of its standard handles (previously, if that happened, GDB would
lose the corresponding handle too; if that handle was stdin, you will
not be able to talk to GDB anymore!)

Perhaps someone would like to reuse the code I wrote for this in the
Mingw port.  Look in go32-nat.c for the calls to functions whose names
begin with "redir_"; the two functions `redir_to_debugger' and
`redir_to_child', called from `go32_terminal_ours' and
`go32_terminal_inferior', are the workhorse of this machinery.  The
functions themselves are part of the DJGPP's debug support library;
see the file src/debug/common/dbgredir.c in the DJGPP source
distribution (djlsr203.zip from the usual DJGPP sites).

The code in dbgrdir.c includes proper handling of quotes in command
lines, to emulate the behavior of stock DOS/Windows shell COMMAND.COM;
thus, the argument to the "run" command may be a full-fledged
command-line tail as you'd typically type it at the command prompt.

More information about the Gdb mailing list