cb9fdab8ef1dd736b7bfe962b5da91cc947c2b47
[deliverable/binutils-gdb.git] / sim / common / run.c
1 /* run front end support for all the simulators.
2 Copyright (C) 1992, 1993 1994, 1995, 1996 Free Software Foundation, Inc.
3
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
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 static void usage PARAMS ((void));
50 extern int optind;
51 extern char *optarg;
52
53 bfd *exec_bfd;
54
55 int target_byte_order;
56
57 extern host_callback default_callback;
58
59 static char *myname;
60
61
62 /* NOTE: sim_size() and sim_trace() are going away */
63 extern void sim_size PARAMS ((int i));
64 extern int sim_trace PARAMS ((SIM_DESC sd));
65
66 extern int getopt ();
67
68
69 int
70 main (ac, av)
71 int ac;
72 char **av;
73 {
74 bfd *abfd;
75 bfd_vma start_address;
76 asection *s;
77 int i;
78 int verbose = 0;
79 int trace = 0;
80 char *name;
81 static char *no_args[2];
82 char **sim_argv = &no_args[0];
83 char **prog_args;
84 enum sim_stop reason;
85 int sigrc;
86 SIM_DESC sd;
87
88 myname = av[0] + strlen (av[0]);
89 while (myname > av[0] && myname[-1] != '/')
90 --myname;
91
92 /* The first element of sim_open's argv is the program name. */
93 no_args[0] = av[0];
94
95 /* FIXME: This is currently being rewritten to have each simulator
96 do all argv processing. */
97
98 #ifdef SIM_H8300 /* FIXME: quick hack */
99 while ((i = getopt (ac, av, "a:c:m:p:s:htv")) != EOF)
100 #else
101 while ((i = getopt (ac, av, "a:c:m:p:s:tv")) != EOF)
102 #endif
103 switch (i)
104 {
105 case 'a':
106 /* FIXME: Temporary hack. */
107 {
108 int len = strlen (av[0]) + strlen (optarg);
109 char *argbuf = (char *) alloca (len + 2);
110 sprintf (argbuf, "%s %s", av[0], optarg);
111 sim_argv = buildargv (argbuf);
112 }
113 break;
114 #ifdef SIM_HAVE_SIMCACHE
115 case 'c':
116 sim_set_simcache_size (atoi (optarg));
117 break;
118 #endif
119 case 'm':
120 /* FIXME: Rename to sim_set_mem_size. */
121 sim_size (atoi (optarg));
122 break;
123 #ifdef SIM_HAVE_PROFILE
124 case 'p':
125 sim_set_profile (atoi (optarg));
126 break;
127 case 's':
128 sim_set_profile_size (atoi (optarg));
129 break;
130 #endif
131 case 't':
132 trace = 1;
133 /* FIXME: need to allow specification of what to trace. */
134 /* sim_set_trace (1); */
135 break;
136 case 'v':
137 /* Things that are printed with -v are the kinds of things that
138 gcc -v prints. This is not meant to include detailed tracing
139 or debugging information, just summaries. */
140 verbose = 1;
141 /* sim_set_verbose (1); */
142 break;
143 /* FIXME: Quick hack, to be replaced by more general facility. */
144 #ifdef SIM_H8300
145 case 'h':
146 set_h8300h (1);
147 break;
148 #endif
149 default:
150 usage ();
151 }
152
153 ac -= optind;
154 av += optind;
155
156 name = *av;
157 prog_args = av + 1;
158
159 if (verbose)
160 {
161 printf ("%s %s\n", myname, name);
162 }
163
164 exec_bfd = abfd = bfd_openr (name, 0);
165 if (!abfd)
166 {
167 fprintf (stderr, "%s: can't open %s: %s\n",
168 myname, name, bfd_errmsg (bfd_get_error ()));
169 exit (1);
170 }
171
172 if (!bfd_check_format (abfd, bfd_object))
173 {
174 fprintf (stderr, "%s: can't load %s: %s\n",
175 myname, name, bfd_errmsg (bfd_get_error ()));
176 exit (1);
177 }
178
179 /* This must be set before sim_open is called, because gdb assumes that
180 the simulator endianness is known immediately after the sim_open call. */
181 target_byte_order = bfd_big_endian (abfd) ? 4321 : 1234;
182
183 sim_set_callbacks (NULL, &default_callback);
184 default_callback.init (&default_callback);
185
186 /* Ensure that any run-time initialisation that needs to be
187 performed by the simulator can occur. */
188 sd = sim_open (sim_argv);
189
190 for (s = abfd->sections; s; s = s->next)
191 {
192 if (s->flags & SEC_LOAD)
193 {
194 unsigned char *buffer = (unsigned char *)malloc ((size_t)(bfd_section_size (abfd, s)));
195 if (buffer != NULL)
196 {
197 bfd_get_section_contents (abfd,
198 s,
199 buffer,
200 0,
201 bfd_section_size (abfd, s));
202 sim_write (sd, s->vma, buffer, bfd_section_size (abfd, s));
203 /* FIXME: How come we don't free buffer? */
204 }
205 else
206 {
207 fprintf (stderr, "%s: failed to allocate section buffer: %s\n",
208 myname, bfd_errmsg (bfd_get_error ()));
209 exit (1);
210 }
211 }
212 }
213
214 start_address = bfd_get_start_address (abfd);
215 sim_create_inferior (sd, start_address, prog_args, NULL);
216
217 if (trace)
218 {
219 int done = 0;
220 while (!done)
221 {
222 done = sim_trace (sd);
223 }
224 }
225 else
226 {
227 sim_resume (sd, 0, 0);
228 }
229 if (verbose)
230 sim_info (sd, 0);
231
232 sim_stop_reason (sd, &reason, &sigrc);
233
234 sim_close (sd, 0);
235
236 /* If reason is sim_exited, then sigrc holds the exit code which we want
237 to return. If reason is sim_stopped or sim_signalled, then sigrc holds
238 the signal that the simulator received; we want to return that to
239 indicate failure. */
240
241 #ifdef SIM_H8300 /* FIXME: Ugh. grep for SLEEP in compile.c */
242 if (sigrc == SIGILL)
243 abort ();
244 sigrc = 0;
245 #else
246 /* Why did we stop? */
247 switch (reason)
248 {
249 case sim_signalled:
250 case sim_stopped:
251 if (sigrc != 0)
252 fprintf (stderr, "program stopped with signal %d.\n", sigrc);
253 break;
254
255 case sim_exited:
256 break;
257 }
258 #endif
259
260 return sigrc;
261 }
262
263 static void
264 usage ()
265 {
266 fprintf (stderr, "Usage: %s [options] program [program args]\n", myname);
267 fprintf (stderr, "Options:\n");
268 fprintf (stderr, "-a args Pass `args' to simulator.\n");
269 #ifdef SIM_HAVE_SIMCACHE
270 fprintf (stderr, "-c size Set simulator cache size to `size'.\n");
271 #endif
272 #ifdef SIM_H8300
273 fprintf (stderr, "-h Executable is for h8/300h or h8/300s.\n");
274 #endif
275 fprintf (stderr, "-m size Set memory size of simulator, in bytes.\n");
276 #ifdef SIM_HAVE_PROFILE
277 fprintf (stderr, "-p freq Set profiling frequency.\n");
278 fprintf (stderr, "-s size Set profiling size.\n");
279 #endif
280 fprintf (stderr, "-t Perform instruction tracing.\n");
281 fprintf (stderr, " Note: Very few simulators support tracing.\n");
282 fprintf (stderr, "-v Verbose output.\n");
283 fprintf (stderr, "\n");
284 fprintf (stderr, "program args Arguments to pass to simulated program.\n");
285 fprintf (stderr, " Note: Very few simulators support this.\n");
286 exit (1);
287 }
This page took 0.046957 seconds and 3 git commands to generate.