Commit | Line | Data |
---|---|---|
c906108c | 1 | /* run front end support for all the simulators. |
6aba47ca | 2 | Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, |
7b6bb8da JB |
3 | 2002, 2003, 2004, 2007, 2008, 2009, 2010, 2011 |
4 | Free Software Foundation, Inc. | |
c906108c SS |
5 | |
6 | This program is free software; you can redistribute it and/or modify | |
7 | it under the terms of the GNU General Public License as published by | |
4744ac1b JB |
8 | the Free Software Foundation; either version 3 of the License, or |
9 | (at your option) any later version. | |
c906108c SS |
10 | |
11 | This program is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
15 | ||
4744ac1b JB |
16 | You should have received a copy of the GNU General Public License |
17 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ | |
c906108c SS |
18 | |
19 | /* Steve Chamberlain sac@cygnus.com, | |
20 | and others at Cygnus. */ | |
21 | ||
41ee5402 DJ |
22 | #ifdef HAVE_CONFIG_H |
23 | #include "cconfig.h" | |
c906108c | 24 | #include "tconfig.h" |
41ee5402 | 25 | #endif |
c906108c SS |
26 | |
27 | #include <signal.h> | |
28 | #include <stdio.h> | |
29 | #ifdef __STDC__ | |
30 | #include <stdarg.h> | |
31 | #else | |
32 | #include <varargs.h> | |
33 | #endif | |
34 | ||
35 | #ifdef HAVE_STDLIB_H | |
36 | #include <stdlib.h> | |
37 | #endif | |
38 | ||
39 | #ifdef HAVE_STRING_H | |
40 | #include <string.h> | |
41 | #else | |
42 | #ifdef HAVE_STRINGS_H | |
43 | #include <strings.h> | |
44 | #endif | |
45 | #endif | |
46 | ||
47 | #include "libiberty.h" | |
48 | #include "bfd.h" | |
3c25f8c7 AC |
49 | #include "gdb/callback.h" |
50 | #include "gdb/remote-sim.h" | |
6d358e86 | 51 | #include "ansidecl.h" |
de4112fa | 52 | #include "run-sim.h" |
6efef468 | 53 | #include "version.h" |
c906108c | 54 | |
6efef468 JM |
55 | static void usage PARAMS ((int help)); |
56 | static void print_version PARAMS ((void)); | |
c906108c SS |
57 | extern int optind; |
58 | extern char *optarg; | |
59 | ||
60 | extern host_callback default_callback; | |
61 | ||
62 | static char *myname; | |
63 | ||
c906108c SS |
64 | extern int getopt (); |
65 | ||
7a292a7a | 66 | #ifdef NEED_UI_LOOP_HOOK |
dbec3bef | 67 | /* Gdb foolery. This is only needed for gdb using a gui. */ |
0aaa4a81 | 68 | int (*deprecated_ui_loop_hook) PARAMS ((int signo)); |
7a292a7a SS |
69 | #endif |
70 | ||
c906108c SS |
71 | static SIM_DESC sd; |
72 | ||
73 | static RETSIGTYPE | |
6d358e86 | 74 | cntrl_c (int sig ATTRIBUTE_UNUSED) |
c906108c SS |
75 | { |
76 | if (! sim_stop (sd)) | |
77 | { | |
78 | fprintf (stderr, "Quit!\n"); | |
79 | exit (1); | |
80 | } | |
81 | } | |
82 | ||
83 | int | |
84 | main (ac, av) | |
85 | int ac; | |
86 | char **av; | |
87 | { | |
88 | RETSIGTYPE (*prev_sigint) (); | |
89 | bfd *abfd; | |
90 | int i; | |
91 | int verbose = 0; | |
92 | int trace = 0; | |
baa7ae6f AC |
93 | #ifdef SIM_HAVE_ENVIRONMENT |
94 | int operating_p = 0; | |
95 | #endif | |
c906108c SS |
96 | char *name; |
97 | static char *no_args[4]; | |
98 | char **sim_argv = &no_args[0]; | |
99 | char **prog_args; | |
100 | enum sim_stop reason; | |
101 | int sigrc; | |
102 | ||
103 | myname = av[0] + strlen (av[0]); | |
104 | while (myname > av[0] && myname[-1] != '/') | |
105 | --myname; | |
106 | ||
107 | /* The first element of sim_open's argv is the program name. */ | |
108 | no_args[0] = av[0]; | |
109 | #ifdef SIM_HAVE_BIENDIAN | |
110 | no_args[1] = "-E"; | |
111 | no_args[2] = "set-later"; | |
112 | #endif | |
113 | ||
114 | /* FIXME: This is currently being migrated into sim_open. | |
115 | Simulators that use functions such as sim_size() still require | |
dbec3bef | 116 | this. */ |
c906108c SS |
117 | default_callback.init (&default_callback); |
118 | sim_set_callbacks (&default_callback); | |
119 | ||
de4112fa NC |
120 | #ifdef SIM_TARGET_SWITCHES |
121 | ac = sim_target_parse_command_line (ac, av); | |
122 | #endif | |
123 | ||
6efef468 JM |
124 | for (i = 1; av[i]; ++i) |
125 | { | |
126 | if (strcmp (av[i], "--help") == 0) | |
127 | { | |
128 | usage (1); | |
129 | } | |
130 | else if (strcmp (av[i], "--version") == 0) | |
131 | { | |
132 | print_version (); | |
133 | return 0; | |
134 | } | |
135 | } | |
136 | ||
c906108c SS |
137 | /* FIXME: This is currently being rewritten to have each simulator |
138 | do all argv processing. */ | |
139 | ||
dbec3bef | 140 | while ((i = getopt (ac, av, "a:c:m:op:s:tv")) != EOF) |
c906108c SS |
141 | switch (i) |
142 | { | |
143 | case 'a': | |
144 | /* FIXME: Temporary hack. */ | |
145 | { | |
146 | int len = strlen (av[0]) + strlen (optarg); | |
147 | char *argbuf = (char *) alloca (len + 2 + 50); | |
148 | sprintf (argbuf, "%s %s", av[0], optarg); | |
149 | #ifdef SIM_HAVE_BIENDIAN | |
150 | /* The desired endianness must be passed to sim_open. | |
151 | The value for "set-later" is set when we know what it is. | |
152 | -E support isn't yet part of the published interface. */ | |
153 | strcat (argbuf, " -E set-later"); | |
154 | #endif | |
155 | sim_argv = buildargv (argbuf); | |
156 | } | |
157 | break; | |
158 | #ifdef SIM_HAVE_SIMCACHE | |
159 | case 'c': | |
160 | sim_set_simcache_size (atoi (optarg)); | |
161 | break; | |
162 | #endif | |
163 | case 'm': | |
164 | /* FIXME: Rename to sim_set_mem_size. */ | |
165 | sim_size (atoi (optarg)); | |
166 | break; | |
baa7ae6f AC |
167 | #ifdef SIM_HAVE_ENVIRONMENT |
168 | case 'o': | |
169 | /* Operating enironment where any signals are delivered to the | |
dbec3bef | 170 | target. */ |
baa7ae6f AC |
171 | operating_p = 1; |
172 | break; | |
d25b1553 | 173 | #endif |
c906108c SS |
174 | #ifdef SIM_HAVE_PROFILE |
175 | case 'p': | |
176 | sim_set_profile (atoi (optarg)); | |
177 | break; | |
178 | case 's': | |
179 | sim_set_profile_size (atoi (optarg)); | |
180 | break; | |
181 | #endif | |
182 | case 't': | |
183 | trace = 1; | |
c906108c SS |
184 | break; |
185 | case 'v': | |
186 | /* Things that are printed with -v are the kinds of things that | |
187 | gcc -v prints. This is not meant to include detailed tracing | |
188 | or debugging information, just summaries. */ | |
189 | verbose = 1; | |
190 | /* sim_set_verbose (1); */ | |
191 | break; | |
192 | /* FIXME: Quick hack, to be replaced by more general facility. */ | |
c906108c | 193 | default: |
6efef468 | 194 | usage (0); |
c906108c SS |
195 | } |
196 | ||
197 | ac -= optind; | |
198 | av += optind; | |
199 | if (ac <= 0) | |
6efef468 | 200 | usage (0); |
c906108c SS |
201 | |
202 | name = *av; | |
203 | prog_args = av; | |
204 | ||
205 | if (verbose) | |
206 | { | |
207 | printf ("%s %s\n", myname, name); | |
208 | } | |
209 | ||
210 | abfd = bfd_openr (name, 0); | |
dbec3bef | 211 | if (!abfd) |
c906108c | 212 | { |
dbec3bef | 213 | fprintf (stderr, "%s: can't open %s: %s\n", |
c906108c SS |
214 | myname, name, bfd_errmsg (bfd_get_error ())); |
215 | exit (1); | |
216 | } | |
217 | ||
218 | if (!bfd_check_format (abfd, bfd_object)) | |
219 | { | |
220 | fprintf (stderr, "%s: can't load %s: %s\n", | |
221 | myname, name, bfd_errmsg (bfd_get_error ())); | |
222 | exit (1); | |
223 | } | |
224 | ||
225 | #ifdef SIM_HAVE_BIENDIAN | |
226 | /* The endianness must be passed to sim_open because one may wish to | |
227 | examine/set registers before calling sim_load [which is the other | |
228 | place where one can determine endianness]. We previously passed the | |
229 | endianness via global `target_byte_order' but that's not a clean | |
230 | interface. */ | |
231 | for (i = 1; sim_argv[i + 1] != NULL; ++i) | |
232 | continue; | |
233 | if (bfd_big_endian (abfd)) | |
234 | sim_argv[i] = "big"; | |
235 | else | |
236 | sim_argv[i] = "little"; | |
237 | #endif | |
238 | ||
239 | /* Ensure that any run-time initialisation that needs to be | |
dbec3bef | 240 | performed by the simulator can occur. */ |
c906108c SS |
241 | sd = sim_open (SIM_OPEN_STANDALONE, &default_callback, abfd, sim_argv); |
242 | if (sd == 0) | |
243 | exit (1); | |
244 | ||
245 | if (sim_load (sd, name, abfd, 0) == SIM_RC_FAIL) | |
246 | exit (1); | |
247 | ||
248 | if (sim_create_inferior (sd, abfd, prog_args, NULL) == SIM_RC_FAIL) | |
249 | exit (1); | |
250 | ||
baa7ae6f AC |
251 | #ifdef SIM_HAVE_ENVIRONMENT |
252 | /* NOTE: An old simulator supporting the operating environment MUST | |
253 | provide sim_set_trace() and not sim_trace(). That way | |
dbec3bef | 254 | sim_stop_reason() can be used to determine any stop reason. */ |
baa7ae6f AC |
255 | if (trace) |
256 | sim_set_trace (); | |
781c552e | 257 | sigrc = 0; |
baa7ae6f AC |
258 | do |
259 | { | |
260 | prev_sigint = signal (SIGINT, cntrl_c); | |
261 | sim_resume (sd, 0, sigrc); | |
262 | signal (SIGINT, prev_sigint); | |
263 | sim_stop_reason (sd, &reason, &sigrc); | |
264 | } | |
265 | while (operating_p && reason == sim_stopped && sigrc != SIGINT); | |
266 | #else | |
c906108c SS |
267 | if (trace) |
268 | { | |
269 | int done = 0; | |
7fc5b5ad | 270 | prev_sigint = signal (SIGINT, cntrl_c); |
c906108c SS |
271 | while (!done) |
272 | { | |
273 | done = sim_trace (sd); | |
274 | } | |
7fc5b5ad | 275 | signal (SIGINT, prev_sigint); |
baa7ae6f | 276 | sim_stop_reason (sd, &reason, &sigrc); |
c906108c SS |
277 | } |
278 | else | |
279 | { | |
baa7ae6f | 280 | prev_sigint = signal (SIGINT, cntrl_c); |
781c552e | 281 | sigrc = 0; |
baa7ae6f AC |
282 | sim_resume (sd, 0, sigrc); |
283 | signal (SIGINT, prev_sigint); | |
284 | sim_stop_reason (sd, &reason, &sigrc); | |
c906108c | 285 | } |
baa7ae6f | 286 | #endif |
c906108c SS |
287 | |
288 | if (verbose) | |
289 | sim_info (sd, 0); | |
c906108c SS |
290 | sim_close (sd, 0); |
291 | ||
292 | /* If reason is sim_exited, then sigrc holds the exit code which we want | |
293 | to return. If reason is sim_stopped or sim_signalled, then sigrc holds | |
294 | the signal that the simulator received; we want to return that to | |
295 | indicate failure. */ | |
296 | ||
c906108c SS |
297 | /* Why did we stop? */ |
298 | switch (reason) | |
299 | { | |
300 | case sim_signalled: | |
301 | case sim_stopped: | |
302 | if (sigrc != 0) | |
dbec3bef | 303 | fprintf (stderr, "program stopped with signal %d.\n", sigrc); |
c906108c SS |
304 | break; |
305 | ||
306 | case sim_exited: | |
307 | break; | |
308 | ||
309 | case sim_running: | |
dbec3bef | 310 | case sim_polling: /* These indicate a serious problem. */ |
c906108c SS |
311 | abort (); |
312 | break; | |
313 | ||
314 | } | |
c906108c SS |
315 | |
316 | return sigrc; | |
317 | } | |
318 | ||
319 | static void | |
6efef468 | 320 | usage (int help) |
c906108c | 321 | { |
6efef468 JM |
322 | FILE *stream = help ? stdout : stderr; |
323 | ||
324 | fprintf (stream, "Usage: %s [options] program [program args]\n", myname); | |
325 | fprintf (stream, "Options:\n"); | |
326 | fprintf (stream, "-a args Pass `args' to simulator.\n"); | |
c906108c | 327 | #ifdef SIM_HAVE_SIMCACHE |
6efef468 | 328 | fprintf (stream, "-c size Set simulator cache size to `size'.\n"); |
c906108c | 329 | #endif |
6efef468 | 330 | fprintf (stream, "-m size Set memory size of simulator, in bytes.\n"); |
baa7ae6f | 331 | #ifdef SIM_HAVE_ENVIRONMENT |
6efef468 | 332 | fprintf (stream, "-o Select operating (kernel) environment.\n"); |
baa7ae6f | 333 | #endif |
c906108c | 334 | #ifdef SIM_HAVE_PROFILE |
6efef468 JM |
335 | fprintf (stream, "-p freq Set profiling frequency.\n"); |
336 | fprintf (stream, "-s size Set profiling size.\n"); | |
c906108c | 337 | #endif |
6efef468 JM |
338 | fprintf (stream, "-t Perform instruction tracing.\n"); |
339 | fprintf (stream, " Note: Very few simulators support tracing.\n"); | |
340 | fprintf (stream, "-v Verbose output.\n"); | |
341 | fprintf (stream, "\n"); | |
342 | fprintf (stream, "program args Arguments to pass to simulated program.\n"); | |
343 | fprintf (stream, " Note: Very few simulators support this.\n"); | |
de4112fa | 344 | #ifdef SIM_TARGET_SWITCHES |
6efef468 JM |
345 | fprintf (stream, "\nTarget specific options:\n"); |
346 | sim_target_display_usage (help); | |
de4112fa | 347 | #endif |
6efef468 JM |
348 | |
349 | if (help && REPORT_BUGS_TO[0]) | |
350 | printf ("Report bugs to %s\n", REPORT_BUGS_TO); | |
351 | ||
352 | exit (help ? 0 : 1); | |
353 | } | |
354 | ||
028f6515 | 355 | static void |
6efef468 JM |
356 | print_version () |
357 | { | |
358 | printf ("GNU simulator %s%s\n", PKGVERSION, version); | |
c906108c | 359 | } |