]>
Commit | Line | Data |
---|---|---|
1 | /* New version of run front end support for simulators. | |
2 | Copyright (C) 1997-2021 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 | /* Need to be before general includes, to pick up e.g. _GNU_SOURCE. */ | |
18 | #include "defs.h" | |
19 | ||
20 | #include <signal.h> | |
21 | #include <stdlib.h> | |
22 | /* For strsignal. */ | |
23 | #include <string.h> | |
24 | ||
25 | #include "sim-main.h" | |
26 | #include "sim-signal.h" | |
27 | #include "sim/callback.h" | |
28 | ||
29 | #include "bfd.h" | |
30 | #include "environ.h" | |
31 | ||
32 | #ifndef HAVE_STRSIGNAL | |
33 | /* While libiberty provides a fallback, it doesn't provide a prototype. */ | |
34 | extern const char *strsignal (int); | |
35 | #endif | |
36 | ||
37 | #ifdef HAVE_UNISTD_H | |
38 | /* For chdir. */ | |
39 | #include <unistd.h> | |
40 | #endif | |
41 | ||
42 | static void usage (void); | |
43 | ||
44 | extern host_callback default_callback; | |
45 | ||
46 | static const char *myname; | |
47 | ||
48 | static SIM_DESC sd; | |
49 | ||
50 | static RETSIGTYPE | |
51 | cntrl_c (int sig) | |
52 | { | |
53 | if (! sim_stop (sd)) | |
54 | { | |
55 | fprintf (stderr, "Quit!\n"); | |
56 | exit (1); | |
57 | } | |
58 | } | |
59 | ||
60 | int | |
61 | main (int argc, char **argv) | |
62 | { | |
63 | const char *name; | |
64 | char **prog_argv = NULL; | |
65 | struct bfd *prog_bfd; | |
66 | enum sim_stop reason; | |
67 | int sigrc = 0; | |
68 | int single_step = 0; | |
69 | RETSIGTYPE (*prev_sigint) (); | |
70 | ||
71 | myname = lbasename (argv[0]); | |
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 | ||
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; | |
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 | sim_create_inferior (sd, prog_bfd, prog_argv, environ); | |
135 | ||
136 | /* To accommodate relative file paths, chdir to sysroot now. We | |
137 | mustn't do this until BFD has opened the program, else we wouldn't | |
138 | find the executable if it has a relative file path. */ | |
139 | if (simulator_sysroot[0] != '\0' && chdir (simulator_sysroot) < 0) | |
140 | { | |
141 | fprintf (stderr, "%s: can't change directory to \"%s\"\n", | |
142 | myname, simulator_sysroot); | |
143 | exit (1); | |
144 | } | |
145 | ||
146 | /* Run/Step the program. */ | |
147 | if (single_step) | |
148 | { | |
149 | do | |
150 | { | |
151 | prev_sigint = signal (SIGINT, cntrl_c); | |
152 | sim_resume (sd, 1/*step*/, 0); | |
153 | signal (SIGINT, prev_sigint); | |
154 | sim_stop_reason (sd, &reason, &sigrc); | |
155 | ||
156 | if ((reason == sim_stopped) && | |
157 | (sigrc == sim_signal_to_host (sd, SIM_SIGINT))) | |
158 | break; /* exit on control-C */ | |
159 | } | |
160 | /* remain on breakpoint or signals in oe mode*/ | |
161 | while (((reason == sim_signalled) && | |
162 | (sigrc == sim_signal_to_host (sd, SIM_SIGTRAP))) || | |
163 | ((reason == sim_stopped) && | |
164 | (STATE_ENVIRONMENT (sd) == OPERATING_ENVIRONMENT))); | |
165 | } | |
166 | else | |
167 | { | |
168 | do | |
169 | { | |
170 | #if defined (HAVE_SIGACTION) && defined (SA_RESTART) | |
171 | struct sigaction sa, osa; | |
172 | sa.sa_handler = cntrl_c; | |
173 | sigemptyset (&sa.sa_mask); | |
174 | sa.sa_flags = 0; | |
175 | sigaction (SIGINT, &sa, &osa); | |
176 | prev_sigint = osa.sa_handler; | |
177 | #else | |
178 | prev_sigint = signal (SIGINT, cntrl_c); | |
179 | #endif | |
180 | sim_resume (sd, 0, sigrc); | |
181 | signal (SIGINT, prev_sigint); | |
182 | sim_stop_reason (sd, &reason, &sigrc); | |
183 | ||
184 | if ((reason == sim_stopped) && | |
185 | (sigrc == sim_signal_to_host (sd, SIM_SIGINT))) | |
186 | break; /* exit on control-C */ | |
187 | ||
188 | /* remain on signals in oe mode */ | |
189 | } while ((reason == sim_stopped) && | |
190 | (STATE_ENVIRONMENT (sd) == OPERATING_ENVIRONMENT)); | |
191 | ||
192 | } | |
193 | /* Print any stats the simulator collected. */ | |
194 | if (STATE_VERBOSE_P (sd)) | |
195 | sim_info (sd, 0); | |
196 | ||
197 | /* Shutdown the simulator. */ | |
198 | sim_close (sd, 0); | |
199 | ||
200 | /* If reason is sim_exited, then sigrc holds the exit code which we want | |
201 | to return. If reason is sim_stopped or sim_signalled, then sigrc holds | |
202 | the signal that the simulator received; we want to return that to | |
203 | indicate failure. */ | |
204 | ||
205 | /* Why did we stop? */ | |
206 | switch (reason) | |
207 | { | |
208 | case sim_signalled: | |
209 | case sim_stopped: | |
210 | if (sigrc != 0) | |
211 | fprintf (stderr, "program stopped with signal %d (%s).\n", sigrc, | |
212 | strsignal (sigrc)); | |
213 | break; | |
214 | ||
215 | case sim_exited: | |
216 | break; | |
217 | ||
218 | default: | |
219 | fprintf (stderr, "program in undefined state (%d:%d)\n", reason, sigrc); | |
220 | break; | |
221 | ||
222 | } | |
223 | ||
224 | return sigrc; | |
225 | } | |
226 | ||
227 | static void | |
228 | usage (void) | |
229 | { | |
230 | fprintf (stderr, "Usage: %s [options] program [program args]\n", myname); | |
231 | fprintf (stderr, "Run `%s --help' for full list of options.\n", myname); | |
232 | exit (1); | |
233 | } |