]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - sim/common/nrun.c
Automatic date update in version.in
[thirdparty/binutils-gdb.git] / sim / common / nrun.c
CommitLineData
c906108c 1/* New version of run front end support for simulators.
3666a048 2 Copyright (C) 1997-2021 Free Software Foundation, Inc.
c906108c
SS
3
4This program is free software; you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
4744ac1b
JB
6the Free Software Foundation; either version 3 of the License, or
7(at your option) any later version.
c906108c
SS
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
4744ac1b
JB
14You should have received a copy of the GNU General Public License
15along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c 16
f914e384 17/* Need to be before general includes, to pick up e.g. _GNU_SOURCE. */
6df01ab8 18#include "defs.h"
f914e384 19
c906108c 20#include <signal.h>
b760fb3a 21#include <stdlib.h>
2232061b 22/* For strsignal. */
2232061b 23#include <string.h>
2232061b 24
c906108c
SS
25#include "sim-main.h"
26
27#include "bfd.h"
c5a2e012 28#include "environ.h"
c906108c 29
fe348617
MF
30#ifndef HAVE_STRSIGNAL
31/* While libiberty provides a fallback, it doesn't provide a prototype. */
32extern const char *strsignal (int);
33#endif
34
027e2a04
HPN
35#ifdef HAVE_UNISTD_H
36/* For chdir. */
37#include <unistd.h>
38#endif
39
c906108c
SS
40static void usage (void);
41
42extern host_callback default_callback;
43
aba6f46b 44static const char *myname;
c906108c
SS
45
46static SIM_DESC sd;
47
48static RETSIGTYPE
49cntrl_c (int sig)
50{
51 if (! sim_stop (sd))
52 {
53 fprintf (stderr, "Quit!\n");
54 exit (1);
55 }
56}
57
58int
59main (int argc, char **argv)
60{
aba6f46b 61 const char *name;
c906108c 62 char **prog_argv = NULL;
6b4a8935 63 struct bfd *prog_bfd;
c906108c
SS
64 enum sim_stop reason;
65 int sigrc = 0;
66 int single_step = 0;
67 RETSIGTYPE (*prev_sigint) ();
68
aba6f46b 69 myname = lbasename (argv[0]);
c906108c
SS
70
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)
76 {
77 single_step = 1;
78 }
79
80 /* Create an instance of the simulator. */
81 default_callback.init (&default_callback);
82 sd = sim_open (SIM_OPEN_STANDALONE, &default_callback, NULL, argv);
83 if (sd == 0)
84 exit (1);
85 if (STATE_MAGIC (sd) != SIM_MAGIC_NUMBER)
86 {
87 fprintf (stderr, "Internal error - bad magic number in simulator struct\n");
88 abort ();
89 }
90
cfc6061b
MF
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;
f4f8cce4 97
c906108c
SS
98 /* Was there a program to run? */
99 prog_argv = STATE_PROG_ARGV (sd);
100 prog_bfd = STATE_PROG_BFD (sd);
101 if (prog_argv == NULL || *prog_argv == NULL)
102 usage ();
103
104 name = *prog_argv;
105
106 /* For simulators that don't open prog during sim_open() */
107 if (prog_bfd == NULL)
108 {
109 prog_bfd = bfd_openr (name, 0);
110 if (prog_bfd == NULL)
111 {
028f6515 112 fprintf (stderr, "%s: can't open \"%s\": %s\n",
c906108c
SS
113 myname, name, bfd_errmsg (bfd_get_error ()));
114 exit (1);
115 }
028f6515 116 if (!bfd_check_format (prog_bfd, bfd_object))
c906108c
SS
117 {
118 fprintf (stderr, "%s: \"%s\" is not an object file: %s\n",
119 myname, name, bfd_errmsg (bfd_get_error ()));
120 exit (1);
121 }
122 }
123
124 if (STATE_VERBOSE_P (sd))
125 printf ("%s %s\n", myname, name);
126
127 /* Load the program into the simulator. */
128 if (sim_load (sd, name, prog_bfd, 0) == SIM_RC_FAIL)
129 exit (1);
130
131 /* Prepare the program for execution. */
c906108c 132 sim_create_inferior (sd, prog_bfd, prog_argv, environ);
c906108c 133
027e2a04
HPN
134 /* To accommodate relative file paths, chdir to sysroot now. We
135 mustn't do this until BFD has opened the program, else we wouldn't
136 find the executable if it has a relative file path. */
137 if (simulator_sysroot[0] != '\0' && chdir (simulator_sysroot) < 0)
138 {
139 fprintf (stderr, "%s: can't change directory to \"%s\"\n",
140 myname, simulator_sysroot);
141 exit (1);
142 }
143
c906108c
SS
144 /* Run/Step the program. */
145 if (single_step)
146 {
147 do
148 {
149 prev_sigint = signal (SIGINT, cntrl_c);
150 sim_resume (sd, 1/*step*/, 0);
151 signal (SIGINT, prev_sigint);
152 sim_stop_reason (sd, &reason, &sigrc);
153
154 if ((reason == sim_stopped) &&
155 (sigrc == sim_signal_to_host (sd, SIM_SIGINT)))
156 break; /* exit on control-C */
157 }
158 /* remain on breakpoint or signals in oe mode*/
159 while (((reason == sim_signalled) &&
160 (sigrc == sim_signal_to_host (sd, SIM_SIGTRAP))) ||
028f6515 161 ((reason == sim_stopped) &&
c906108c
SS
162 (STATE_ENVIRONMENT (sd) == OPERATING_ENVIRONMENT)));
163 }
028f6515 164 else
c906108c 165 {
43e526b9
JM
166 do
167 {
c906108c 168#if defined (HAVE_SIGACTION) && defined (SA_RESTART)
43e526b9
JM
169 struct sigaction sa, osa;
170 sa.sa_handler = cntrl_c;
171 sigemptyset (&sa.sa_mask);
172 sa.sa_flags = 0;
173 sigaction (SIGINT, &sa, &osa);
174 prev_sigint = osa.sa_handler;
c906108c 175#else
43e526b9 176 prev_sigint = signal (SIGINT, cntrl_c);
c906108c 177#endif
43e526b9
JM
178 sim_resume (sd, 0, sigrc);
179 signal (SIGINT, prev_sigint);
180 sim_stop_reason (sd, &reason, &sigrc);
028f6515 181
43e526b9
JM
182 if ((reason == sim_stopped) &&
183 (sigrc == sim_signal_to_host (sd, SIM_SIGINT)))
184 break; /* exit on control-C */
028f6515 185
43e526b9
JM
186 /* remain on signals in oe mode */
187 } while ((reason == sim_stopped) &&
188 (STATE_ENVIRONMENT (sd) == OPERATING_ENVIRONMENT));
028f6515 189
43e526b9 190 }
c906108c 191 /* Print any stats the simulator collected. */
f9cbceb6
AC
192 if (STATE_VERBOSE_P (sd))
193 sim_info (sd, 0);
028f6515 194
c906108c
SS
195 /* Shutdown the simulator. */
196 sim_close (sd, 0);
028f6515 197
c906108c
SS
198 /* If reason is sim_exited, then sigrc holds the exit code which we want
199 to return. If reason is sim_stopped or sim_signalled, then sigrc holds
200 the signal that the simulator received; we want to return that to
201 indicate failure. */
028f6515 202
c906108c
SS
203 /* Why did we stop? */
204 switch (reason)
205 {
206 case sim_signalled:
207 case sim_stopped:
208 if (sigrc != 0)
9a5e0c49
MF
209 fprintf (stderr, "program stopped with signal %d (%s).\n", sigrc,
210 strsignal (sigrc));
c906108c
SS
211 break;
212
213 case sim_exited:
214 break;
215
216 default:
217 fprintf (stderr, "program in undefined state (%d:%d)\n", reason, sigrc);
218 break;
219
220 }
c906108c
SS
221
222 return sigrc;
223}
224
225static void
dc416615 226usage (void)
c906108c
SS
227{
228 fprintf (stderr, "Usage: %s [options] program [program args]\n", myname);
229 fprintf (stderr, "Run `%s --help' for full list of options.\n", myname);
230 exit (1);
231}