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