Commit | Line | Data |
---|---|---|
6834d493 | 1 | /* run front end support for all the simulators. |
0f2811d1 | 2 | Copyright (C) 1992, 93-96, 1997 Free Software Foundation, Inc. |
6834d493 | 3 | |
75cfb486 | 4 | This program is free software; you can redistribute it and/or modify |
6834d493 JW |
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 | ||
75cfb486 | 9 | This program is distributed in the hope that it will be useful, |
6834d493 JW |
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 | ||
75cfb486 DE |
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. */ | |
6834d493 | 17 | |
fa21d299 AC |
18 | /* Steve Chamberlain sac@cygnus.com, |
19 | and others at Cygnus. */ | |
6834d493 | 20 | |
fa21d299 AC |
21 | #include "config.h" |
22 | #include "tconfig.h" | |
6834d493 JW |
23 | |
24 | #include <signal.h> | |
25 | #include <stdio.h> | |
fa21d299 AC |
26 | #ifdef __STDC__ |
27 | #include <stdarg.h> | |
28 | #else | |
6834d493 | 29 | #include <varargs.h> |
fa21d299 AC |
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" | |
6834d493 | 45 | #include "bfd.h" |
6834d493 | 46 | #include "callback.h" |
fa21d299 | 47 | #include "remote-sim.h" |
6834d493 | 48 | |
bcd1475a DE |
49 | #include "../libiberty/alloca-conf.h" |
50 | ||
fa21d299 | 51 | static void usage PARAMS ((void)); |
6834d493 JW |
52 | extern int optind; |
53 | extern char *optarg; | |
54 | ||
6834d493 | 55 | extern host_callback default_callback; |
fa21d299 AC |
56 | |
57 | static char *myname; | |
58 | ||
59 | ||
60 | /* NOTE: sim_size() and sim_trace() are going away */ | |
fa21d299 AC |
61 | extern int sim_trace PARAMS ((SIM_DESC sd)); |
62 | ||
63 | extern int getopt (); | |
64 | ||
8517f62b AC |
65 | static SIM_DESC sd; |
66 | ||
67 | static RETSIGTYPE | |
68 | cntrl_c (int sig) | |
69 | { | |
70 | if (! sim_stop (sd)) | |
71 | { | |
72 | fprintf (stderr, "Quit!\n"); | |
73 | exit (1); | |
74 | } | |
75 | } | |
fa21d299 | 76 | |
6834d493 JW |
77 | int |
78 | main (ac, av) | |
79 | int ac; | |
80 | char **av; | |
81 | { | |
8517f62b | 82 | RETSIGTYPE (*prev_sigint) (); |
6834d493 | 83 | bfd *abfd; |
6834d493 JW |
84 | asection *s; |
85 | int i; | |
86 | int verbose = 0; | |
87 | int trace = 0; | |
fa21d299 | 88 | char *name; |
0f2811d1 | 89 | static char *no_args[4]; |
fa21d299 AC |
90 | char **sim_argv = &no_args[0]; |
91 | char **prog_args; | |
6834d493 JW |
92 | enum sim_stop reason; |
93 | int sigrc; | |
fa21d299 AC |
94 | |
95 | myname = av[0] + strlen (av[0]); | |
96 | while (myname > av[0] && myname[-1] != '/') | |
97 | --myname; | |
6834d493 | 98 | |
fa21d299 AC |
99 | /* The first element of sim_open's argv is the program name. */ |
100 | no_args[0] = av[0]; | |
24aa2b57 | 101 | #ifdef SIM_HAVE_BIENDIAN |
0f2811d1 DE |
102 | no_args[1] = "-E"; |
103 | no_args[2] = "set-later"; | |
24aa2b57 | 104 | #endif |
fa21d299 | 105 | |
247fccde AC |
106 | /* FIXME: This is currently being migrated into sim_open. |
107 | Simulators that use functions such as sim_size() still require | |
108 | this. */ | |
109 | default_callback.init (&default_callback); | |
110 | sim_set_callbacks (&default_callback); | |
111 | ||
fa21d299 AC |
112 | /* FIXME: This is currently being rewritten to have each simulator |
113 | do all argv processing. */ | |
114 | ||
115 | #ifdef SIM_H8300 /* FIXME: quick hack */ | |
116 | while ((i = getopt (ac, av, "a:c:m:p:s:htv")) != EOF) | |
117 | #else | |
118 | while ((i = getopt (ac, av, "a:c:m:p:s:tv")) != EOF) | |
119 | #endif | |
6834d493 JW |
120 | switch (i) |
121 | { | |
fa21d299 AC |
122 | case 'a': |
123 | /* FIXME: Temporary hack. */ | |
124 | { | |
125 | int len = strlen (av[0]) + strlen (optarg); | |
0f2811d1 | 126 | char *argbuf = (char *) alloca (len + 2 + 50); |
24aa2b57 AC |
127 | sprintf (argbuf, "%s %s", av[0], optarg); |
128 | #ifdef SIM_HAVE_BIENDIAN | |
0f2811d1 DE |
129 | /* The desired endianness must be passed to sim_open. |
130 | The value for "set-later" is set when we know what it is. | |
24aa2b57 AC |
131 | -E support isn't yet part of the published interface. */ |
132 | strcat (argbuf, " -E set-later"); | |
133 | #endif | |
fa21d299 AC |
134 | sim_argv = buildargv (argbuf); |
135 | } | |
136 | break; | |
137 | #ifdef SIM_HAVE_SIMCACHE | |
138 | case 'c': | |
139 | sim_set_simcache_size (atoi (optarg)); | |
140 | break; | |
141 | #endif | |
6834d493 | 142 | case 'm': |
fa21d299 | 143 | /* FIXME: Rename to sim_set_mem_size. */ |
6834d493 JW |
144 | sim_size (atoi (optarg)); |
145 | break; | |
fa21d299 | 146 | #ifdef SIM_HAVE_PROFILE |
6834d493 JW |
147 | case 'p': |
148 | sim_set_profile (atoi (optarg)); | |
149 | break; | |
150 | case 's': | |
151 | sim_set_profile_size (atoi (optarg)); | |
152 | break; | |
fa21d299 | 153 | #endif |
6834d493 JW |
154 | case 't': |
155 | trace = 1; | |
fa21d299 AC |
156 | /* FIXME: need to allow specification of what to trace. */ |
157 | /* sim_set_trace (1); */ | |
6834d493 JW |
158 | break; |
159 | case 'v': | |
fa21d299 AC |
160 | /* Things that are printed with -v are the kinds of things that |
161 | gcc -v prints. This is not meant to include detailed tracing | |
162 | or debugging information, just summaries. */ | |
6834d493 | 163 | verbose = 1; |
fa21d299 | 164 | /* sim_set_verbose (1); */ |
6834d493 | 165 | break; |
fa21d299 AC |
166 | /* FIXME: Quick hack, to be replaced by more general facility. */ |
167 | #ifdef SIM_H8300 | |
168 | case 'h': | |
169 | set_h8300h (1); | |
170 | break; | |
171 | #endif | |
6834d493 | 172 | default: |
fa21d299 | 173 | usage (); |
6834d493 | 174 | } |
fa21d299 | 175 | |
6834d493 JW |
176 | ac -= optind; |
177 | av += optind; | |
0f2811d1 DE |
178 | if (ac <= 0) |
179 | usage (); | |
6834d493 | 180 | |
6834d493 | 181 | name = *av; |
0f2811d1 | 182 | prog_args = av; |
6834d493 JW |
183 | |
184 | if (verbose) | |
185 | { | |
fa21d299 | 186 | printf ("%s %s\n", myname, name); |
6834d493 | 187 | } |
3be50301 | 188 | |
24aa2b57 | 189 | abfd = bfd_openr (name, 0); |
3be50301 JW |
190 | if (!abfd) |
191 | { | |
fa21d299 AC |
192 | fprintf (stderr, "%s: can't open %s: %s\n", |
193 | myname, name, bfd_errmsg (bfd_get_error ())); | |
3be50301 JW |
194 | exit (1); |
195 | } | |
196 | ||
197 | if (!bfd_check_format (abfd, bfd_object)) | |
198 | { | |
fa21d299 AC |
199 | fprintf (stderr, "%s: can't load %s: %s\n", |
200 | myname, name, bfd_errmsg (bfd_get_error ())); | |
3be50301 JW |
201 | exit (1); |
202 | } | |
203 | ||
24aa2b57 | 204 | #ifdef SIM_HAVE_BIENDIAN |
0f2811d1 DE |
205 | /* The endianness must be passed to sim_open because one may wish to |
206 | examine/set registers before calling sim_load [which is the other | |
207 | place where one can determine endianness]. We previously passed the | |
208 | endianness via global `target_byte_order' but that's not a clean | |
209 | interface. */ | |
210 | for (i = 1; sim_argv[i + 1] != NULL; ++i) | |
211 | continue; | |
212 | if (bfd_big_endian (abfd)) | |
213 | sim_argv[i] = "big"; | |
214 | else | |
215 | sim_argv[i] = "little"; | |
24aa2b57 | 216 | #endif |
3be50301 | 217 | |
9b280a86 MM |
218 | /* Ensure that any run-time initialisation that needs to be |
219 | performed by the simulator can occur. */ | |
247fccde | 220 | sd = sim_open (SIM_OPEN_STANDALONE, &default_callback, abfd, sim_argv); |
f7d2f538 DE |
221 | if (sd == 0) |
222 | exit (1); | |
9b280a86 | 223 | |
0f2811d1 DE |
224 | if (sim_load (sd, name, abfd, 0) == SIM_RC_FAIL) |
225 | exit (1); | |
3be50301 | 226 | |
0f2811d1 DE |
227 | if (sim_create_inferior (sd, prog_args, NULL) == SIM_RC_FAIL) |
228 | exit (1); | |
6834d493 | 229 | |
8517f62b | 230 | prev_sigint = signal (SIGINT, cntrl_c); |
3be50301 JW |
231 | if (trace) |
232 | { | |
233 | int done = 0; | |
234 | while (!done) | |
235 | { | |
fa21d299 | 236 | done = sim_trace (sd); |
6834d493 JW |
237 | } |
238 | } | |
3be50301 JW |
239 | else |
240 | { | |
fa21d299 | 241 | sim_resume (sd, 0, 0); |
3be50301 | 242 | } |
8517f62b | 243 | signal (SIGINT, prev_sigint); |
0f2811d1 | 244 | |
3be50301 | 245 | if (verbose) |
fa21d299 | 246 | sim_info (sd, 0); |
3be50301 | 247 | |
fa21d299 | 248 | sim_stop_reason (sd, &reason, &sigrc); |
6834d493 | 249 | |
fa21d299 | 250 | sim_close (sd, 0); |
9b280a86 | 251 | |
fa21d299 AC |
252 | /* If reason is sim_exited, then sigrc holds the exit code which we want |
253 | to return. If reason is sim_stopped or sim_signalled, then sigrc holds | |
254 | the signal that the simulator received; we want to return that to | |
255 | indicate failure. */ | |
256 | ||
257 | #ifdef SIM_H8300 /* FIXME: Ugh. grep for SLEEP in compile.c */ | |
258 | if (sigrc == SIGILL) | |
259 | abort (); | |
260 | sigrc = 0; | |
261 | #else | |
57bc1a72 MM |
262 | /* Why did we stop? */ |
263 | switch (reason) | |
264 | { | |
265 | case sim_signalled: | |
266 | case sim_stopped: | |
fa21d299 AC |
267 | if (sigrc != 0) |
268 | fprintf (stderr, "program stopped with signal %d.\n", sigrc); | |
57bc1a72 MM |
269 | break; |
270 | ||
271 | case sim_exited: | |
272 | break; | |
273 | } | |
fa21d299 | 274 | #endif |
57bc1a72 | 275 | |
3be50301 | 276 | return sigrc; |
6834d493 JW |
277 | } |
278 | ||
fa21d299 AC |
279 | static void |
280 | usage () | |
6834d493 | 281 | { |
fa21d299 AC |
282 | fprintf (stderr, "Usage: %s [options] program [program args]\n", myname); |
283 | fprintf (stderr, "Options:\n"); | |
284 | fprintf (stderr, "-a args Pass `args' to simulator.\n"); | |
285 | #ifdef SIM_HAVE_SIMCACHE | |
286 | fprintf (stderr, "-c size Set simulator cache size to `size'.\n"); | |
287 | #endif | |
288 | #ifdef SIM_H8300 | |
289 | fprintf (stderr, "-h Executable is for h8/300h or h8/300s.\n"); | |
290 | #endif | |
291 | fprintf (stderr, "-m size Set memory size of simulator, in bytes.\n"); | |
292 | #ifdef SIM_HAVE_PROFILE | |
293 | fprintf (stderr, "-p freq Set profiling frequency.\n"); | |
294 | fprintf (stderr, "-s size Set profiling size.\n"); | |
295 | #endif | |
296 | fprintf (stderr, "-t Perform instruction tracing.\n"); | |
297 | fprintf (stderr, " Note: Very few simulators support tracing.\n"); | |
298 | fprintf (stderr, "-v Verbose output.\n"); | |
299 | fprintf (stderr, "\n"); | |
300 | fprintf (stderr, "program args Arguments to pass to simulated program.\n"); | |
301 | fprintf (stderr, " Note: Very few simulators support this.\n"); | |
6834d493 JW |
302 | exit (1); |
303 | } |