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