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