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