OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-old/] [gdb-7.1/] [sim/] [common/] [nrun.c] - Diff between revs 227 and 816

Go to most recent revision | Only display areas with differences | Details | Blame | View Log

Rev 227 Rev 816
/* New version of run front end support for simulators.
/* New version of run front end support for simulators.
   Copyright (C) 1997, 2004, 2007, 2008, 2009, 2010
   Copyright (C) 1997, 2004, 2007, 2008, 2009, 2010
   Free Software Foundation, Inc.
   Free Software Foundation, Inc.
 
 
This program is free software; you can redistribute it and/or modify
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
(at your option) any later version.
 
 
This program is distributed in the hope that it will be useful,
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
GNU General Public License for more details.
 
 
You should have received a copy of the GNU General Public License
You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 
 
#include <signal.h>
#include <signal.h>
#include "sim-main.h"
#include "sim-main.h"
 
 
#include "bfd.h"
#include "bfd.h"
 
 
#ifdef HAVE_ENVIRON
#ifdef HAVE_ENVIRON
extern char **environ;
extern char **environ;
#endif
#endif
 
 
#ifdef HAVE_UNISTD_H
#ifdef HAVE_UNISTD_H
/* For chdir.  */
/* For chdir.  */
#include <unistd.h>
#include <unistd.h>
#endif
#endif
 
 
static void usage (void);
static void usage (void);
 
 
extern host_callback default_callback;
extern host_callback default_callback;
 
 
static char *myname;
static char *myname;
 
 
static SIM_DESC sd;
static SIM_DESC sd;
 
 
static RETSIGTYPE
static RETSIGTYPE
cntrl_c (int sig)
cntrl_c (int sig)
{
{
  if (! sim_stop (sd))
  if (! sim_stop (sd))
    {
    {
      fprintf (stderr, "Quit!\n");
      fprintf (stderr, "Quit!\n");
      exit (1);
      exit (1);
    }
    }
}
}
 
 
int
int
main (int argc, char **argv)
main (int argc, char **argv)
{
{
  char *name;
  char *name;
  char **prog_argv = NULL;
  char **prog_argv = NULL;
  struct bfd *prog_bfd;
  struct bfd *prog_bfd;
  enum sim_stop reason;
  enum sim_stop reason;
  int sigrc = 0;
  int sigrc = 0;
  int single_step = 0;
  int single_step = 0;
  RETSIGTYPE (*prev_sigint) ();
  RETSIGTYPE (*prev_sigint) ();
 
 
  myname = argv[0] + strlen (argv[0]);
  myname = argv[0] + strlen (argv[0]);
  while (myname > argv[0] && myname[-1] != '/')
  while (myname > argv[0] && myname[-1] != '/')
    --myname;
    --myname;
 
 
  /* INTERNAL: When MYNAME is `step', single step the simulator
  /* INTERNAL: When MYNAME is `step', single step the simulator
     instead of allowing it to run free.  The sole purpose of this
     instead of allowing it to run free.  The sole purpose of this
     HACK is to allow the sim_resume interface's step argument to be
     HACK is to allow the sim_resume interface's step argument to be
     tested without having to build/run gdb. */
     tested without having to build/run gdb. */
  if (strlen (myname) > 4 && strcmp (myname - 4, "step") == 0)
  if (strlen (myname) > 4 && strcmp (myname - 4, "step") == 0)
    {
    {
      single_step = 1;
      single_step = 1;
    }
    }
 
 
  /* Create an instance of the simulator.  */
  /* Create an instance of the simulator.  */
  default_callback.init (&default_callback);
  default_callback.init (&default_callback);
  sd = sim_open (SIM_OPEN_STANDALONE, &default_callback, NULL, argv);
  sd = sim_open (SIM_OPEN_STANDALONE, &default_callback, NULL, argv);
  if (sd == 0)
  if (sd == 0)
    exit (1);
    exit (1);
  if (STATE_MAGIC (sd) != SIM_MAGIC_NUMBER)
  if (STATE_MAGIC (sd) != SIM_MAGIC_NUMBER)
    {
    {
      fprintf (stderr, "Internal error - bad magic number in simulator struct\n");
      fprintf (stderr, "Internal error - bad magic number in simulator struct\n");
      abort ();
      abort ();
    }
    }
 
 
  /* We can't set the endianness in the callback structure until
  /* We can't set the endianness in the callback structure until
     sim_config is called, which happens in sim_open.  */
     sim_config is called, which happens in sim_open.  */
  default_callback.target_endian
  default_callback.target_endian
    = (CURRENT_TARGET_BYTE_ORDER == BIG_ENDIAN
    = (CURRENT_TARGET_BYTE_ORDER == BIG_ENDIAN
       ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
       ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
 
 
  /* Was there a program to run?  */
  /* Was there a program to run?  */
  prog_argv = STATE_PROG_ARGV (sd);
  prog_argv = STATE_PROG_ARGV (sd);
  prog_bfd = STATE_PROG_BFD (sd);
  prog_bfd = STATE_PROG_BFD (sd);
  if (prog_argv == NULL || *prog_argv == NULL)
  if (prog_argv == NULL || *prog_argv == NULL)
    usage ();
    usage ();
 
 
  name = *prog_argv;
  name = *prog_argv;
 
 
  /* For simulators that don't open prog during sim_open() */
  /* For simulators that don't open prog during sim_open() */
  if (prog_bfd == NULL)
  if (prog_bfd == NULL)
    {
    {
      prog_bfd = bfd_openr (name, 0);
      prog_bfd = bfd_openr (name, 0);
      if (prog_bfd == NULL)
      if (prog_bfd == NULL)
        {
        {
          fprintf (stderr, "%s: can't open \"%s\": %s\n",
          fprintf (stderr, "%s: can't open \"%s\": %s\n",
                   myname, name, bfd_errmsg (bfd_get_error ()));
                   myname, name, bfd_errmsg (bfd_get_error ()));
          exit (1);
          exit (1);
        }
        }
      if (!bfd_check_format (prog_bfd, bfd_object))
      if (!bfd_check_format (prog_bfd, bfd_object))
        {
        {
          fprintf (stderr, "%s: \"%s\" is not an object file: %s\n",
          fprintf (stderr, "%s: \"%s\" is not an object file: %s\n",
                   myname, name, bfd_errmsg (bfd_get_error ()));
                   myname, name, bfd_errmsg (bfd_get_error ()));
          exit (1);
          exit (1);
        }
        }
    }
    }
 
 
  if (STATE_VERBOSE_P (sd))
  if (STATE_VERBOSE_P (sd))
    printf ("%s %s\n", myname, name);
    printf ("%s %s\n", myname, name);
 
 
  /* Load the program into the simulator.  */
  /* Load the program into the simulator.  */
  if (sim_load (sd, name, prog_bfd, 0) == SIM_RC_FAIL)
  if (sim_load (sd, name, prog_bfd, 0) == SIM_RC_FAIL)
    exit (1);
    exit (1);
 
 
  /* Prepare the program for execution.  */
  /* Prepare the program for execution.  */
#ifdef HAVE_ENVIRON
#ifdef HAVE_ENVIRON
  sim_create_inferior (sd, prog_bfd, prog_argv, environ);
  sim_create_inferior (sd, prog_bfd, prog_argv, environ);
#else
#else
  sim_create_inferior (sd, prog_bfd, prog_argv, NULL);
  sim_create_inferior (sd, prog_bfd, prog_argv, NULL);
#endif
#endif
 
 
  /* To accommodate relative file paths, chdir to sysroot now.  We
  /* To accommodate relative file paths, chdir to sysroot now.  We
     mustn't do this until BFD has opened the program, else we wouldn't
     mustn't do this until BFD has opened the program, else we wouldn't
     find the executable if it has a relative file path.  */
     find the executable if it has a relative file path.  */
  if (simulator_sysroot[0] != '\0' && chdir (simulator_sysroot) < 0)
  if (simulator_sysroot[0] != '\0' && chdir (simulator_sysroot) < 0)
    {
    {
      fprintf (stderr, "%s: can't change directory to \"%s\"\n",
      fprintf (stderr, "%s: can't change directory to \"%s\"\n",
               myname, simulator_sysroot);
               myname, simulator_sysroot);
      exit (1);
      exit (1);
    }
    }
 
 
  /* Run/Step the program.  */
  /* Run/Step the program.  */
  if (single_step)
  if (single_step)
    {
    {
      do
      do
        {
        {
          prev_sigint = signal (SIGINT, cntrl_c);
          prev_sigint = signal (SIGINT, cntrl_c);
          sim_resume (sd, 1/*step*/, 0);
          sim_resume (sd, 1/*step*/, 0);
          signal (SIGINT, prev_sigint);
          signal (SIGINT, prev_sigint);
          sim_stop_reason (sd, &reason, &sigrc);
          sim_stop_reason (sd, &reason, &sigrc);
 
 
          if ((reason == sim_stopped) &&
          if ((reason == sim_stopped) &&
              (sigrc == sim_signal_to_host (sd, SIM_SIGINT)))
              (sigrc == sim_signal_to_host (sd, SIM_SIGINT)))
            break; /* exit on control-C */
            break; /* exit on control-C */
        }
        }
      /* remain on breakpoint or signals in oe mode*/
      /* remain on breakpoint or signals in oe mode*/
      while (((reason == sim_signalled) &&
      while (((reason == sim_signalled) &&
              (sigrc == sim_signal_to_host (sd, SIM_SIGTRAP))) ||
              (sigrc == sim_signal_to_host (sd, SIM_SIGTRAP))) ||
             ((reason == sim_stopped) &&
             ((reason == sim_stopped) &&
              (STATE_ENVIRONMENT (sd) == OPERATING_ENVIRONMENT)));
              (STATE_ENVIRONMENT (sd) == OPERATING_ENVIRONMENT)));
    }
    }
  else
  else
    {
    {
      do
      do
        {
        {
#if defined (HAVE_SIGACTION) && defined (SA_RESTART)
#if defined (HAVE_SIGACTION) && defined (SA_RESTART)
          struct sigaction sa, osa;
          struct sigaction sa, osa;
          sa.sa_handler = cntrl_c;
          sa.sa_handler = cntrl_c;
          sigemptyset (&sa.sa_mask);
          sigemptyset (&sa.sa_mask);
          sa.sa_flags = 0;
          sa.sa_flags = 0;
          sigaction (SIGINT, &sa, &osa);
          sigaction (SIGINT, &sa, &osa);
          prev_sigint = osa.sa_handler;
          prev_sigint = osa.sa_handler;
#else
#else
          prev_sigint = signal (SIGINT, cntrl_c);
          prev_sigint = signal (SIGINT, cntrl_c);
#endif
#endif
          sim_resume (sd, 0, sigrc);
          sim_resume (sd, 0, sigrc);
          signal (SIGINT, prev_sigint);
          signal (SIGINT, prev_sigint);
          sim_stop_reason (sd, &reason, &sigrc);
          sim_stop_reason (sd, &reason, &sigrc);
 
 
          if ((reason == sim_stopped) &&
          if ((reason == sim_stopped) &&
              (sigrc == sim_signal_to_host (sd, SIM_SIGINT)))
              (sigrc == sim_signal_to_host (sd, SIM_SIGINT)))
            break; /* exit on control-C */
            break; /* exit on control-C */
 
 
          /* remain on signals in oe mode */
          /* remain on signals in oe mode */
        } while ((reason == sim_stopped) &&
        } while ((reason == sim_stopped) &&
                 (STATE_ENVIRONMENT (sd) == OPERATING_ENVIRONMENT));
                 (STATE_ENVIRONMENT (sd) == OPERATING_ENVIRONMENT));
 
 
    }
    }
  /* Print any stats the simulator collected.  */
  /* Print any stats the simulator collected.  */
  if (STATE_VERBOSE_P (sd))
  if (STATE_VERBOSE_P (sd))
    sim_info (sd, 0);
    sim_info (sd, 0);
 
 
  /* Shutdown the simulator.  */
  /* Shutdown the simulator.  */
  sim_close (sd, 0);
  sim_close (sd, 0);
 
 
  /* If reason is sim_exited, then sigrc holds the exit code which we want
  /* If reason is sim_exited, then sigrc holds the exit code which we want
     to return.  If reason is sim_stopped or sim_signalled, then sigrc holds
     to return.  If reason is sim_stopped or sim_signalled, then sigrc holds
     the signal that the simulator received; we want to return that to
     the signal that the simulator received; we want to return that to
     indicate failure.  */
     indicate failure.  */
 
 
  /* Why did we stop? */
  /* Why did we stop? */
  switch (reason)
  switch (reason)
    {
    {
    case sim_signalled:
    case sim_signalled:
    case sim_stopped:
    case sim_stopped:
      if (sigrc != 0)
      if (sigrc != 0)
        fprintf (stderr, "program stopped with signal %d.\n", sigrc);
        fprintf (stderr, "program stopped with signal %d.\n", sigrc);
      break;
      break;
 
 
    case sim_exited:
    case sim_exited:
      break;
      break;
 
 
    default:
    default:
      fprintf (stderr, "program in undefined state (%d:%d)\n", reason, sigrc);
      fprintf (stderr, "program in undefined state (%d:%d)\n", reason, sigrc);
      break;
      break;
 
 
    }
    }
 
 
  return sigrc;
  return sigrc;
}
}
 
 
static void
static void
usage ()
usage ()
{
{
  fprintf (stderr, "Usage: %s [options] program [program args]\n", myname);
  fprintf (stderr, "Usage: %s [options] program [program args]\n", myname);
  fprintf (stderr, "Run `%s --help' for full list of options.\n", myname);
  fprintf (stderr, "Run `%s --help' for full list of options.\n", myname);
  exit (1);
  exit (1);
}
}
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.