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