import insight-2000-02-04 snapshot (2nd try)
[external/binutils.git] / sim / common / nrun.c
index f78ed8d..c5b43a2 100644 (file)
@@ -49,13 +49,23 @@ main (int argc, char **argv)
   char **prog_argv = NULL;
   struct _bfd *prog_bfd;
   enum sim_stop reason;
-  int sigrc;
+  int sigrc = 0;
+  int single_step = 0;
   RETSIGTYPE (*prev_sigint) ();
 
   myname = argv[0] + strlen (argv[0]);
   while (myname > argv[0] && myname[-1] != '/')
     --myname;
 
+  /* INTERNAL: When MYNAME is `step', single step the simulator
+     instead of allowing it to run free.  The sole purpose of this
+     HACK is to allow the sim_resume interface's step argument to be
+     tested without having to build/run gdb. */
+  if (strlen (myname) > 4 && strcmp (myname - 4, "step") == 0)
+    {
+      single_step = 1;
+    }
+
   /* Create an instance of the simulator.  */
   default_callback.init (&default_callback);
   sd = sim_open (SIM_OPEN_STANDALONE, &default_callback, NULL, argv);
@@ -80,8 +90,17 @@ main (int argc, char **argv)
     {
       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 ()));
+       {
+         fprintf (stderr, "%s: can't open \"%s\": %s\n", 
+                  myname, name, bfd_errmsg (bfd_get_error ()));
+         exit (1);
+       }
+      if (!bfd_check_format (prog_bfd, bfd_object)) 
+       {
+         fprintf (stderr, "%s: \"%s\" is not an object file: %s\n",
+                  myname, name, bfd_errmsg (bfd_get_error ()));
+         exit (1);
+       }
     }
 
   if (STATE_VERBOSE_P (sd))
@@ -98,25 +117,64 @@ main (int argc, char **argv)
   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);
-
+  /* Run/Step the program.  */
+  if (single_step)
+    {
+      do
+       {
+         prev_sigint = signal (SIGINT, cntrl_c);
+         sim_resume (sd, 1/*step*/, 0);
+         signal (SIGINT, prev_sigint);
+         sim_stop_reason (sd, &reason, &sigrc);
+
+         if ((reason == sim_stopped) &&
+             (sigrc == sim_signal_to_host (sd, SIM_SIGINT)))
+           break; /* exit on control-C */
+       }
+      /* remain on breakpoint or signals in oe mode*/
+      while (((reason == sim_signalled) &&
+             (sigrc == sim_signal_to_host (sd, SIM_SIGTRAP))) ||
+            ((reason == sim_stopped) && 
+             (STATE_ENVIRONMENT (sd) == OPERATING_ENVIRONMENT)));
+    }
+  else 
+    {
+      do
+       {
+#if defined (HAVE_SIGACTION) && defined (SA_RESTART)
+         struct sigaction sa, osa;
+         sa.sa_handler = cntrl_c;
+         sigemptyset (&sa.sa_mask);
+         sa.sa_flags = 0;
+         sigaction (SIGINT, &sa, &osa);
+         prev_sigint = osa.sa_handler;
+#else
+         prev_sigint = signal (SIGINT, cntrl_c);
+#endif
+         sim_resume (sd, 0, sigrc);
+         signal (SIGINT, prev_sigint);
+         sim_stop_reason (sd, &reason, &sigrc);
+         
+         if ((reason == sim_stopped) &&
+             (sigrc == sim_signal_to_host (sd, SIM_SIGINT)))
+           break; /* exit on control-C */
+         
+         /* remain on signals in oe mode */
+       } while ((reason == sim_stopped) &&
+                (STATE_ENVIRONMENT (sd) == OPERATING_ENVIRONMENT));
+      
+    }
   /* Print any stats the simulator collected.  */
   sim_info (sd, 0);
-
-  /* Find out why the program exited.  */
-  sim_stop_reason (sd, &reason, &sigrc);
-
+  
   /* Shutdown the simulator.  */
   sim_close (sd, 0);
-
+  
   /* If reason is sim_exited, then sigrc holds the exit code which we want
      to return.  If reason is sim_stopped or sim_signalled, then sigrc holds
      the signal that the simulator received; we want to return that to
      indicate failure.  */
-
+  
 #ifdef SIM_H8300 /* FIXME: Ugh.  grep for SLEEP in compile.c  */
   if (sigrc == SIGILL)
     abort ();