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