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