1 /* New version of run front end support for simulators.
2 Copyright (C) 1997-2024 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. */
31 #include "sim-signal.h"
32 #include "sim/callback.h"
34 #ifndef HAVE_STRSIGNAL
35 /* While libiberty provides a fallback, it doesn't provide a prototype. */
36 extern const char *strsignal (int);
39 static void usage (void);
41 extern host_callback default_callback
;
43 static const char *myname
;
52 fprintf (stderr
, "Quit!\n");
58 main (int argc
, char **argv
)
61 char **prog_argv
= NULL
;
62 char **prog_envp
= NULL
;
67 RETSIGTYPE (*prev_sigint
) ();
69 myname
= lbasename (argv
[0]);
71 /* INTERNAL: When MYNAME is `step', single step the simulator
72 instead of allowing it to run free. The sole purpose of this
73 HACK is to allow the sim_resume interface's step argument to be
74 tested without having to build/run gdb. */
75 if (strlen (myname
) > 4 && strcmp (myname
- 4, "step") == 0)
80 /* Create an instance of the simulator. */
81 default_callback
.init (&default_callback
);
82 sd
= sim_open (SIM_OPEN_STANDALONE
, &default_callback
, NULL
, argv
);
85 if (STATE_MAGIC (sd
) != SIM_MAGIC_NUMBER
)
87 fprintf (stderr
, "Internal error - bad magic number in simulator struct\n");
91 /* We can't set the endianness in the callback structure until sim_config is
92 called, which happens in sim_open. If it's still the default, switch it.
93 Don't use CURRENT_TARGET_BYTE_ORDER as all its internal processing already
94 happened in sim_config. */
95 if (default_callback
.target_endian
== BFD_ENDIAN_UNKNOWN
)
96 default_callback
.target_endian
= current_target_byte_order
;
98 /* Was there a program to run? */
99 prog_argv
= STATE_PROG_ARGV (sd
);
100 prog_envp
= STATE_PROG_ENVP (sd
) ? : environ
;
101 prog_bfd
= STATE_PROG_BFD (sd
);
102 if (prog_argv
== NULL
|| *prog_argv
== NULL
)
105 name
= STATE_PROG_FILE (sd
);
107 /* For simulators that don't open prog during sim_open() */
108 if (prog_bfd
== NULL
)
110 prog_bfd
= bfd_openr (name
, 0);
111 if (prog_bfd
== NULL
)
113 fprintf (stderr
, "%s: can't open \"%s\": %s\n",
114 myname
, name
, bfd_errmsg (bfd_get_error ()));
117 if (!bfd_check_format (prog_bfd
, bfd_object
))
119 fprintf (stderr
, "%s: \"%s\" is not an object file: %s\n",
120 myname
, name
, bfd_errmsg (bfd_get_error ()));
125 if (STATE_VERBOSE_P (sd
))
126 printf ("%s %s\n", myname
, name
);
128 /* Load the program into the simulator. */
129 if (sim_load (sd
, name
, prog_bfd
, 0) == SIM_RC_FAIL
)
132 /* Prepare the program for execution. */
133 sim_create_inferior (sd
, prog_bfd
, prog_argv
, prog_envp
);
135 /* To accommodate relative file paths, chdir to sysroot now. We
136 mustn't do this until BFD has opened the program, else we wouldn't
137 find the executable if it has a relative file path. */
138 if (simulator_sysroot
[0] != '\0' && chdir (simulator_sysroot
) < 0)
140 fprintf (stderr
, "%s: can't change directory to \"%s\"\n",
141 myname
, simulator_sysroot
);
145 /* Run/Step the program. */
150 prev_sigint
= signal (SIGINT
, cntrl_c
);
151 sim_resume (sd
, 1/*step*/, 0);
152 signal (SIGINT
, prev_sigint
);
153 sim_stop_reason (sd
, &reason
, &sigrc
);
155 if ((reason
== sim_stopped
) &&
156 (sigrc
== sim_signal_to_host (sd
, SIM_SIGINT
)))
157 break; /* exit on control-C */
159 /* remain on breakpoint or signals in oe mode*/
160 while (((reason
== sim_signalled
) &&
161 (sigrc
== sim_signal_to_host (sd
, SIM_SIGTRAP
))) ||
162 ((reason
== sim_stopped
) &&
163 (STATE_ENVIRONMENT (sd
) == OPERATING_ENVIRONMENT
)));
169 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
170 struct sigaction sa
, osa
;
171 sa
.sa_handler
= cntrl_c
;
172 sigemptyset (&sa
.sa_mask
);
174 sigaction (SIGINT
, &sa
, &osa
);
175 prev_sigint
= osa
.sa_handler
;
177 prev_sigint
= signal (SIGINT
, cntrl_c
);
179 sim_resume (sd
, 0, sigrc
);
180 signal (SIGINT
, prev_sigint
);
181 sim_stop_reason (sd
, &reason
, &sigrc
);
183 if ((reason
== sim_stopped
) &&
184 (sigrc
== sim_signal_to_host (sd
, SIM_SIGINT
)))
185 break; /* exit on control-C */
187 /* remain on signals in oe mode */
188 } while ((reason
== sim_stopped
) &&
189 (STATE_ENVIRONMENT (sd
) == OPERATING_ENVIRONMENT
));
192 /* Print any stats the simulator collected. */
193 if (STATE_VERBOSE_P (sd
))
196 /* Shutdown the simulator. */
199 /* If reason is sim_exited, then sigrc holds the exit code which we want
200 to return. If reason is sim_stopped or sim_signalled, then sigrc holds
201 the signal that the simulator received; we want to return that to
204 /* Why did we stop? */
210 fprintf (stderr
, "program stopped with signal %d (%s).\n", sigrc
,
218 fprintf (stderr
, "program in undefined state (%d:%d)\n", reason
, sigrc
);
229 fprintf (stderr
, "Usage: %s [options] [VAR=VAL|--] program [program args]\n",
231 fprintf (stderr
, "Run `%s --help' for full list of options.\n", myname
);