1 /* New version of run front end support for simulators.
2 Copyright (C) 1997-2015 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 3 of the License, or
7 (at your option) any later version.
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
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
17 /* Need to be before general includes, to pick up e.g. _GNU_SOURCE. */
38 extern char **environ
;
46 static void usage (void);
48 extern host_callback default_callback
;
59 fprintf (stderr
, "Quit!\n");
65 main (int argc
, char **argv
)
68 char **prog_argv
= NULL
;
73 RETSIGTYPE (*prev_sigint
) ();
75 myname
= argv
[0] + strlen (argv
[0]);
76 while (myname
> argv
[0] && myname
[-1] != '/')
79 /* INTERNAL: When MYNAME is `step', single step the simulator
80 instead of allowing it to run free. The sole purpose of this
81 HACK is to allow the sim_resume interface's step argument to be
82 tested without having to build/run gdb. */
83 if (strlen (myname
) > 4 && strcmp (myname
- 4, "step") == 0)
88 /* Create an instance of the simulator. */
89 default_callback
.init (&default_callback
);
90 sd
= sim_open (SIM_OPEN_STANDALONE
, &default_callback
, NULL
, argv
);
93 if (STATE_MAGIC (sd
) != SIM_MAGIC_NUMBER
)
95 fprintf (stderr
, "Internal error - bad magic number in simulator struct\n");
99 /* We can't set the endianness in the callback structure until
100 sim_config is called, which happens in sim_open. */
101 default_callback
.target_endian
102 = (CURRENT_TARGET_BYTE_ORDER
== BIG_ENDIAN
103 ? BFD_ENDIAN_BIG
: BFD_ENDIAN_LITTLE
);
105 /* Was there a program to run? */
106 prog_argv
= STATE_PROG_ARGV (sd
);
107 prog_bfd
= STATE_PROG_BFD (sd
);
108 if (prog_argv
== NULL
|| *prog_argv
== NULL
)
113 /* For simulators that don't open prog during sim_open() */
114 if (prog_bfd
== NULL
)
116 prog_bfd
= bfd_openr (name
, 0);
117 if (prog_bfd
== NULL
)
119 fprintf (stderr
, "%s: can't open \"%s\": %s\n",
120 myname
, name
, bfd_errmsg (bfd_get_error ()));
123 if (!bfd_check_format (prog_bfd
, bfd_object
))
125 fprintf (stderr
, "%s: \"%s\" is not an object file: %s\n",
126 myname
, name
, bfd_errmsg (bfd_get_error ()));
131 if (STATE_VERBOSE_P (sd
))
132 printf ("%s %s\n", myname
, name
);
134 /* Load the program into the simulator. */
135 if (sim_load (sd
, name
, prog_bfd
, 0) == SIM_RC_FAIL
)
138 /* Prepare the program for execution. */
140 sim_create_inferior (sd
, prog_bfd
, prog_argv
, environ
);
142 sim_create_inferior (sd
, prog_bfd
, prog_argv
, NULL
);
145 /* To accommodate relative file paths, chdir to sysroot now. We
146 mustn't do this until BFD has opened the program, else we wouldn't
147 find the executable if it has a relative file path. */
148 if (simulator_sysroot
[0] != '\0' && chdir (simulator_sysroot
) < 0)
150 fprintf (stderr
, "%s: can't change directory to \"%s\"\n",
151 myname
, simulator_sysroot
);
155 /* Run/Step the program. */
160 prev_sigint
= signal (SIGINT
, cntrl_c
);
161 sim_resume (sd
, 1/*step*/, 0);
162 signal (SIGINT
, prev_sigint
);
163 sim_stop_reason (sd
, &reason
, &sigrc
);
165 if ((reason
== sim_stopped
) &&
166 (sigrc
== sim_signal_to_host (sd
, SIM_SIGINT
)))
167 break; /* exit on control-C */
169 /* remain on breakpoint or signals in oe mode*/
170 while (((reason
== sim_signalled
) &&
171 (sigrc
== sim_signal_to_host (sd
, SIM_SIGTRAP
))) ||
172 ((reason
== sim_stopped
) &&
173 (STATE_ENVIRONMENT (sd
) == OPERATING_ENVIRONMENT
)));
179 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
180 struct sigaction sa
, osa
;
181 sa
.sa_handler
= cntrl_c
;
182 sigemptyset (&sa
.sa_mask
);
184 sigaction (SIGINT
, &sa
, &osa
);
185 prev_sigint
= osa
.sa_handler
;
187 prev_sigint
= signal (SIGINT
, cntrl_c
);
189 sim_resume (sd
, 0, sigrc
);
190 signal (SIGINT
, prev_sigint
);
191 sim_stop_reason (sd
, &reason
, &sigrc
);
193 if ((reason
== sim_stopped
) &&
194 (sigrc
== sim_signal_to_host (sd
, SIM_SIGINT
)))
195 break; /* exit on control-C */
197 /* remain on signals in oe mode */
198 } while ((reason
== sim_stopped
) &&
199 (STATE_ENVIRONMENT (sd
) == OPERATING_ENVIRONMENT
));
202 /* Print any stats the simulator collected. */
203 if (STATE_VERBOSE_P (sd
))
206 /* Shutdown the simulator. */
209 /* If reason is sim_exited, then sigrc holds the exit code which we want
210 to return. If reason is sim_stopped or sim_signalled, then sigrc holds
211 the signal that the simulator received; we want to return that to
214 /* Why did we stop? */
220 fprintf (stderr
, "program stopped with signal %d (%s).\n", sigrc
,
228 fprintf (stderr
, "program in undefined state (%d:%d)\n", reason
, sigrc
);
239 fprintf (stderr
, "Usage: %s [options] program [program args]\n", myname
);
240 fprintf (stderr
, "Run `%s --help' for full list of options.\n", myname
);