www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp-workers/1999/05/31/02:42:30

Date: Mon, 31 May 1999 09:38:53 +0300 (IDT)
From: Eli Zaretskii <eliz AT is DOT elta DOT co DOT il>
X-Sender: eliz AT is
To: djgpp-workers AT delorie DOT com
Subject: Re: gdb 4.18 for DJGPP (alpha)
Message-ID: <Pine.SUN.3.91.990531093720.10861O-100000@is>
MIME-Version: 1.0
Reply-To: djgpp-workers AT delorie DOT com

Here's a simple test program that can be used to demonstrate the GDB
support for redirection in the debuggee.  Simply put a breakpoint in
`main' and then single-step through the program watching how GDB works
correctly even though the debuggee commits atrocities against the 3
standard handles.  Typing "info terminal" at several points should also
reveal interesting information.

Needless to say, previous versions of GDB would fail miserably while
debugging such programs.  For example, if you step through the code
that closes handle 0, previous versions of GDB will promptly exit
because the debugger hits EOF on its stdin.

Have fun.

--------------------------------------------------------------------
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <io.h>

int main (void)
{
  int fd0 = open ("tredir.dat", O_RDONLY | O_BINARY);
  int fd1 = open ("tredir.out", O_WRONLY | O_APPEND | O_CREAT, 0644);
  int fd2 = open ("tredir.err", O_WRONLY | O_TRUNC | O_CREAT, 0644);
  int fd00 = dup (0);
  int fd10 = dup (1);
  int fd20 = dup (2);

  if (fd0 == -1 || fd1 == -1 || fd2 == -1
      || fd00 == -1 || fd10 == -1 || fd20 == -1)
    printf ("BLOODY MURDER!!\n");

  printf ("Type _exactly_ 2 letters and hit RET: "); fflush (stdout);
  fputc (fgetc (stdin), stdout);
  fputc (fgetc (stdin), stdout);
  fputc (fgetc (stdin), stdout);

  printf ("This is stdout.\n");
  fprintf (stderr, "This is stderr.\n");

  if (close (0) != 0)
    printf ("couldn't close stdin: %s\n", strerror (errno));
  else
    printf ("stdin closed.\n");

  if (fgetc (stdin) != EOF)
    printf ("stdin is still valid when it shouldn't!\n");
  else
    printf ("stdin is now invalid.\n");

  if (dup2 (fd0, 0) == -1)
    printf ("couldn't redirect stdin: %s\n", strerror (errno));
  else
    printf ("stdin redirected.\n");

  if (fgetc (stdin) == EOF)
    printf ("stdin invalid when it should be!\n");
  else
    printf ("redirected stdin okay.\n");

  if (dup2 (fd00, 0) == -1)
    printf ("cannot restore stdin: %s\n", strerror (errno));
  else
    printf ("stdin restored.\n");

  setmode (0, O_BINARY);
  printf ("stdin should be in raw mode now.\n");

  setmode (0, O_TEXT);
  printf ("stdin should be back in cooked mode.\n");

  if (close (1) != 0)
    fprintf (stderr, "couldn't close stdout: %s\n", strerror (errno));
  else
    fprintf (stderr, "stdout closed.\n");

  if (printf ("This is stdout\n") != EOF && fflush (stdout) != EOF)
    fprintf (stderr, "stdout is still valid when it shouldn't!\n");
  else
    fprintf (stderr, "stdout is now invalid.\n");

  if (dup2 (fd1, 1) == -1)
    fprintf (stderr, "couldn't redirect stdout: %s\n", strerror (errno));
  else
    fprintf (stderr, "stdout redirected.\n");

  clearerr (stdout);

  if (printf ("This is stdout\n") == EOF)
    fprintf (stderr, "stdout not valid when it should be!\n");
  else
    fprintf (stderr, "redirected stdout okay.\n");
  fprintf (stderr,
	   "If you saw \"This is stdout\" message, stdout isn't redirected\n");

  if (dup2 (fd10, 1) == -1)
    fprintf (stderr, "cannot restore stdout: %s\n", strerror (errno));
  else
    printf ("stdout restored (this is stdout speaking).\n");

  if (close (2) != 0)
    fprintf (stdout, "couldn't close stderr: %s\n", strerror (errno));
  else
    fprintf (stdout, "stderr closed.\n");

  if (fprintf (stderr, "This is stderr\n") != EOF && fflush (stderr) != EOF)
    fprintf (stdout, "stderr is still valid when it shouldn't!\n");
  else
    fprintf (stdout, "stderr is now invalid.\n");

  if (dup2 (fd2, 2) == -1)
    fprintf (stdout, "couldn't redirect stderr: %s\n", strerror (errno));
  else
    fprintf (stdout, "stderr redirected.\n");

  clearerr (stderr);

  if (fprintf (stderr, "This is stderr\n") == EOF)
    fprintf (stdout, "stderr not valid when it should be!\n");
  else
    fprintf (stdout, "redirected stderr okay.\n");
  fprintf (stdout,
	   "If you saw \"This is stderr\" message, stderr isn't redirected\n");

  if (dup2 (fd20, 2) == -1)
    fprintf (stdout, "cannot restore stderr: %s\n", strerror (errno));
  else
    fprintf (stderr, "stderr restored (this is stderr speaking).\n");

  printf ("\nTHAT'S ALL, FOLKS!!\n");

  return 0;
}

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019