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