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