1/* run front end support for all the simulators.
2   Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
3   2002, 2003, 2004, 2007, 2008, 2009, 2010, 2011
4   Free Software Foundation, Inc.
5
6This program is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 3 of the License, or
9(at your option) any later version.
10
11This program is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
18
19/* Steve Chamberlain sac@cygnus.com,
20   and others at Cygnus.  */
21
22#ifdef HAVE_CONFIG_H
23#include "cconfig.h"
24#include "tconfig.h"
25#endif
26
27#include <signal.h>
28#include <stdio.h>
29#ifdef __STDC__
30#include <stdarg.h>
31#else
32#include <varargs.h>
33#endif
34
35#ifdef HAVE_STDLIB_H
36#include <stdlib.h>
37#endif
38
39#ifdef HAVE_STRING_H
40#include <string.h>
41#else
42#ifdef HAVE_STRINGS_H
43#include <strings.h>
44#endif
45#endif
46
47#include "libiberty.h"
48#include "bfd.h"
49#include "gdb/callback.h"
50#include "gdb/remote-sim.h"
51#include "ansidecl.h"
52#include "run-sim.h"
53#include "version.h"
54
55static void usage PARAMS ((int help));
56static void print_version PARAMS ((void));
57extern int optind;
58extern char *optarg;
59
60extern host_callback default_callback;
61
62static char *myname;
63
64extern int getopt ();
65
66#ifdef NEED_UI_LOOP_HOOK
67/* Gdb foolery. This is only needed for gdb using a gui.  */
68int (*deprecated_ui_loop_hook) PARAMS ((int signo));
69#endif
70
71static SIM_DESC sd;
72
73static RETSIGTYPE
74cntrl_c (int sig ATTRIBUTE_UNUSED)
75{
76  if (! sim_stop (sd))
77    {
78      fprintf (stderr, "Quit!\n");
79      exit (1);
80    }
81}
82
83int
84main (ac, av)
85     int ac;
86     char **av;
87{
88  RETSIGTYPE (*prev_sigint) ();
89  bfd *abfd;
90  int i;
91  int verbose = 0;
92  int trace = 0;
93#ifdef SIM_HAVE_ENVIRONMENT
94  int operating_p = 0;
95#endif
96  char *name;
97  static char *no_args[4];
98  char **sim_argv = &no_args[0];
99  char **prog_args;
100  enum sim_stop reason;
101  int sigrc;
102
103  myname = av[0] + strlen (av[0]);
104  while (myname > av[0] && myname[-1] != '/')
105    --myname;
106
107  /* The first element of sim_open's argv is the program name.  */
108  no_args[0] = av[0];
109#ifdef SIM_HAVE_BIENDIAN
110  no_args[1] = "-E";
111  no_args[2] = "set-later";
112#endif
113
114  /* FIXME: This is currently being migrated into sim_open.
115     Simulators that use functions such as sim_size() still require
116     this.  */
117  default_callback.init (&default_callback);
118  sim_set_callbacks (&default_callback);
119
120#ifdef SIM_TARGET_SWITCHES
121  ac = sim_target_parse_command_line (ac, av);
122#endif
123
124  for (i = 1; av[i]; ++i)
125    {
126      if (strcmp (av[i], "--help") == 0)
127        {
128          usage (1);
129        }
130      else if (strcmp (av[i], "--version") == 0)
131        {
132          print_version ();
133          return 0;
134        }
135    }
136
137  /* FIXME: This is currently being rewritten to have each simulator
138     do all argv processing.  */
139
140  while ((i = getopt (ac, av, "a:c:m:op:s:tv")) != EOF)
141    switch (i)
142      {
143      case 'a':
144	/* FIXME: Temporary hack.  */
145	{
146	  int len = strlen (av[0]) + strlen (optarg);
147	  char *argbuf = (char *) alloca (len + 2 + 50);
148	  sprintf (argbuf, "%s %s", av[0], optarg);
149#ifdef SIM_HAVE_BIENDIAN
150	  /* The desired endianness must be passed to sim_open.
151	     The value for "set-later" is set when we know what it is.
152	     -E support isn't yet part of the published interface.  */
153	  strcat (argbuf, " -E set-later");
154#endif
155	  sim_argv = buildargv (argbuf);
156	}
157	break;
158#ifdef SIM_HAVE_SIMCACHE
159      case 'c':
160	sim_set_simcache_size (atoi (optarg));
161	break;
162#endif
163      case 'm':
164	/* FIXME: Rename to sim_set_mem_size.  */
165	sim_size (atoi (optarg));
166	break;
167#ifdef SIM_HAVE_ENVIRONMENT
168      case 'o':
169	/* Operating enironment where any signals are delivered to the
170           target.  */
171	operating_p = 1;
172	break;
173#endif
174#ifdef SIM_HAVE_PROFILE
175      case 'p':
176	sim_set_profile (atoi (optarg));
177	break;
178      case 's':
179	sim_set_profile_size (atoi (optarg));
180	break;
181#endif
182      case 't':
183	trace = 1;
184	break;
185      case 'v':
186	/* Things that are printed with -v are the kinds of things that
187	   gcc -v prints.  This is not meant to include detailed tracing
188	   or debugging information, just summaries.  */
189	verbose = 1;
190	/* sim_set_verbose (1); */
191	break;
192	/* FIXME: Quick hack, to be replaced by more general facility.  */
193      default:
194	usage (0);
195      }
196
197  ac -= optind;
198  av += optind;
199  if (ac <= 0)
200    usage (0);
201
202  name = *av;
203  prog_args = av;
204
205  if (verbose)
206    {
207      printf ("%s %s\n", myname, name);
208    }
209
210  abfd = bfd_openr (name, 0);
211  if (!abfd)
212    {
213      fprintf (stderr, "%s: can't open %s: %s\n",
214	       myname, name, bfd_errmsg (bfd_get_error ()));
215      exit (1);
216    }
217
218  if (!bfd_check_format (abfd, bfd_object))
219    {
220      fprintf (stderr, "%s: can't load %s: %s\n",
221	       myname, name, bfd_errmsg (bfd_get_error ()));
222      exit (1);
223    }
224
225#ifdef SIM_HAVE_BIENDIAN
226  /* The endianness must be passed to sim_open because one may wish to
227     examine/set registers before calling sim_load [which is the other
228     place where one can determine endianness].  We previously passed the
229     endianness via global `target_byte_order' but that's not a clean
230     interface.  */
231  for (i = 1; sim_argv[i + 1] != NULL; ++i)
232    continue;
233  if (bfd_big_endian (abfd))
234    sim_argv[i] = "big";
235  else
236    sim_argv[i] = "little";
237#endif
238
239  /* Ensure that any run-time initialisation that needs to be
240     performed by the simulator can occur.  */
241  sd = sim_open (SIM_OPEN_STANDALONE, &default_callback, abfd, sim_argv);
242  if (sd == 0)
243    exit (1);
244
245  if (sim_load (sd, name, abfd, 0) == SIM_RC_FAIL)
246    exit (1);
247
248  if (sim_create_inferior (sd, abfd, prog_args, NULL) == SIM_RC_FAIL)
249    exit (1);
250
251#ifdef SIM_HAVE_ENVIRONMENT
252  /* NOTE: An old simulator supporting the operating environment MUST
253     provide sim_set_trace() and not sim_trace(). That way
254     sim_stop_reason() can be used to determine any stop reason.  */
255  if (trace)
256    sim_set_trace ();
257  sigrc = 0;
258  do
259    {
260      prev_sigint = signal (SIGINT, cntrl_c);
261      sim_resume (sd, 0, sigrc);
262      signal (SIGINT, prev_sigint);
263      sim_stop_reason (sd, &reason, &sigrc);
264    }
265  while (operating_p && reason == sim_stopped && sigrc != SIGINT);
266#else
267  if (trace)
268    {
269      int done = 0;
270      prev_sigint = signal (SIGINT, cntrl_c);
271      while (!done)
272	{
273	  done = sim_trace (sd);
274	}
275      signal (SIGINT, prev_sigint);
276      sim_stop_reason (sd, &reason, &sigrc);
277    }
278  else
279    {
280      prev_sigint = signal (SIGINT, cntrl_c);
281      sigrc = 0;
282      sim_resume (sd, 0, sigrc);
283      signal (SIGINT, prev_sigint);
284      sim_stop_reason (sd, &reason, &sigrc);
285    }
286#endif
287
288  if (verbose)
289    sim_info (sd, 0);
290  sim_close (sd, 0);
291
292  /* If reason is sim_exited, then sigrc holds the exit code which we want
293     to return.  If reason is sim_stopped or sim_signalled, then sigrc holds
294     the signal that the simulator received; we want to return that to
295     indicate failure.  */
296
297  /* Why did we stop? */
298  switch (reason)
299    {
300    case sim_signalled:
301    case sim_stopped:
302      if (sigrc != 0)
303	fprintf (stderr, "program stopped with signal %d.\n", sigrc);
304      break;
305
306    case sim_exited:
307      break;
308
309    case sim_running:
310    case sim_polling: /* These indicate a serious problem.  */
311      abort ();
312      break;
313
314    }
315
316  return sigrc;
317}
318
319static void
320usage (int help)
321{
322  FILE *stream = help ? stdout : stderr;
323
324  fprintf (stream, "Usage: %s [options] program [program args]\n", myname);
325  fprintf (stream, "Options:\n");
326  fprintf (stream, "-a args         Pass `args' to simulator.\n");
327#ifdef SIM_HAVE_SIMCACHE
328  fprintf (stream, "-c size         Set simulator cache size to `size'.\n");
329#endif
330  fprintf (stream, "-m size         Set memory size of simulator, in bytes.\n");
331#ifdef SIM_HAVE_ENVIRONMENT
332  fprintf (stream, "-o              Select operating (kernel) environment.\n");
333#endif
334#ifdef SIM_HAVE_PROFILE
335  fprintf (stream, "-p freq         Set profiling frequency.\n");
336  fprintf (stream, "-s size         Set profiling size.\n");
337#endif
338  fprintf (stream, "-t              Perform instruction tracing.\n");
339  fprintf (stream, "                Note: Very few simulators support tracing.\n");
340  fprintf (stream, "-v              Verbose output.\n");
341  fprintf (stream, "\n");
342  fprintf (stream, "program args    Arguments to pass to simulated program.\n");
343  fprintf (stream, "                Note: Very few simulators support this.\n");
344#ifdef SIM_TARGET_SWITCHES
345  fprintf (stream, "\nTarget specific options:\n");
346  sim_target_display_usage (help);
347#endif
348
349  if (help && REPORT_BUGS_TO[0])
350    printf ("Report bugs to %s\n", REPORT_BUGS_TO);
351
352  exit (help ? 0 : 1);
353}
354
355static void
356print_version ()
357{
358  printf ("GNU simulator %s%s\n", PKGVERSION, version);
359}
360