]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - sim/common/nrun.c
sim: nrun: add --env-{set,unset,clear} command line options
[thirdparty/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 #include "sim-signal.h"
27 #include "sim/callback.h"
28
29 #include "bfd.h"
30 #include "environ.h"
31
32 #ifndef HAVE_STRSIGNAL
33 /* While libiberty provides a fallback, it doesn't provide a prototype. */
34 extern const char *strsignal (int);
35 #endif
36
37 #ifdef HAVE_UNISTD_H
38 /* For chdir. */
39 #include <unistd.h>
40 #endif
41
42 static void usage (void);
43
44 extern host_callback default_callback;
45
46 static const char *myname;
47
48 static SIM_DESC sd;
49
50 static RETSIGTYPE
51 cntrl_c (int sig)
52 {
53 if (! sim_stop (sd))
54 {
55 fprintf (stderr, "Quit!\n");
56 exit (1);
57 }
58 }
59
60 int
61 main (int argc, char **argv)
62 {
63 const char *name;
64 char **prog_argv = NULL;
65 char **prog_envp = NULL;
66 struct bfd *prog_bfd;
67 enum sim_stop reason;
68 int sigrc = 0;
69 int single_step = 0;
70 RETSIGTYPE (*prev_sigint) ();
71
72 myname = lbasename (argv[0]);
73
74 /* INTERNAL: When MYNAME is `step', single step the simulator
75 instead of allowing it to run free. The sole purpose of this
76 HACK is to allow the sim_resume interface's step argument to be
77 tested without having to build/run gdb. */
78 if (strlen (myname) > 4 && strcmp (myname - 4, "step") == 0)
79 {
80 single_step = 1;
81 }
82
83 /* Create an instance of the simulator. */
84 default_callback.init (&default_callback);
85 sd = sim_open (SIM_OPEN_STANDALONE, &default_callback, NULL, argv);
86 if (sd == 0)
87 exit (1);
88 if (STATE_MAGIC (sd) != SIM_MAGIC_NUMBER)
89 {
90 fprintf (stderr, "Internal error - bad magic number in simulator struct\n");
91 abort ();
92 }
93
94 /* We can't set the endianness in the callback structure until sim_config is
95 called, which happens in sim_open. If it's still the default, switch it.
96 Don't use CURRENT_TARGET_BYTE_ORDER as all its internal processing already
97 happened in sim_config. */
98 if (default_callback.target_endian == BFD_ENDIAN_UNKNOWN)
99 default_callback.target_endian = current_target_byte_order;
100
101 /* Was there a program to run? */
102 prog_argv = STATE_PROG_ARGV (sd);
103 prog_envp = STATE_PROG_ENVP (sd) ? : environ;
104 prog_bfd = STATE_PROG_BFD (sd);
105 if (prog_argv == NULL || *prog_argv == NULL)
106 usage ();
107
108 name = STATE_PROG_FILE (sd);
109
110 /* For simulators that don't open prog during sim_open() */
111 if (prog_bfd == NULL)
112 {
113 prog_bfd = bfd_openr (name, 0);
114 if (prog_bfd == NULL)
115 {
116 fprintf (stderr, "%s: can't open \"%s\": %s\n",
117 myname, name, bfd_errmsg (bfd_get_error ()));
118 exit (1);
119 }
120 if (!bfd_check_format (prog_bfd, bfd_object))
121 {
122 fprintf (stderr, "%s: \"%s\" is not an object file: %s\n",
123 myname, name, bfd_errmsg (bfd_get_error ()));
124 exit (1);
125 }
126 }
127
128 if (STATE_VERBOSE_P (sd))
129 printf ("%s %s\n", myname, name);
130
131 /* Load the program into the simulator. */
132 if (sim_load (sd, name, prog_bfd, 0) == SIM_RC_FAIL)
133 exit (1);
134
135 /* Prepare the program for execution. */
136 sim_create_inferior (sd, prog_bfd, prog_argv, prog_envp);
137
138 /* To accommodate relative file paths, chdir to sysroot now. We
139 mustn't do this until BFD has opened the program, else we wouldn't
140 find the executable if it has a relative file path. */
141 if (simulator_sysroot[0] != '\0' && chdir (simulator_sysroot) < 0)
142 {
143 fprintf (stderr, "%s: can't change directory to \"%s\"\n",
144 myname, simulator_sysroot);
145 exit (1);
146 }
147
148 /* Run/Step the program. */
149 if (single_step)
150 {
151 do
152 {
153 prev_sigint = signal (SIGINT, cntrl_c);
154 sim_resume (sd, 1/*step*/, 0);
155 signal (SIGINT, prev_sigint);
156 sim_stop_reason (sd, &reason, &sigrc);
157
158 if ((reason == sim_stopped) &&
159 (sigrc == sim_signal_to_host (sd, SIM_SIGINT)))
160 break; /* exit on control-C */
161 }
162 /* remain on breakpoint or signals in oe mode*/
163 while (((reason == sim_signalled) &&
164 (sigrc == sim_signal_to_host (sd, SIM_SIGTRAP))) ||
165 ((reason == sim_stopped) &&
166 (STATE_ENVIRONMENT (sd) == OPERATING_ENVIRONMENT)));
167 }
168 else
169 {
170 do
171 {
172 #if defined (HAVE_SIGACTION) && defined (SA_RESTART)
173 struct sigaction sa, osa;
174 sa.sa_handler = cntrl_c;
175 sigemptyset (&sa.sa_mask);
176 sa.sa_flags = 0;
177 sigaction (SIGINT, &sa, &osa);
178 prev_sigint = osa.sa_handler;
179 #else
180 prev_sigint = signal (SIGINT, cntrl_c);
181 #endif
182 sim_resume (sd, 0, sigrc);
183 signal (SIGINT, prev_sigint);
184 sim_stop_reason (sd, &reason, &sigrc);
185
186 if ((reason == sim_stopped) &&
187 (sigrc == sim_signal_to_host (sd, SIM_SIGINT)))
188 break; /* exit on control-C */
189
190 /* remain on signals in oe mode */
191 } while ((reason == sim_stopped) &&
192 (STATE_ENVIRONMENT (sd) == OPERATING_ENVIRONMENT));
193
194 }
195 /* Print any stats the simulator collected. */
196 if (STATE_VERBOSE_P (sd))
197 sim_info (sd, 0);
198
199 /* Shutdown the simulator. */
200 sim_close (sd, 0);
201
202 /* If reason is sim_exited, then sigrc holds the exit code which we want
203 to return. If reason is sim_stopped or sim_signalled, then sigrc holds
204 the signal that the simulator received; we want to return that to
205 indicate failure. */
206
207 /* Why did we stop? */
208 switch (reason)
209 {
210 case sim_signalled:
211 case sim_stopped:
212 if (sigrc != 0)
213 fprintf (stderr, "program stopped with signal %d (%s).\n", sigrc,
214 strsignal (sigrc));
215 break;
216
217 case sim_exited:
218 break;
219
220 default:
221 fprintf (stderr, "program in undefined state (%d:%d)\n", reason, sigrc);
222 break;
223
224 }
225
226 return sigrc;
227 }
228
229 static void
230 usage (void)
231 {
232 fprintf (stderr, "Usage: %s [options] [--] program [program args]\n", myname);
233 fprintf (stderr, "Run `%s --help' for full list of options.\n", myname);
234 exit (1);
235 }