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