Ref gdb/11763 - can't stop a running simulator:
[deliverable/binutils-gdb.git] / sim / common / run.c
CommitLineData
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 4This program is free software; you can redistribute it and/or modify
6834d493
JW
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation; either version 2, or (at your option)
7any later version.
8
75cfb486 9This program is distributed in the hope that it will be useful,
6834d493
JW
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
75cfb486
DE
14You should have received a copy of the GNU General Public License along
15with this program; if not, write to the Free Software Foundation, Inc.,
1659 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 51static void usage PARAMS ((void));
6834d493
JW
52extern int optind;
53extern char *optarg;
54
6834d493 55extern host_callback default_callback;
fa21d299
AC
56
57static char *myname;
58
59
60/* NOTE: sim_size() and sim_trace() are going away */
61extern void sim_size PARAMS ((int i));
62extern int sim_trace PARAMS ((SIM_DESC sd));
63
64extern int getopt ();
65
8517f62b
AC
66static SIM_DESC sd;
67
68static RETSIGTYPE
69cntrl_c (int sig)
70{
71 if (! sim_stop (sd))
72 {
73 fprintf (stderr, "Quit!\n");
74 exit (1);
75 }
76}
fa21d299 77
6834d493
JW
78int
79main (ac, av)
80 int ac;
81 char **av;
82{
8517f62b 83 RETSIGTYPE (*prev_sigint) ();
6834d493 84 bfd *abfd;
6834d493
JW
85 asection *s;
86 int i;
87 int verbose = 0;
88 int trace = 0;
fa21d299 89 char *name;
0f2811d1 90 static char *no_args[4];
fa21d299
AC
91 char **sim_argv = &no_args[0];
92 char **prog_args;
6834d493
JW
93 enum sim_stop reason;
94 int sigrc;
fa21d299
AC
95
96 myname = av[0] + strlen (av[0]);
97 while (myname > av[0] && myname[-1] != '/')
98 --myname;
6834d493 99
fa21d299
AC
100 /* The first element of sim_open's argv is the program name. */
101 no_args[0] = av[0];
0f2811d1
DE
102 no_args[1] = "-E";
103 no_args[2] = "set-later";
fa21d299
AC
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
6834d493
JW
113 switch (i)
114 {
fa21d299
AC
115 case 'a':
116 /* FIXME: Temporary hack. */
117 {
118 int len = strlen (av[0]) + strlen (optarg);
0f2811d1
DE
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);
fa21d299
AC
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
6834d493 132 case 'm':
fa21d299 133 /* FIXME: Rename to sim_set_mem_size. */
6834d493
JW
134 sim_size (atoi (optarg));
135 break;
fa21d299 136#ifdef SIM_HAVE_PROFILE
6834d493
JW
137 case 'p':
138 sim_set_profile (atoi (optarg));
139 break;
140 case 's':
141 sim_set_profile_size (atoi (optarg));
142 break;
fa21d299 143#endif
6834d493
JW
144 case 't':
145 trace = 1;
fa21d299
AC
146 /* FIXME: need to allow specification of what to trace. */
147 /* sim_set_trace (1); */
6834d493
JW
148 break;
149 case 'v':
fa21d299
AC
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. */
6834d493 153 verbose = 1;
fa21d299 154 /* sim_set_verbose (1); */
6834d493 155 break;
fa21d299
AC
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
6834d493 162 default:
fa21d299 163 usage ();
6834d493 164 }
fa21d299 165
6834d493
JW
166 ac -= optind;
167 av += optind;
0f2811d1
DE
168 if (ac <= 0)
169 usage ();
6834d493 170
6834d493 171 name = *av;
0f2811d1 172 prog_args = av;
6834d493
JW
173
174 if (verbose)
175 {
fa21d299 176 printf ("%s %s\n", myname, name);
6834d493 177 }
3be50301 178
0f2811d1
DE
179 sim_set_callbacks (NULL, &default_callback);
180 default_callback.init (&default_callback);
181
182 abfd = bfd_openr (name, 0);
3be50301
JW
183 if (!abfd)
184 {
fa21d299
AC
185 fprintf (stderr, "%s: can't open %s: %s\n",
186 myname, name, bfd_errmsg (bfd_get_error ()));
3be50301
JW
187 exit (1);
188 }
189
190 if (!bfd_check_format (abfd, bfd_object))
191 {
fa21d299
AC
192 fprintf (stderr, "%s: can't load %s: %s\n",
193 myname, name, bfd_errmsg (bfd_get_error ()));
3be50301
JW
194 exit (1);
195 }
196
0f2811d1
DE
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";
3be50301 208
9b280a86
MM
209 /* Ensure that any run-time initialisation that needs to be
210 performed by the simulator can occur. */
bcd1475a 211 sd = sim_open (SIM_OPEN_STANDALONE, sim_argv);
f7d2f538
DE
212 if (sd == 0)
213 exit (1);
9b280a86 214
0f2811d1
DE
215 if (sim_load (sd, name, abfd, 0) == SIM_RC_FAIL)
216 exit (1);
3be50301 217
0f2811d1
DE
218 if (sim_create_inferior (sd, prog_args, NULL) == SIM_RC_FAIL)
219 exit (1);
6834d493 220
8517f62b 221 prev_sigint = signal (SIGINT, cntrl_c);
3be50301
JW
222 if (trace)
223 {
224 int done = 0;
225 while (!done)
226 {
fa21d299 227 done = sim_trace (sd);
6834d493
JW
228 }
229 }
3be50301
JW
230 else
231 {
fa21d299 232 sim_resume (sd, 0, 0);
3be50301 233 }
8517f62b 234 signal (SIGINT, prev_sigint);
0f2811d1 235
3be50301 236 if (verbose)
fa21d299 237 sim_info (sd, 0);
3be50301 238
fa21d299 239 sim_stop_reason (sd, &reason, &sigrc);
6834d493 240
fa21d299 241 sim_close (sd, 0);
9b280a86 242
fa21d299
AC
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
57bc1a72
MM
253 /* Why did we stop? */
254 switch (reason)
255 {
256 case sim_signalled:
257 case sim_stopped:
fa21d299
AC
258 if (sigrc != 0)
259 fprintf (stderr, "program stopped with signal %d.\n", sigrc);
57bc1a72
MM
260 break;
261
262 case sim_exited:
263 break;
264 }
fa21d299 265#endif
57bc1a72 266
3be50301 267 return sigrc;
6834d493
JW
268}
269
fa21d299
AC
270static void
271usage ()
6834d493 272{
fa21d299
AC
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");
6834d493
JW
293 exit (1);
294}
This page took 0.085461 seconds and 4 git commands to generate.