1 /* New version of run front end support for simulators.
2 Copyright (C) 1997 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License along
15 with this program; if not, write to the Free Software Foundation, Inc.,
16 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
24 extern char **environ
;
27 static void usage (void);
29 extern host_callback default_callback
;
40 fprintf (stderr
, "Quit!\n");
46 main (int argc
, char **argv
)
49 char **prog_argv
= NULL
;
50 struct _bfd
*prog_bfd
;
54 RETSIGTYPE (*prev_sigint
) ();
56 myname
= argv
[0] + strlen (argv
[0]);
57 while (myname
> argv
[0] && myname
[-1] != '/')
60 /* INTERNAL: When MYNAME is `step', single step the simulator
61 instead of allowing it to run free. The sole purpose of this
62 HACK is to allow the sim_resume interface's step argument to be
63 tested without having to build/run gdb. */
64 if (strlen (myname
) > 4 && strcmp (myname
- 4, "step") == 0)
69 /* Create an instance of the simulator. */
70 default_callback
.init (&default_callback
);
71 sd
= sim_open (SIM_OPEN_STANDALONE
, &default_callback
, NULL
, argv
);
74 if (STATE_MAGIC (sd
) != SIM_MAGIC_NUMBER
)
76 fprintf (stderr
, "Internal error - bad magic number in simulator struct\n");
80 /* Was there a program to run? */
81 prog_argv
= STATE_PROG_ARGV (sd
);
82 prog_bfd
= STATE_PROG_BFD (sd
);
83 if (prog_argv
== NULL
|| *prog_argv
== NULL
)
88 /* For simulators that don't open prog during sim_open() */
91 prog_bfd
= bfd_openr (name
, 0);
94 fprintf (stderr
, "%s: can't open \"%s\": %s\n",
95 myname
, name
, bfd_errmsg (bfd_get_error ()));
98 if (!bfd_check_format (prog_bfd
, bfd_object
))
100 fprintf (stderr
, "%s: \"%s\" is not an object file: %s\n",
101 myname
, name
, bfd_errmsg (bfd_get_error ()));
106 if (STATE_VERBOSE_P (sd
))
107 printf ("%s %s\n", myname
, name
);
109 /* Load the program into the simulator. */
110 if (sim_load (sd
, name
, prog_bfd
, 0) == SIM_RC_FAIL
)
113 /* Prepare the program for execution. */
115 sim_create_inferior (sd
, prog_bfd
, prog_argv
, environ
);
117 sim_create_inferior (sd
, prog_bfd
, prog_argv
, NULL
);
120 /* Run/Step the program. */
125 prev_sigint
= signal (SIGINT
, cntrl_c
);
126 sim_resume (sd
, 1/*step*/, 0);
127 signal (SIGINT
, prev_sigint
);
128 sim_stop_reason (sd
, &reason
, &sigrc
);
130 if ((reason
== sim_stopped
) &&
131 (sigrc
== sim_signal_to_host (sd
, SIM_SIGINT
)))
132 break; /* exit on control-C */
134 /* remain on breakpoint or signals in oe mode*/
135 while (((reason
== sim_signalled
) &&
136 (sigrc
== sim_signal_to_host (sd
, SIM_SIGTRAP
))) ||
137 ((reason
== sim_stopped
) &&
138 (STATE_ENVIRONMENT (sd
) == OPERATING_ENVIRONMENT
)));
144 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
145 struct sigaction sa
, osa
;
146 sa
.sa_handler
= cntrl_c
;
147 sigemptyset (&sa
.sa_mask
);
149 sigaction (SIGINT
, &sa
, &osa
);
150 prev_sigint
= osa
.sa_handler
;
152 prev_sigint
= signal (SIGINT
, cntrl_c
);
154 sim_resume (sd
, 0, sigrc
);
155 signal (SIGINT
, prev_sigint
);
156 sim_stop_reason (sd
, &reason
, &sigrc
);
158 if ((reason
== sim_stopped
) &&
159 (sigrc
== sim_signal_to_host (sd
, SIM_SIGINT
)))
160 break; /* exit on control-C */
162 /* remain on signals in oe mode */
163 } while ((reason
== sim_stopped
) &&
164 (STATE_ENVIRONMENT (sd
) == OPERATING_ENVIRONMENT
));
167 /* Print any stats the simulator collected. */
168 if (STATE_VERBOSE_P (sd
))
171 /* Shutdown the simulator. */
174 /* If reason is sim_exited, then sigrc holds the exit code which we want
175 to return. If reason is sim_stopped or sim_signalled, then sigrc holds
176 the signal that the simulator received; we want to return that to
179 #ifdef SIM_H8300 /* FIXME: Ugh. grep for SLEEP in compile.c */
184 /* Why did we stop? */
190 fprintf (stderr
, "program stopped with signal %d.\n", sigrc
);
197 fprintf (stderr
, "program in undefined state (%d:%d)\n", reason
, sigrc
);
209 fprintf (stderr
, "Usage: %s [options] program [program args]\n", myname
);
210 fprintf (stderr
, "Run `%s --help' for full list of options.\n", myname
);