Do top level sim-hw module for device tree.
[deliverable/binutils-gdb.git] / sim / common / nrun.c
index 17fdfcc4bac7c6c286486282650fa8cf2fa3f82a..f78ed8d79b1af90adfdbecca93c6be0e7a474eb0 100644 (file)
@@ -15,64 +15,93 @@ You should have received a copy of the GNU General Public License along
 with this program; if not, write to the Free Software Foundation, Inc.,
 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
 
+#include <signal.h>
 #include "sim-main.h"
 
+#include "bfd.h"
+
 #ifdef HAVE_ENVIRON
 extern char **environ;
 #endif
 
-static void usage PARAMS ((void));
+static void usage (void);
 
 extern host_callback default_callback;
 
 static char *myname;
 
+static SIM_DESC sd;
+
+static RETSIGTYPE
+cntrl_c (int sig)
+{
+  if (! sim_stop (sd))
+    {
+      fprintf (stderr, "Quit!\n");
+      exit (1);
+    }
+}
+
 int
-main (argc, argv)
-     int argc;
-     char **argv;
+main (int argc, char **argv)
 {
   char *name;
   char **prog_argv = NULL;
+  struct _bfd *prog_bfd;
   enum sim_stop reason;
   int sigrc;
-  SIM_DESC sd;
+  RETSIGTYPE (*prev_sigint) ();
 
   myname = argv[0] + strlen (argv[0]);
   while (myname > argv[0] && myname[-1] != '/')
     --myname;
 
-  sim_set_callbacks (NULL, &default_callback);
-  default_callback.init (&default_callback);
-
   /* Create an instance of the simulator.  */
-  sd = sim_open (SIM_OPEN_STANDALONE, argv);
+  default_callback.init (&default_callback);
+  sd = sim_open (SIM_OPEN_STANDALONE, &default_callback, NULL, argv);
   if (sd == 0)
     exit (1);
+  if (STATE_MAGIC (sd) != SIM_MAGIC_NUMBER)
+    {
+      fprintf (stderr, "Internal error - bad magic number in simulator struct\n");
+      abort ();
+    }
 
   /* Was there a program to run?  */
   prog_argv = STATE_PROG_ARGV (sd);
+  prog_bfd = STATE_PROG_BFD (sd);
   if (prog_argv == NULL || *prog_argv == NULL)
     usage ();
 
   name = *prog_argv;
 
+  /* For simulators that don't open prog during sim_open() */
+  if (prog_bfd == NULL)
+    {
+      prog_bfd = bfd_openr (name, 0);
+      if (prog_bfd == NULL)
+       fprintf (stderr, "%s: can't open \"%s\": %s\n", 
+                myname, name, bfd_errmsg (bfd_get_error ()));
+    }
+
   if (STATE_VERBOSE_P (sd))
     printf ("%s %s\n", myname, name);
 
   /* Load the program into the simulator.  */
-  if (sim_load (sd, name, NULL, 0) == SIM_RC_FAIL)
+  if (sim_load (sd, name, prog_bfd, 0) == SIM_RC_FAIL)
     exit (1);
 
   /* Prepare the program for execution.  */
 #ifdef HAVE_ENVIRON
-  sim_create_inferior (sd, prog_argv, environ);
+  sim_create_inferior (sd, prog_bfd, prog_argv, environ);
 #else
-  sim_create_inferior (sd, prog_argv, NULL);
+  sim_create_inferior (sd, prog_bfd, prog_argv, NULL);
 #endif
 
   /* Run the program.  */
+  prev_sigint = signal (SIGINT, cntrl_c);
   sim_resume (sd, 0, 0);
+  signal (SIGINT, prev_sigint);
 
   /* Print any stats the simulator collected.  */
   sim_info (sd, 0);
@@ -104,6 +133,11 @@ main (argc, argv)
 
     case sim_exited:
       break;
+
+    default:
+      fprintf (stderr, "program in undefined state (%d:%d)\n", reason, sigrc);
+      break;
+
     }
 #endif
 
This page took 0.023748 seconds and 4 git commands to generate.