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