Restore exec_bfd, needed for v850 debug code
[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
49 #include "../libiberty/alloca-conf.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 /* bfd descriptor of the executable. Same name as gdb uses. */
60 bfd *exec_bfd;
61
62
63 /* NOTE: sim_size() and sim_trace() are going away */
64 extern void sim_size PARAMS ((int i));
65 extern int sim_trace PARAMS ((SIM_DESC sd));
66
67 extern int getopt ();
68
69 static SIM_DESC sd;
70
71 static RETSIGTYPE
72 cntrl_c (int sig)
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 asection *s;
89 int i;
90 int verbose = 0;
91 int trace = 0;
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 no_args[1] = "-E";
106 no_args[2] = "set-later";
107
108 /* FIXME: This is currently being rewritten to have each simulator
109 do all argv processing. */
110
111 #ifdef SIM_H8300 /* FIXME: quick hack */
112 while ((i = getopt (ac, av, "a:c:m:p:s:htv")) != EOF)
113 #else
114 while ((i = getopt (ac, av, "a:c:m:p:s:tv")) != EOF)
115 #endif
116 switch (i)
117 {
118 case 'a':
119 /* FIXME: Temporary hack. */
120 {
121 int len = strlen (av[0]) + strlen (optarg);
122 char *argbuf = (char *) alloca (len + 2 + 50);
123 /* The desired endianness must be passed to sim_open.
124 The value for "set-later" is set when we know what it is.
125 -e support isn't yet part of the published interface. */
126 sprintf (argbuf, "%s %s -E set-later", av[0], optarg);
127 sim_argv = buildargv (argbuf);
128 }
129 break;
130 #ifdef SIM_HAVE_SIMCACHE
131 case 'c':
132 sim_set_simcache_size (atoi (optarg));
133 break;
134 #endif
135 case 'm':
136 /* FIXME: Rename to sim_set_mem_size. */
137 sim_size (atoi (optarg));
138 break;
139 #ifdef SIM_HAVE_PROFILE
140 case 'p':
141 sim_set_profile (atoi (optarg));
142 break;
143 case 's':
144 sim_set_profile_size (atoi (optarg));
145 break;
146 #endif
147 case 't':
148 trace = 1;
149 /* FIXME: need to allow specification of what to trace. */
150 /* sim_set_trace (1); */
151 break;
152 case 'v':
153 /* Things that are printed with -v are the kinds of things that
154 gcc -v prints. This is not meant to include detailed tracing
155 or debugging information, just summaries. */
156 verbose = 1;
157 /* sim_set_verbose (1); */
158 break;
159 /* FIXME: Quick hack, to be replaced by more general facility. */
160 #ifdef SIM_H8300
161 case 'h':
162 set_h8300h (1);
163 break;
164 #endif
165 default:
166 usage ();
167 }
168
169 ac -= optind;
170 av += optind;
171 if (ac <= 0)
172 usage ();
173
174 name = *av;
175 prog_args = av;
176
177 if (verbose)
178 {
179 printf ("%s %s\n", myname, name);
180 }
181
182 sim_set_callbacks (NULL, &default_callback);
183 default_callback.init (&default_callback);
184
185 exec_bfd = abfd = bfd_openr (name, 0);
186 if (!abfd)
187 {
188 fprintf (stderr, "%s: can't open %s: %s\n",
189 myname, name, bfd_errmsg (bfd_get_error ()));
190 exit (1);
191 }
192
193 if (!bfd_check_format (abfd, bfd_object))
194 {
195 fprintf (stderr, "%s: can't load %s: %s\n",
196 myname, name, bfd_errmsg (bfd_get_error ()));
197 exit (1);
198 }
199
200 /* The endianness must be passed to sim_open because one may wish to
201 examine/set registers before calling sim_load [which is the other
202 place where one can determine endianness]. We previously passed the
203 endianness via global `target_byte_order' but that's not a clean
204 interface. */
205 for (i = 1; sim_argv[i + 1] != NULL; ++i)
206 continue;
207 if (bfd_big_endian (abfd))
208 sim_argv[i] = "big";
209 else
210 sim_argv[i] = "little";
211
212 /* Ensure that any run-time initialisation that needs to be
213 performed by the simulator can occur. */
214 sd = sim_open (SIM_OPEN_STANDALONE, sim_argv);
215 if (sd == 0)
216 exit (1);
217
218 if (sim_load (sd, name, abfd, 0) == SIM_RC_FAIL)
219 exit (1);
220
221 if (sim_create_inferior (sd, prog_args, NULL) == SIM_RC_FAIL)
222 exit (1);
223
224 prev_sigint = signal (SIGINT, cntrl_c);
225 if (trace)
226 {
227 int done = 0;
228 while (!done)
229 {
230 done = sim_trace (sd);
231 }
232 }
233 else
234 {
235 sim_resume (sd, 0, 0);
236 }
237 signal (SIGINT, prev_sigint);
238
239 if (verbose)
240 sim_info (sd, 0);
241
242 sim_stop_reason (sd, &reason, &sigrc);
243
244 sim_close (sd, 0);
245
246 /* If reason is sim_exited, then sigrc holds the exit code which we want
247 to return. If reason is sim_stopped or sim_signalled, then sigrc holds
248 the signal that the simulator received; we want to return that to
249 indicate failure. */
250
251 #ifdef SIM_H8300 /* FIXME: Ugh. grep for SLEEP in compile.c */
252 if (sigrc == SIGILL)
253 abort ();
254 sigrc = 0;
255 #else
256 /* Why did we stop? */
257 switch (reason)
258 {
259 case sim_signalled:
260 case sim_stopped:
261 if (sigrc != 0)
262 fprintf (stderr, "program stopped with signal %d.\n", sigrc);
263 break;
264
265 case sim_exited:
266 break;
267 }
268 #endif
269
270 return sigrc;
271 }
272
273 static void
274 usage ()
275 {
276 fprintf (stderr, "Usage: %s [options] program [program args]\n", myname);
277 fprintf (stderr, "Options:\n");
278 fprintf (stderr, "-a args Pass `args' to simulator.\n");
279 #ifdef SIM_HAVE_SIMCACHE
280 fprintf (stderr, "-c size Set simulator cache size to `size'.\n");
281 #endif
282 #ifdef SIM_H8300
283 fprintf (stderr, "-h Executable is for h8/300h or h8/300s.\n");
284 #endif
285 fprintf (stderr, "-m size Set memory size of simulator, in bytes.\n");
286 #ifdef SIM_HAVE_PROFILE
287 fprintf (stderr, "-p freq Set profiling frequency.\n");
288 fprintf (stderr, "-s size Set profiling size.\n");
289 #endif
290 fprintf (stderr, "-t Perform instruction tracing.\n");
291 fprintf (stderr, " Note: Very few simulators support tracing.\n");
292 fprintf (stderr, "-v Verbose output.\n");
293 fprintf (stderr, "\n");
294 fprintf (stderr, "program args Arguments to pass to simulated program.\n");
295 fprintf (stderr, " Note: Very few simulators support this.\n");
296 exit (1);
297 }