This commit was generated by cvs2svn to track changes on a CVS vendor
[external/binutils.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 int sim_trace PARAMS ((SIM_DESC sd));
62
63 extern int getopt ();
64
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 }
76
77 int
78 main (ac, av)
79      int ac;
80      char **av;
81 {
82   RETSIGTYPE (*prev_sigint) ();
83   bfd *abfd;
84   asection *s;
85   int i;
86   int verbose = 0;
87   int trace = 0;
88   char *name;
89   static char *no_args[4];
90   char **sim_argv = &no_args[0];
91   char **prog_args;
92   enum sim_stop reason;
93   int sigrc;
94
95   myname = av[0] + strlen (av[0]);
96   while (myname > av[0] && myname[-1] != '/')
97     --myname;
98
99   /* The first element of sim_open's argv is the program name.  */
100   no_args[0] = av[0];
101 #ifdef SIM_HAVE_BIENDIAN
102   no_args[1] = "-E";
103   no_args[2] = "set-later";
104 #endif
105
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
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
120     switch (i)
121       {
122       case 'a':
123         /* FIXME: Temporary hack.  */
124         {
125           int len = strlen (av[0]) + strlen (optarg);
126           char *argbuf = (char *) alloca (len + 2 + 50);
127           sprintf (argbuf, "%s %s", av[0], optarg);
128 #ifdef SIM_HAVE_BIENDIAN
129           /* The desired endianness must be passed to sim_open.
130              The value for "set-later" is set when we know what it is.
131              -E support isn't yet part of the published interface.  */
132           strcat (argbuf, " -E set-later");
133 #endif
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
142       case 'm':
143         /* FIXME: Rename to sim_set_mem_size.  */
144         sim_size (atoi (optarg));
145         break;
146 #ifdef SIM_HAVE_PROFILE
147       case 'p':
148         sim_set_profile (atoi (optarg));
149         break;
150       case 's':
151         sim_set_profile_size (atoi (optarg));
152         break;
153 #endif
154       case 't':
155         trace = 1;
156         /* FIXME: need to allow specification of what to trace.  */
157         /* sim_set_trace (1); */
158         break;
159       case 'v':
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.  */
163         verbose = 1;
164         /* sim_set_verbose (1); */
165         break;
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
172       default:
173         usage ();
174       }
175
176   ac -= optind;
177   av += optind;
178   if (ac <= 0)
179     usage ();
180
181   name = *av;
182   prog_args = av;
183
184   if (verbose)
185     {
186       printf ("%s %s\n", myname, name);
187     }
188
189   abfd = bfd_openr (name, 0);
190   if (!abfd) 
191     {
192       fprintf (stderr, "%s: can't open %s: %s\n", 
193                myname, name, bfd_errmsg (bfd_get_error ()));
194       exit (1);
195     }
196
197   if (!bfd_check_format (abfd, bfd_object))
198     {
199       fprintf (stderr, "%s: can't load %s: %s\n",
200                myname, name, bfd_errmsg (bfd_get_error ()));
201       exit (1);
202     }
203
204 #ifdef SIM_HAVE_BIENDIAN
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";
216 #endif
217
218   /* Ensure that any run-time initialisation that needs to be
219      performed by the simulator can occur. */
220   sd = sim_open (SIM_OPEN_STANDALONE, &default_callback, abfd, sim_argv);
221   if (sd == 0)
222     exit (1);
223
224   if (sim_load (sd, name, abfd, 0) == SIM_RC_FAIL)
225     exit (1);
226
227   if (sim_create_inferior (sd, prog_args, NULL) == SIM_RC_FAIL)
228     exit (1);
229
230   prev_sigint = signal (SIGINT, cntrl_c);
231   if (trace)
232     {
233       int done = 0;
234       while (!done)
235         {
236           done = sim_trace (sd);
237         }
238     }
239   else
240     {
241       sim_resume (sd, 0, 0);
242     }
243   signal (SIGINT, prev_sigint);
244
245   if (verbose)
246     sim_info (sd, 0);
247
248   sim_stop_reason (sd, &reason, &sigrc);
249
250   sim_close (sd, 0);
251
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
262   /* Why did we stop? */
263   switch (reason)
264     {
265     case sim_signalled:
266     case sim_stopped:
267       if (sigrc != 0)
268         fprintf (stderr, "program stopped with signal %d.\n", sigrc);
269       break;
270
271     case sim_exited:
272       break;
273     }
274 #endif
275
276   return sigrc;
277 }
278
279 static void
280 usage ()
281 {
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");
302   exit (1);
303 }