]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - sim/common/run.c
Copyright updates for 2007.
[thirdparty/binutils-gdb.git] / sim / common / run.c
CommitLineData
c906108c 1/* run front end support for all the simulators.
6aba47ca
DJ
2 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
3 2002, 2003, 2004, 2007 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
7the Free Software Foundation; either version 2, or (at your option)
8any later version.
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
15You should have received a copy of the GNU General Public License along
16with this program; if not, write to the Free Software Foundation, Inc.,
1759 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
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"
c906108c 53
c906108c
SS
54static void usage PARAMS ((void));
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
c906108c
SS
122 /* FIXME: This is currently being rewritten to have each simulator
123 do all argv processing. */
124
dbec3bef 125 while ((i = getopt (ac, av, "a:c:m:op:s:tv")) != EOF)
c906108c
SS
126 switch (i)
127 {
128 case 'a':
129 /* FIXME: Temporary hack. */
130 {
131 int len = strlen (av[0]) + strlen (optarg);
132 char *argbuf = (char *) alloca (len + 2 + 50);
133 sprintf (argbuf, "%s %s", av[0], optarg);
134#ifdef SIM_HAVE_BIENDIAN
135 /* The desired endianness must be passed to sim_open.
136 The value for "set-later" is set when we know what it is.
137 -E support isn't yet part of the published interface. */
138 strcat (argbuf, " -E set-later");
139#endif
140 sim_argv = buildargv (argbuf);
141 }
142 break;
143#ifdef SIM_HAVE_SIMCACHE
144 case 'c':
145 sim_set_simcache_size (atoi (optarg));
146 break;
147#endif
148 case 'm':
149 /* FIXME: Rename to sim_set_mem_size. */
150 sim_size (atoi (optarg));
151 break;
baa7ae6f
AC
152#ifdef SIM_HAVE_ENVIRONMENT
153 case 'o':
154 /* Operating enironment where any signals are delivered to the
dbec3bef 155 target. */
baa7ae6f
AC
156 operating_p = 1;
157 break;
d25b1553 158#endif
c906108c
SS
159#ifdef SIM_HAVE_PROFILE
160 case 'p':
161 sim_set_profile (atoi (optarg));
162 break;
163 case 's':
164 sim_set_profile_size (atoi (optarg));
165 break;
166#endif
167 case 't':
168 trace = 1;
c906108c
SS
169 break;
170 case 'v':
171 /* Things that are printed with -v are the kinds of things that
172 gcc -v prints. This is not meant to include detailed tracing
173 or debugging information, just summaries. */
174 verbose = 1;
175 /* sim_set_verbose (1); */
176 break;
177 /* FIXME: Quick hack, to be replaced by more general facility. */
c906108c
SS
178 default:
179 usage ();
180 }
181
182 ac -= optind;
183 av += optind;
184 if (ac <= 0)
185 usage ();
186
187 name = *av;
188 prog_args = av;
189
190 if (verbose)
191 {
192 printf ("%s %s\n", myname, name);
193 }
194
195 abfd = bfd_openr (name, 0);
dbec3bef 196 if (!abfd)
c906108c 197 {
dbec3bef 198 fprintf (stderr, "%s: can't open %s: %s\n",
c906108c
SS
199 myname, name, bfd_errmsg (bfd_get_error ()));
200 exit (1);
201 }
202
203 if (!bfd_check_format (abfd, bfd_object))
204 {
205 fprintf (stderr, "%s: can't load %s: %s\n",
206 myname, name, bfd_errmsg (bfd_get_error ()));
207 exit (1);
208 }
209
210#ifdef SIM_HAVE_BIENDIAN
211 /* The endianness must be passed to sim_open because one may wish to
212 examine/set registers before calling sim_load [which is the other
213 place where one can determine endianness]. We previously passed the
214 endianness via global `target_byte_order' but that's not a clean
215 interface. */
216 for (i = 1; sim_argv[i + 1] != NULL; ++i)
217 continue;
218 if (bfd_big_endian (abfd))
219 sim_argv[i] = "big";
220 else
221 sim_argv[i] = "little";
222#endif
223
224 /* Ensure that any run-time initialisation that needs to be
dbec3bef 225 performed by the simulator can occur. */
c906108c
SS
226 sd = sim_open (SIM_OPEN_STANDALONE, &default_callback, abfd, sim_argv);
227 if (sd == 0)
228 exit (1);
229
230 if (sim_load (sd, name, abfd, 0) == SIM_RC_FAIL)
231 exit (1);
232
233 if (sim_create_inferior (sd, abfd, prog_args, NULL) == SIM_RC_FAIL)
234 exit (1);
235
baa7ae6f
AC
236#ifdef SIM_HAVE_ENVIRONMENT
237 /* NOTE: An old simulator supporting the operating environment MUST
238 provide sim_set_trace() and not sim_trace(). That way
dbec3bef 239 sim_stop_reason() can be used to determine any stop reason. */
baa7ae6f
AC
240 if (trace)
241 sim_set_trace ();
781c552e 242 sigrc = 0;
baa7ae6f
AC
243 do
244 {
245 prev_sigint = signal (SIGINT, cntrl_c);
246 sim_resume (sd, 0, sigrc);
247 signal (SIGINT, prev_sigint);
248 sim_stop_reason (sd, &reason, &sigrc);
249 }
250 while (operating_p && reason == sim_stopped && sigrc != SIGINT);
251#else
c906108c
SS
252 if (trace)
253 {
254 int done = 0;
7fc5b5ad 255 prev_sigint = signal (SIGINT, cntrl_c);
c906108c
SS
256 while (!done)
257 {
258 done = sim_trace (sd);
259 }
7fc5b5ad 260 signal (SIGINT, prev_sigint);
baa7ae6f 261 sim_stop_reason (sd, &reason, &sigrc);
c906108c
SS
262 }
263 else
264 {
baa7ae6f 265 prev_sigint = signal (SIGINT, cntrl_c);
781c552e 266 sigrc = 0;
baa7ae6f
AC
267 sim_resume (sd, 0, sigrc);
268 signal (SIGINT, prev_sigint);
269 sim_stop_reason (sd, &reason, &sigrc);
c906108c 270 }
baa7ae6f 271#endif
c906108c
SS
272
273 if (verbose)
274 sim_info (sd, 0);
c906108c
SS
275 sim_close (sd, 0);
276
277 /* If reason is sim_exited, then sigrc holds the exit code which we want
278 to return. If reason is sim_stopped or sim_signalled, then sigrc holds
279 the signal that the simulator received; we want to return that to
280 indicate failure. */
281
c906108c
SS
282 /* Why did we stop? */
283 switch (reason)
284 {
285 case sim_signalled:
286 case sim_stopped:
287 if (sigrc != 0)
dbec3bef 288 fprintf (stderr, "program stopped with signal %d.\n", sigrc);
c906108c
SS
289 break;
290
291 case sim_exited:
292 break;
293
294 case sim_running:
dbec3bef 295 case sim_polling: /* These indicate a serious problem. */
c906108c
SS
296 abort ();
297 break;
298
299 }
c906108c
SS
300
301 return sigrc;
302}
303
304static void
305usage ()
306{
307 fprintf (stderr, "Usage: %s [options] program [program args]\n", myname);
308 fprintf (stderr, "Options:\n");
309 fprintf (stderr, "-a args Pass `args' to simulator.\n");
310#ifdef SIM_HAVE_SIMCACHE
311 fprintf (stderr, "-c size Set simulator cache size to `size'.\n");
c906108c
SS
312#endif
313 fprintf (stderr, "-m size Set memory size of simulator, in bytes.\n");
baa7ae6f
AC
314#ifdef SIM_HAVE_ENVIRONMENT
315 fprintf (stderr, "-o Select operating (kernel) environment.\n");
316#endif
c906108c
SS
317#ifdef SIM_HAVE_PROFILE
318 fprintf (stderr, "-p freq Set profiling frequency.\n");
319 fprintf (stderr, "-s size Set profiling size.\n");
320#endif
321 fprintf (stderr, "-t Perform instruction tracing.\n");
322 fprintf (stderr, " Note: Very few simulators support tracing.\n");
323 fprintf (stderr, "-v Verbose output.\n");
324 fprintf (stderr, "\n");
325 fprintf (stderr, "program args Arguments to pass to simulated program.\n");
326 fprintf (stderr, " Note: Very few simulators support this.\n");
de4112fa
NC
327#ifdef SIM_TARGET_SWITCHES
328 fprintf (stderr, "\nTarget specific options:\n");
329 sim_target_display_usage ();
330#endif
c906108c
SS
331 exit (1);
332}