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