Add ABFD argument to sim_open call. Pass through to sim_config so
[external/binutils.git] / sim / common / nrun.c
1 /* New version of run front end support for simulators.
2    Copyright (C) 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 #include <signal.h>
19 #include "sim-main.h"
20
21 #ifdef HAVE_ENVIRON
22 extern char **environ;
23 #endif
24
25 static void usage (void);
26
27 extern host_callback default_callback;
28
29 static char *myname;
30
31 static SIM_DESC sd;
32
33 static RETSIGTYPE
34 cntrl_c (int sig)
35 {
36   if (! sim_stop (sd))
37     {
38       fprintf (stderr, "Quit!\n");
39       exit (1);
40     }
41 }
42
43 int
44 main (int argc, char **argv)
45 {
46   char *name;
47   char **prog_argv = NULL;
48   enum sim_stop reason;
49   int sigrc;
50   RETSIGTYPE (*prev_sigint) ();
51
52   myname = argv[0] + strlen (argv[0]);
53   while (myname > argv[0] && myname[-1] != '/')
54     --myname;
55
56   /* Create an instance of the simulator.  */
57   default_callback.init (&default_callback);
58   sd = sim_open (SIM_OPEN_STANDALONE, &default_callback, NULL, argv);
59   if (sd == 0)
60     exit (1);
61   if (STATE_MAGIC (sd) != SIM_MAGIC_NUMBER)
62     {
63       fprintf (stderr, "Internal error - bad magic number in simulator struct\n");
64       abort ();
65     }
66
67   /* Was there a program to run?  */
68   prog_argv = STATE_PROG_ARGV (sd);
69   if (prog_argv == NULL || *prog_argv == NULL)
70     usage ();
71
72   name = *prog_argv;
73
74   if (STATE_VERBOSE_P (sd))
75     printf ("%s %s\n", myname, name);
76
77   /* Load the program into the simulator.  */
78   if (sim_load (sd, name, NULL, 0) == SIM_RC_FAIL)
79     exit (1);
80
81   /* Prepare the program for execution.  */
82 #ifdef HAVE_ENVIRON
83   sim_create_inferior (sd, prog_argv, environ);
84 #else
85   sim_create_inferior (sd, prog_argv, NULL);
86 #endif
87
88   /* Run the program.  */
89   prev_sigint = signal (SIGINT, cntrl_c);
90   sim_resume (sd, 0, 0);
91   signal (SIGINT, prev_sigint);
92
93   /* Print any stats the simulator collected.  */
94   sim_info (sd, 0);
95
96   /* Find out why the program exited.  */
97   sim_stop_reason (sd, &reason, &sigrc);
98
99   /* Shutdown the simulator.  */
100   sim_close (sd, 0);
101
102   /* If reason is sim_exited, then sigrc holds the exit code which we want
103      to return.  If reason is sim_stopped or sim_signalled, then sigrc holds
104      the signal that the simulator received; we want to return that to
105      indicate failure.  */
106
107 #ifdef SIM_H8300 /* FIXME: Ugh.  grep for SLEEP in compile.c  */
108   if (sigrc == SIGILL)
109     abort ();
110   sigrc = 0;
111 #else
112   /* Why did we stop? */
113   switch (reason)
114     {
115     case sim_signalled:
116     case sim_stopped:
117       if (sigrc != 0)
118         fprintf (stderr, "program stopped with signal %d.\n", sigrc);
119       break;
120
121     case sim_exited:
122       break;
123
124     default:
125       fprintf (stderr, "program in undefined state (%d:%d)\n", reason, sigrc);
126       break;
127
128     }
129 #endif
130
131   return sigrc;
132 }
133
134 static void
135 usage ()
136 {
137   fprintf (stderr, "Usage: %s [options] program [program args]\n", myname);
138   fprintf (stderr, "Run `%s --help' for full list of options.\n", myname);
139   exit (1);
140 }