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