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