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