sim: switch config.h usage to defs.h
[deliverable/binutils-gdb.git] / sim / common / nrun.c
1 /* New version of run front end support for simulators.
2 Copyright (C) 1997-2021 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>. */
16
17 /* Need to be before general includes, to pick up e.g. _GNU_SOURCE. */
18 #include "defs.h"
19
20 #include <signal.h>
21 #include <stdlib.h>
22 /* For strsignal. */
23 #include <string.h>
24
25 #include "sim-main.h"
26
27 #include "bfd.h"
28 #include "environ.h"
29
30 #ifndef HAVE_STRSIGNAL
31 /* While libiberty provides a fallback, it doesn't provide a prototype. */
32 extern const char *strsignal (int);
33 #endif
34
35 #ifdef HAVE_UNISTD_H
36 /* For chdir. */
37 #include <unistd.h>
38 #endif
39
40 static void usage (void);
41
42 extern host_callback default_callback;
43
44 static const char *myname;
45
46 static SIM_DESC sd;
47
48 static RETSIGTYPE
49 cntrl_c (int sig)
50 {
51 if (! sim_stop (sd))
52 {
53 fprintf (stderr, "Quit!\n");
54 exit (1);
55 }
56 }
57
58 int
59 main (int argc, char **argv)
60 {
61 const char *name;
62 char **prog_argv = NULL;
63 struct bfd *prog_bfd;
64 enum sim_stop reason;
65 int sigrc = 0;
66 int single_step = 0;
67 RETSIGTYPE (*prev_sigint) ();
68
69 myname = lbasename (argv[0]);
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
91 /* We can't set the endianness in the callback structure until
92 sim_config is called, which happens in sim_open. */
93 default_callback.target_endian
94 = (CURRENT_TARGET_BYTE_ORDER == BFD_ENDIAN_BIG
95 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
96
97 /* Was there a program to run? */
98 prog_argv = STATE_PROG_ARGV (sd);
99 prog_bfd = STATE_PROG_BFD (sd);
100 if (prog_argv == NULL || *prog_argv == NULL)
101 usage ();
102
103 name = *prog_argv;
104
105 /* For simulators that don't open prog during sim_open() */
106 if (prog_bfd == NULL)
107 {
108 prog_bfd = bfd_openr (name, 0);
109 if (prog_bfd == NULL)
110 {
111 fprintf (stderr, "%s: can't open \"%s\": %s\n",
112 myname, name, bfd_errmsg (bfd_get_error ()));
113 exit (1);
114 }
115 if (!bfd_check_format (prog_bfd, bfd_object))
116 {
117 fprintf (stderr, "%s: \"%s\" is not an object file: %s\n",
118 myname, name, bfd_errmsg (bfd_get_error ()));
119 exit (1);
120 }
121 }
122
123 if (STATE_VERBOSE_P (sd))
124 printf ("%s %s\n", myname, name);
125
126 /* Load the program into the simulator. */
127 if (sim_load (sd, name, prog_bfd, 0) == SIM_RC_FAIL)
128 exit (1);
129
130 /* Prepare the program for execution. */
131 sim_create_inferior (sd, prog_bfd, prog_argv, environ);
132
133 /* To accommodate relative file paths, chdir to sysroot now. We
134 mustn't do this until BFD has opened the program, else we wouldn't
135 find the executable if it has a relative file path. */
136 if (simulator_sysroot[0] != '\0' && chdir (simulator_sysroot) < 0)
137 {
138 fprintf (stderr, "%s: can't change directory to \"%s\"\n",
139 myname, simulator_sysroot);
140 exit (1);
141 }
142
143 /* Run/Step the program. */
144 if (single_step)
145 {
146 do
147 {
148 prev_sigint = signal (SIGINT, cntrl_c);
149 sim_resume (sd, 1/*step*/, 0);
150 signal (SIGINT, prev_sigint);
151 sim_stop_reason (sd, &reason, &sigrc);
152
153 if ((reason == sim_stopped) &&
154 (sigrc == sim_signal_to_host (sd, SIM_SIGINT)))
155 break; /* exit on control-C */
156 }
157 /* remain on breakpoint or signals in oe mode*/
158 while (((reason == sim_signalled) &&
159 (sigrc == sim_signal_to_host (sd, SIM_SIGTRAP))) ||
160 ((reason == sim_stopped) &&
161 (STATE_ENVIRONMENT (sd) == OPERATING_ENVIRONMENT)));
162 }
163 else
164 {
165 do
166 {
167 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
168 struct sigaction sa, osa;
169 sa.sa_handler = cntrl_c;
170 sigemptyset (&sa.sa_mask);
171 sa.sa_flags = 0;
172 sigaction (SIGINT, &sa, &osa);
173 prev_sigint = osa.sa_handler;
174 #else
175 prev_sigint = signal (SIGINT, cntrl_c);
176 #endif
177 sim_resume (sd, 0, sigrc);
178 signal (SIGINT, prev_sigint);
179 sim_stop_reason (sd, &reason, &sigrc);
180
181 if ((reason == sim_stopped) &&
182 (sigrc == sim_signal_to_host (sd, SIM_SIGINT)))
183 break; /* exit on control-C */
184
185 /* remain on signals in oe mode */
186 } while ((reason == sim_stopped) &&
187 (STATE_ENVIRONMENT (sd) == OPERATING_ENVIRONMENT));
188
189 }
190 /* Print any stats the simulator collected. */
191 if (STATE_VERBOSE_P (sd))
192 sim_info (sd, 0);
193
194 /* Shutdown the simulator. */
195 sim_close (sd, 0);
196
197 /* If reason is sim_exited, then sigrc holds the exit code which we want
198 to return. If reason is sim_stopped or sim_signalled, then sigrc holds
199 the signal that the simulator received; we want to return that to
200 indicate failure. */
201
202 /* Why did we stop? */
203 switch (reason)
204 {
205 case sim_signalled:
206 case sim_stopped:
207 if (sigrc != 0)
208 fprintf (stderr, "program stopped with signal %d (%s).\n", sigrc,
209 strsignal (sigrc));
210 break;
211
212 case sim_exited:
213 break;
214
215 default:
216 fprintf (stderr, "program in undefined state (%d:%d)\n", reason, sigrc);
217 break;
218
219 }
220
221 return sigrc;
222 }
223
224 static void
225 usage (void)
226 {
227 fprintf (stderr, "Usage: %s [options] program [program args]\n", myname);
228 fprintf (stderr, "Run `%s --help' for full list of options.\n", myname);
229 exit (1);
230 }
This page took 0.033872 seconds and 5 git commands to generate.