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