sim: options: fix --help output
[deliverable/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
HPN
17/* Need to be before general includes, to pick up e.g. _GNU_SOURCE. */
18#ifdef HAVE_CONFIG_H
936df756 19#include "config.h"
f914e384
HPN
20#endif
21
c906108c 22#include <signal.h>
b760fb3a 23#include <stdlib.h>
2232061b 24/* For strsignal. */
2232061b 25#include <string.h>
2232061b 26
c906108c
SS
27#include "sim-main.h"
28
29#include "bfd.h"
30
fe348617
MF
31#ifndef HAVE_STRSIGNAL
32/* While libiberty provides a fallback, it doesn't provide a prototype. */
33extern const char *strsignal (int);
34#endif
35
c906108c
SS
36#ifdef HAVE_ENVIRON
37extern char **environ;
38#endif
39
027e2a04
HPN
40#ifdef HAVE_UNISTD_H
41/* For chdir. */
42#include <unistd.h>
43#endif
44
c906108c
SS
45static void usage (void);
46
47extern host_callback default_callback;
48
aba6f46b 49static const char *myname;
c906108c
SS
50
51static SIM_DESC sd;
52
53static RETSIGTYPE
54cntrl_c (int sig)
55{
56 if (! sim_stop (sd))
57 {
58 fprintf (stderr, "Quit!\n");
59 exit (1);
60 }
61}
62
63int
64main (int argc, char **argv)
65{
aba6f46b 66 const char *name;
c906108c 67 char **prog_argv = NULL;
6b4a8935 68 struct bfd *prog_bfd;
c906108c
SS
69 enum sim_stop reason;
70 int sigrc = 0;
71 int single_step = 0;
72 RETSIGTYPE (*prev_sigint) ();
73
aba6f46b 74 myname = lbasename (argv[0]);
c906108c
SS
75
76 /* INTERNAL: When MYNAME is `step', single step the simulator
77 instead of allowing it to run free. The sole purpose of this
78 HACK is to allow the sim_resume interface's step argument to be
79 tested without having to build/run gdb. */
80 if (strlen (myname) > 4 && strcmp (myname - 4, "step") == 0)
81 {
82 single_step = 1;
83 }
84
85 /* Create an instance of the simulator. */
86 default_callback.init (&default_callback);
87 sd = sim_open (SIM_OPEN_STANDALONE, &default_callback, NULL, argv);
88 if (sd == 0)
89 exit (1);
90 if (STATE_MAGIC (sd) != SIM_MAGIC_NUMBER)
91 {
92 fprintf (stderr, "Internal error - bad magic number in simulator struct\n");
93 abort ();
94 }
95
f4f8cce4
HPN
96 /* We can't set the endianness in the callback structure until
97 sim_config is called, which happens in sim_open. */
98 default_callback.target_endian
1ac72f06 99 = (CURRENT_TARGET_BYTE_ORDER == BFD_ENDIAN_BIG
f4f8cce4
HPN
100 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
101
c906108c
SS
102 /* Was there a program to run? */
103 prog_argv = STATE_PROG_ARGV (sd);
104 prog_bfd = STATE_PROG_BFD (sd);
105 if (prog_argv == NULL || *prog_argv == NULL)
106 usage ();
107
108 name = *prog_argv;
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 {
028f6515 116 fprintf (stderr, "%s: can't open \"%s\": %s\n",
c906108c
SS
117 myname, name, bfd_errmsg (bfd_get_error ()));
118 exit (1);
119 }
028f6515 120 if (!bfd_check_format (prog_bfd, bfd_object))
c906108c
SS
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#ifdef HAVE_ENVIRON
137 sim_create_inferior (sd, prog_bfd, prog_argv, environ);
138#else
139 sim_create_inferior (sd, prog_bfd, prog_argv, NULL);
140#endif
141
027e2a04
HPN
142 /* To accommodate relative file paths, chdir to sysroot now. We
143 mustn't do this until BFD has opened the program, else we wouldn't
144 find the executable if it has a relative file path. */
145 if (simulator_sysroot[0] != '\0' && chdir (simulator_sysroot) < 0)
146 {
147 fprintf (stderr, "%s: can't change directory to \"%s\"\n",
148 myname, simulator_sysroot);
149 exit (1);
150 }
151
c906108c
SS
152 /* Run/Step the program. */
153 if (single_step)
154 {
155 do
156 {
157 prev_sigint = signal (SIGINT, cntrl_c);
158 sim_resume (sd, 1/*step*/, 0);
159 signal (SIGINT, prev_sigint);
160 sim_stop_reason (sd, &reason, &sigrc);
161
162 if ((reason == sim_stopped) &&
163 (sigrc == sim_signal_to_host (sd, SIM_SIGINT)))
164 break; /* exit on control-C */
165 }
166 /* remain on breakpoint or signals in oe mode*/
167 while (((reason == sim_signalled) &&
168 (sigrc == sim_signal_to_host (sd, SIM_SIGTRAP))) ||
028f6515 169 ((reason == sim_stopped) &&
c906108c
SS
170 (STATE_ENVIRONMENT (sd) == OPERATING_ENVIRONMENT)));
171 }
028f6515 172 else
c906108c 173 {
43e526b9
JM
174 do
175 {
c906108c 176#if defined (HAVE_SIGACTION) && defined (SA_RESTART)
43e526b9
JM
177 struct sigaction sa, osa;
178 sa.sa_handler = cntrl_c;
179 sigemptyset (&sa.sa_mask);
180 sa.sa_flags = 0;
181 sigaction (SIGINT, &sa, &osa);
182 prev_sigint = osa.sa_handler;
c906108c 183#else
43e526b9 184 prev_sigint = signal (SIGINT, cntrl_c);
c906108c 185#endif
43e526b9
JM
186 sim_resume (sd, 0, sigrc);
187 signal (SIGINT, prev_sigint);
188 sim_stop_reason (sd, &reason, &sigrc);
028f6515 189
43e526b9
JM
190 if ((reason == sim_stopped) &&
191 (sigrc == sim_signal_to_host (sd, SIM_SIGINT)))
192 break; /* exit on control-C */
028f6515 193
43e526b9
JM
194 /* remain on signals in oe mode */
195 } while ((reason == sim_stopped) &&
196 (STATE_ENVIRONMENT (sd) == OPERATING_ENVIRONMENT));
028f6515 197
43e526b9 198 }
c906108c 199 /* Print any stats the simulator collected. */
f9cbceb6
AC
200 if (STATE_VERBOSE_P (sd))
201 sim_info (sd, 0);
028f6515 202
c906108c
SS
203 /* Shutdown the simulator. */
204 sim_close (sd, 0);
028f6515 205
c906108c
SS
206 /* If reason is sim_exited, then sigrc holds the exit code which we want
207 to return. If reason is sim_stopped or sim_signalled, then sigrc holds
208 the signal that the simulator received; we want to return that to
209 indicate failure. */
028f6515 210
c906108c
SS
211 /* Why did we stop? */
212 switch (reason)
213 {
214 case sim_signalled:
215 case sim_stopped:
216 if (sigrc != 0)
9a5e0c49
MF
217 fprintf (stderr, "program stopped with signal %d (%s).\n", sigrc,
218 strsignal (sigrc));
c906108c
SS
219 break;
220
221 case sim_exited:
222 break;
223
224 default:
225 fprintf (stderr, "program in undefined state (%d:%d)\n", reason, sigrc);
226 break;
227
228 }
c906108c
SS
229
230 return sigrc;
231}
232
233static void
dc416615 234usage (void)
c906108c
SS
235{
236 fprintf (stderr, "Usage: %s [options] program [program args]\n", myname);
237 fprintf (stderr, "Run `%s --help' for full list of options.\n", myname);
238 exit (1);
239}
This page took 0.974957 seconds and 4 git commands to generate.