1999-04-16 01:35:26 +00:00
|
|
|
/* New version of run front end support for simulators.
|
2016-01-01 04:33:14 +00:00
|
|
|
Copyright (C) 1997-2016 Free Software Foundation, Inc.
|
1999-04-16 01:35:26 +00:00
|
|
|
|
|
|
|
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
|
2007-08-24 14:30:15 +00:00
|
|
|
the Free Software Foundation; either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
1999-04-16 01:35:26 +00:00
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
2007-08-24 14:30:15 +00:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
1999-04-16 01:35:26 +00:00
|
|
|
|
2012-03-24 09:31:09 +00:00
|
|
|
/* Need to be before general includes, to pick up e.g. _GNU_SOURCE. */
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "cconfig.h"
|
|
|
|
#endif
|
|
|
|
|
1999-04-16 01:35:26 +00:00
|
|
|
#include <signal.h>
|
2012-03-24 05:38:43 +00:00
|
|
|
|
|
|
|
/* For strsignal. */
|
|
|
|
#ifdef HAVE_STRING_H
|
|
|
|
#include <string.h>
|
|
|
|
#else
|
|
|
|
#ifdef HAVE_STRINGS_H
|
|
|
|
#include <strings.h>
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
1999-04-16 01:35:26 +00:00
|
|
|
#include "sim-main.h"
|
|
|
|
|
|
|
|
#include "bfd.h"
|
|
|
|
|
|
|
|
#ifdef HAVE_ENVIRON
|
|
|
|
extern char **environ;
|
|
|
|
#endif
|
|
|
|
|
2004-12-08 00:40:30 +00:00
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
/* For chdir. */
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
1999-04-16 01:35:26 +00:00
|
|
|
static void usage (void);
|
|
|
|
|
|
|
|
extern host_callback default_callback;
|
|
|
|
|
2016-01-03 06:44:44 +00:00
|
|
|
static const char *myname;
|
1999-04-16 01:35:26 +00:00
|
|
|
|
|
|
|
static SIM_DESC sd;
|
|
|
|
|
|
|
|
static RETSIGTYPE
|
|
|
|
cntrl_c (int sig)
|
|
|
|
{
|
|
|
|
if (! sim_stop (sd))
|
|
|
|
{
|
|
|
|
fprintf (stderr, "Quit!\n");
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main (int argc, char **argv)
|
|
|
|
{
|
2016-01-03 06:44:44 +00:00
|
|
|
const char *name;
|
1999-04-16 01:35:26 +00:00
|
|
|
char **prog_argv = NULL;
|
Index: arm/ChangeLog
2003-02-27 Andrew Cagney <cagney@redhat.com>
* wrapper.c (sim_create_inferior, sim_open): Rename _bfd to bfd.
Index: common/ChangeLog
2003-02-27 Andrew Cagney <cagney@redhat.com>
* sim-utils.h (sim_analyze_program, sim_load_file): Rename _bfd to bfd.
* sim-hload.c (sim_load), sim-base.h (sim_state_base): Ditto.
* nrun.c (main): Ditto.
Index: d10v/ChangeLog
2003-02-27 Andrew Cagney <cagney@redhat.com>
* interp.c (sim_open, sim_create_inferior): Rename _bfd to bfd.
Index: erc32/ChangeLog
2003-02-27 Andrew Cagney <cagney@redhat.com>
* interf.c (sim_open, sim_create_inferior): Rename _bfd to bfd.
Index: h8300/ChangeLog
2003-02-27 Andrew Cagney <cagney@redhat.com>
* compile.c (sim_open, sim_create_inferior): Rename _bfd to bfd.
Index: h8500/ChangeLog
2003-02-27 Andrew Cagney <cagney@redhat.com>
* compile.c (sim_open, sim_create_inferior): Rename _bfd to bfd.
Index: i960/ChangeLog
2003-02-27 Andrew Cagney <cagney@redhat.com>
* sim-if.c (sim_open, sim_create_inferior): Rename _bfd to bfd.
Index: m32r/ChangeLog
2003-02-27 Andrew Cagney <cagney@redhat.com>
* sim-if.c (sim_open, sim_create_inferior): Rename _bfd to bfd.
Index: m68hc11/ChangeLog
2003-02-27 Andrew Cagney <cagney@redhat.com>
* interp.c (sim_prepare_for_program, sim_open)
(sim_create_inferior): Rename _bfd to bfd.
Index: mcore/ChangeLog
2003-02-27 Andrew Cagney <cagney@redhat.com>
* interp.c (sim_open, sim_create_inferior): Rename _bfd to bfd.
Index: mips/ChangeLog
2003-02-27 Andrew Cagney <cagney@redhat.com>
* interp.c (sim_open):
(sim_create_inferior):
Index: mn10200/ChangeLog
2003-02-27 Andrew Cagney <cagney@redhat.com>
* interp.c (sim_open, sim_create_inferior): Rename _bfd to bfd.
Index: mn10300/ChangeLog
2003-02-27 Andrew Cagney <cagney@redhat.com>
* interp.c (sim_open, sim_create_inferior, sim_open)
(sim_create_inferior): Rename _bfd to bfd.
Index: ppc/ChangeLog
2003-02-27 Andrew Cagney <cagney@redhat.com>
* sim_calls.c (sim_open, sim_create_inferior): Rename _bfd to bfd.
Index: sh/ChangeLog
2003-02-27 Andrew Cagney <cagney@redhat.com>
* interp.c (init_dsp, sim_open, sim_create_inferior): Rename _bfd
to bfd.
Index: v850/ChangeLog
2003-02-27 Andrew Cagney <cagney@redhat.com>
* interp.c (sim_open, sim_create_inferior): Rename _bfd to bfd.
Index: z8k/ChangeLog
2003-02-27 Andrew Cagney <cagney@redhat.com>
* iface.c (sim_open, sim_create_inferior): Rename _bfd to bfd.
2003-02-27 23:26:34 +00:00
|
|
|
struct bfd *prog_bfd;
|
1999-04-16 01:35:26 +00:00
|
|
|
enum sim_stop reason;
|
|
|
|
int sigrc = 0;
|
|
|
|
int single_step = 0;
|
|
|
|
RETSIGTYPE (*prev_sigint) ();
|
|
|
|
|
2016-01-03 06:44:44 +00:00
|
|
|
myname = lbasename (argv[0]);
|
1999-04-16 01:35:26 +00:00
|
|
|
|
|
|
|
/* INTERNAL: When MYNAME is `step', single step the simulator
|
|
|
|
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
|
|
|
|
tested without having to build/run gdb. */
|
|
|
|
if (strlen (myname) > 4 && strcmp (myname - 4, "step") == 0)
|
|
|
|
{
|
|
|
|
single_step = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create an instance of the simulator. */
|
|
|
|
default_callback.init (&default_callback);
|
|
|
|
sd = sim_open (SIM_OPEN_STANDALONE, &default_callback, NULL, argv);
|
|
|
|
if (sd == 0)
|
|
|
|
exit (1);
|
|
|
|
if (STATE_MAGIC (sd) != SIM_MAGIC_NUMBER)
|
|
|
|
{
|
|
|
|
fprintf (stderr, "Internal error - bad magic number in simulator struct\n");
|
|
|
|
abort ();
|
|
|
|
}
|
|
|
|
|
2005-01-28 03:28:40 +00:00
|
|
|
/* We can't set the endianness in the callback structure until
|
|
|
|
sim_config is called, which happens in sim_open. */
|
|
|
|
default_callback.target_endian
|
2016-01-02 22:46:16 +00:00
|
|
|
= (CURRENT_TARGET_BYTE_ORDER == BFD_ENDIAN_BIG
|
2005-01-28 03:28:40 +00:00
|
|
|
? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
|
|
|
|
|
1999-04-16 01:35:26 +00:00
|
|
|
/* Was there a program to run? */
|
|
|
|
prog_argv = STATE_PROG_ARGV (sd);
|
|
|
|
prog_bfd = STATE_PROG_BFD (sd);
|
|
|
|
if (prog_argv == NULL || *prog_argv == NULL)
|
|
|
|
usage ();
|
|
|
|
|
|
|
|
name = *prog_argv;
|
|
|
|
|
|
|
|
/* For simulators that don't open prog during sim_open() */
|
|
|
|
if (prog_bfd == NULL)
|
|
|
|
{
|
|
|
|
prog_bfd = bfd_openr (name, 0);
|
|
|
|
if (prog_bfd == NULL)
|
|
|
|
{
|
2011-03-15 03:16:17 +00:00
|
|
|
fprintf (stderr, "%s: can't open \"%s\": %s\n",
|
1999-04-16 01:35:26 +00:00
|
|
|
myname, name, bfd_errmsg (bfd_get_error ()));
|
|
|
|
exit (1);
|
|
|
|
}
|
2011-03-15 03:16:17 +00:00
|
|
|
if (!bfd_check_format (prog_bfd, bfd_object))
|
1999-04-16 01:35:26 +00:00
|
|
|
{
|
|
|
|
fprintf (stderr, "%s: \"%s\" is not an object file: %s\n",
|
|
|
|
myname, name, bfd_errmsg (bfd_get_error ()));
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (STATE_VERBOSE_P (sd))
|
|
|
|
printf ("%s %s\n", myname, name);
|
|
|
|
|
|
|
|
/* Load the program into the simulator. */
|
|
|
|
if (sim_load (sd, name, prog_bfd, 0) == SIM_RC_FAIL)
|
|
|
|
exit (1);
|
|
|
|
|
|
|
|
/* Prepare the program for execution. */
|
|
|
|
#ifdef HAVE_ENVIRON
|
|
|
|
sim_create_inferior (sd, prog_bfd, prog_argv, environ);
|
|
|
|
#else
|
|
|
|
sim_create_inferior (sd, prog_bfd, prog_argv, NULL);
|
|
|
|
#endif
|
|
|
|
|
2004-12-08 00:40:30 +00:00
|
|
|
/* To accommodate relative file paths, chdir to sysroot now. We
|
|
|
|
mustn't do this until BFD has opened the program, else we wouldn't
|
|
|
|
find the executable if it has a relative file path. */
|
|
|
|
if (simulator_sysroot[0] != '\0' && chdir (simulator_sysroot) < 0)
|
|
|
|
{
|
|
|
|
fprintf (stderr, "%s: can't change directory to \"%s\"\n",
|
|
|
|
myname, simulator_sysroot);
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
|
1999-04-16 01:35:26 +00:00
|
|
|
/* Run/Step the program. */
|
|
|
|
if (single_step)
|
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
|
|
|
prev_sigint = signal (SIGINT, cntrl_c);
|
|
|
|
sim_resume (sd, 1/*step*/, 0);
|
|
|
|
signal (SIGINT, prev_sigint);
|
|
|
|
sim_stop_reason (sd, &reason, &sigrc);
|
|
|
|
|
|
|
|
if ((reason == sim_stopped) &&
|
|
|
|
(sigrc == sim_signal_to_host (sd, SIM_SIGINT)))
|
|
|
|
break; /* exit on control-C */
|
|
|
|
}
|
|
|
|
/* remain on breakpoint or signals in oe mode*/
|
|
|
|
while (((reason == sim_signalled) &&
|
|
|
|
(sigrc == sim_signal_to_host (sd, SIM_SIGTRAP))) ||
|
2011-03-15 03:16:17 +00:00
|
|
|
((reason == sim_stopped) &&
|
1999-04-16 01:35:26 +00:00
|
|
|
(STATE_ENVIRONMENT (sd) == OPERATING_ENVIRONMENT)));
|
|
|
|
}
|
2011-03-15 03:16:17 +00:00
|
|
|
else
|
1999-04-16 01:35:26 +00:00
|
|
|
{
|
1999-07-12 11:15:22 +00:00
|
|
|
do
|
|
|
|
{
|
1999-04-16 01:35:26 +00:00
|
|
|
#if defined (HAVE_SIGACTION) && defined (SA_RESTART)
|
1999-07-12 11:15:22 +00:00
|
|
|
struct sigaction sa, osa;
|
|
|
|
sa.sa_handler = cntrl_c;
|
|
|
|
sigemptyset (&sa.sa_mask);
|
|
|
|
sa.sa_flags = 0;
|
|
|
|
sigaction (SIGINT, &sa, &osa);
|
|
|
|
prev_sigint = osa.sa_handler;
|
1999-04-16 01:35:26 +00:00
|
|
|
#else
|
1999-07-12 11:15:22 +00:00
|
|
|
prev_sigint = signal (SIGINT, cntrl_c);
|
1999-04-16 01:35:26 +00:00
|
|
|
#endif
|
1999-07-12 11:15:22 +00:00
|
|
|
sim_resume (sd, 0, sigrc);
|
|
|
|
signal (SIGINT, prev_sigint);
|
|
|
|
sim_stop_reason (sd, &reason, &sigrc);
|
2011-03-15 03:16:17 +00:00
|
|
|
|
1999-07-12 11:15:22 +00:00
|
|
|
if ((reason == sim_stopped) &&
|
|
|
|
(sigrc == sim_signal_to_host (sd, SIM_SIGINT)))
|
|
|
|
break; /* exit on control-C */
|
2011-03-15 03:16:17 +00:00
|
|
|
|
1999-07-12 11:15:22 +00:00
|
|
|
/* remain on signals in oe mode */
|
|
|
|
} while ((reason == sim_stopped) &&
|
|
|
|
(STATE_ENVIRONMENT (sd) == OPERATING_ENVIRONMENT));
|
2011-03-15 03:16:17 +00:00
|
|
|
|
1999-07-12 11:15:22 +00:00
|
|
|
}
|
1999-04-16 01:35:26 +00:00
|
|
|
/* Print any stats the simulator collected. */
|
2000-07-27 11:49:07 +00:00
|
|
|
if (STATE_VERBOSE_P (sd))
|
|
|
|
sim_info (sd, 0);
|
2011-03-15 03:16:17 +00:00
|
|
|
|
1999-04-16 01:35:26 +00:00
|
|
|
/* Shutdown the simulator. */
|
|
|
|
sim_close (sd, 0);
|
2011-03-15 03:16:17 +00:00
|
|
|
|
1999-04-16 01:35:26 +00:00
|
|
|
/* 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
|
|
|
|
the signal that the simulator received; we want to return that to
|
|
|
|
indicate failure. */
|
2011-03-15 03:16:17 +00:00
|
|
|
|
1999-04-16 01:35:26 +00:00
|
|
|
/* Why did we stop? */
|
|
|
|
switch (reason)
|
|
|
|
{
|
|
|
|
case sim_signalled:
|
|
|
|
case sim_stopped:
|
|
|
|
if (sigrc != 0)
|
2012-03-19 03:58:36 +00:00
|
|
|
fprintf (stderr, "program stopped with signal %d (%s).\n", sigrc,
|
|
|
|
strsignal (sigrc));
|
1999-04-16 01:35:26 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case sim_exited:
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
fprintf (stderr, "program in undefined state (%d:%d)\n", reason, sigrc);
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return sigrc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2010-03-30 19:43:42 +00:00
|
|
|
usage (void)
|
1999-04-16 01:35:26 +00:00
|
|
|
{
|
|
|
|
fprintf (stderr, "Usage: %s [options] program [program args]\n", myname);
|
|
|
|
fprintf (stderr, "Run `%s --help' for full list of options.\n", myname);
|
|
|
|
exit (1);
|
|
|
|
}
|