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