]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - sim/common/run.c
d23b5a0152e4d1f621be7d44a21ecb4655012605
[thirdparty/binutils-gdb.git] / sim / common / run.c
1 /* run front end support for all the simulators.
2 Copyright (C) 1992, 93-96, 1997 Free Software Foundation, Inc.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License along
15 with this program; if not, write to the Free Software Foundation, Inc.,
16 59 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 #include "ansidecl.h"
49
50 #include "../libiberty/alloca-conf.h"
51
52 static void usage PARAMS ((void));
53 extern int optind;
54 extern char *optarg;
55
56 extern host_callback default_callback;
57
58 static char *myname;
59
60 extern int getopt ();
61
62 #ifdef NEED_UI_LOOP_HOOK
63 /* Gdb foolery. This is only needed for gdb using a gui. */
64 int (*ui_loop_hook) PARAMS ((int signo));
65 #endif
66
67 static SIM_DESC sd;
68
69 static RETSIGTYPE
70 cntrl_c (int sig ATTRIBUTE_UNUSED)
71 {
72 if (! sim_stop (sd))
73 {
74 fprintf (stderr, "Quit!\n");
75 exit (1);
76 }
77 }
78
79 int
80 main (ac, av)
81 int ac;
82 char **av;
83 {
84 RETSIGTYPE (*prev_sigint) ();
85 bfd *abfd;
86 int i;
87 int verbose = 0;
88 int trace = 0;
89 #ifdef SIM_HAVE_ENVIRONMENT
90 int operating_p = 0;
91 #endif
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:op:s:htv")) != EOF)
121 #else
122 while ((i = getopt (ac, av, "a:c:m:op: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_ENVIRONMENT
151 case 'o':
152 /* Operating enironment where any signals are delivered to the
153 target. */
154 operating_p = 1;
155 break;
156 #endif SIM_HAVE_ENVIRONMENT
157 #ifdef SIM_HAVE_PROFILE
158 case 'p':
159 sim_set_profile (atoi (optarg));
160 break;
161 case 's':
162 sim_set_profile_size (atoi (optarg));
163 break;
164 #endif
165 case 't':
166 trace = 1;
167 break;
168 case 'v':
169 /* Things that are printed with -v are the kinds of things that
170 gcc -v prints. This is not meant to include detailed tracing
171 or debugging information, just summaries. */
172 verbose = 1;
173 /* sim_set_verbose (1); */
174 break;
175 /* FIXME: Quick hack, to be replaced by more general facility. */
176 #ifdef SIM_H8300
177 case 'h':
178 set_h8300h (1);
179 break;
180 #endif
181 default:
182 usage ();
183 }
184
185 ac -= optind;
186 av += optind;
187 if (ac <= 0)
188 usage ();
189
190 name = *av;
191 prog_args = av;
192
193 if (verbose)
194 {
195 printf ("%s %s\n", myname, name);
196 }
197
198 abfd = bfd_openr (name, 0);
199 if (!abfd)
200 {
201 fprintf (stderr, "%s: can't open %s: %s\n",
202 myname, name, bfd_errmsg (bfd_get_error ()));
203 exit (1);
204 }
205
206 if (!bfd_check_format (abfd, bfd_object))
207 {
208 fprintf (stderr, "%s: can't load %s: %s\n",
209 myname, name, bfd_errmsg (bfd_get_error ()));
210 exit (1);
211 }
212
213 #ifdef SIM_HAVE_BIENDIAN
214 /* The endianness must be passed to sim_open because one may wish to
215 examine/set registers before calling sim_load [which is the other
216 place where one can determine endianness]. We previously passed the
217 endianness via global `target_byte_order' but that's not a clean
218 interface. */
219 for (i = 1; sim_argv[i + 1] != NULL; ++i)
220 continue;
221 if (bfd_big_endian (abfd))
222 sim_argv[i] = "big";
223 else
224 sim_argv[i] = "little";
225 #endif
226
227 /* Ensure that any run-time initialisation that needs to be
228 performed by the simulator can occur. */
229 sd = sim_open (SIM_OPEN_STANDALONE, &default_callback, abfd, sim_argv);
230 if (sd == 0)
231 exit (1);
232
233 if (sim_load (sd, name, abfd, 0) == SIM_RC_FAIL)
234 exit (1);
235
236 if (sim_create_inferior (sd, abfd, prog_args, NULL) == SIM_RC_FAIL)
237 exit (1);
238
239 #ifdef SIM_HAVE_ENVIRONMENT
240 /* NOTE: An old simulator supporting the operating environment MUST
241 provide sim_set_trace() and not sim_trace(). That way
242 sim_stop_reason() can be used to determine any stop reason. */
243 if (trace)
244 sim_set_trace ();
245 do
246 {
247 prev_sigint = signal (SIGINT, cntrl_c);
248 sim_resume (sd, 0, sigrc);
249 signal (SIGINT, prev_sigint);
250 sim_stop_reason (sd, &reason, &sigrc);
251 }
252 while (operating_p && reason == sim_stopped && sigrc != SIGINT);
253 #else
254 if (trace)
255 {
256 int done = 0;
257 prev_sigint = signal (SIGINT, cntrl_c);
258 while (!done)
259 {
260 done = sim_trace (sd);
261 }
262 signal (SIGINT, prev_sigint);
263 sim_stop_reason (sd, &reason, &sigrc);
264 }
265 else
266 {
267 prev_sigint = signal (SIGINT, cntrl_c);
268 sim_resume (sd, 0, sigrc);
269 signal (SIGINT, prev_sigint);
270 sim_stop_reason (sd, &reason, &sigrc);
271 }
272 #endif
273
274 if (verbose)
275 sim_info (sd, 0);
276 sim_close (sd, 0);
277
278 /* If reason is sim_exited, then sigrc holds the exit code which we want
279 to return. If reason is sim_stopped or sim_signalled, then sigrc holds
280 the signal that the simulator received; we want to return that to
281 indicate failure. */
282
283 #ifdef SIM_H8300 /* FIXME: Ugh. grep for SLEEP in compile.c */
284 if (sigrc == SIGILL)
285 abort ();
286 sigrc = 0;
287 #else
288 /* Why did we stop? */
289 switch (reason)
290 {
291 case sim_signalled:
292 case sim_stopped:
293 if (sigrc != 0)
294 fprintf (stderr, "program stopped with signal %d.\n", sigrc);
295 break;
296
297 case sim_exited:
298 break;
299
300 case sim_running:
301 case sim_polling: /* these indicate a serious problem */
302 abort ();
303 break;
304
305 }
306 #endif
307
308 return sigrc;
309 }
310
311 static void
312 usage ()
313 {
314 fprintf (stderr, "Usage: %s [options] program [program args]\n", myname);
315 fprintf (stderr, "Options:\n");
316 fprintf (stderr, "-a args Pass `args' to simulator.\n");
317 #ifdef SIM_HAVE_SIMCACHE
318 fprintf (stderr, "-c size Set simulator cache size to `size'.\n");
319 #endif
320 #ifdef SIM_H8300
321 fprintf (stderr, "-h Executable is for h8/300h or h8/300s.\n");
322 #endif
323 fprintf (stderr, "-m size Set memory size of simulator, in bytes.\n");
324 #ifdef SIM_HAVE_ENVIRONMENT
325 fprintf (stderr, "-o Select operating (kernel) environment.\n");
326 #endif
327 #ifdef SIM_HAVE_PROFILE
328 fprintf (stderr, "-p freq Set profiling frequency.\n");
329 fprintf (stderr, "-s size Set profiling size.\n");
330 #endif
331 fprintf (stderr, "-t Perform instruction tracing.\n");
332 fprintf (stderr, " Note: Very few simulators support tracing.\n");
333 fprintf (stderr, "-v Verbose output.\n");
334 fprintf (stderr, "\n");
335 fprintf (stderr, "program args Arguments to pass to simulated program.\n");
336 fprintf (stderr, " Note: Very few simulators support this.\n");
337 exit (1);
338 }