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