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