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