* libaout.h (enum machine_type): Change M_SPARCLET from 142 to 131.
[deliverable/binutils-gdb.git] / gdb / remote-sim.c
index 2a3e527d9c08420a358c6ba3d8e6db0b4c187d23..e9c32dbab578e2f380230d6ba9259c4afe780204 100644 (file)
@@ -1,5 +1,5 @@
 /* Generic remote debugging interface for simulators.
-   Copyright 1993, 1994 Free Software Foundation, Inc.
+   Copyright 1993, 1994, 1996 Free Software Foundation, Inc.
    Contributed by Cygnus Support.
    Steve Chamberlain (sac@cygnus.com).
 
@@ -17,13 +17,13 @@ GNU General Public License for more details.
 
 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., 675 Mass Ave, Cambridge, MA 02139, USA.  */
+Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
 
 #include "defs.h"
 #include "inferior.h"
 #include "wait.h"
 #include "value.h"
-#include <string.h>
+#include "gdb_string.h"
 #include <ctype.h>
 #include <fcntl.h>
 #include <signal.h>
@@ -32,13 +32,60 @@ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
 #include "terminal.h"
 #include "target.h"
 #include "gdbcore.h"
+#include "callback.h"
 #include "remote-sim.h"
 #include "remote-utils.h"
 
+/* Prototypes */
+
+static void dump_mem PARAMS ((char *buf, int len));
+
+static void init_callbacks PARAMS ((void));
+
+static void end_callbacks PARAMS ((void));
+
+static int gdb_os_write_stdout PARAMS ((host_callback *, const char *, int));
+
+static void gdb_os_printf_filtered PARAMS ((host_callback *, const char *, ...));
+
+static void gdb_os_error PARAMS ((host_callback *, const char *, ...));
+
+static void gdbsim_fetch_register PARAMS ((int regno));
+
+static void gdbsim_store_register PARAMS ((int regno));
+
+static void gdbsim_kill PARAMS ((void));
+
+static void gdbsim_load PARAMS ((char *prog, int fromtty));
+
+static void gdbsim_create_inferior PARAMS ((char *exec_file, char *args, char **env));
+
+static void gdbsim_open PARAMS ((char *args, int from_tty));
+
+static void gdbsim_close PARAMS ((int quitting));
+
+static void gdbsim_detach PARAMS ((char *args, int from_tty));
+
+static void gdbsim_resume PARAMS ((int pid, int step, enum target_signal siggnal));
+
+static int gdbsim_wait PARAMS ((int pid, struct target_waitstatus *status));
+
+static void gdbsim_prepare_to_store PARAMS ((void));
+
+static int gdbsim_xfer_inferior_memory PARAMS ((CORE_ADDR memaddr,
+                                               char *myaddr, int len,
+                                               int write,
+                                               struct target_ops *target));
+
+static void gdbsim_files_info PARAMS ((struct target_ops *target));
+
+static void gdbsim_mourn_inferior PARAMS ((void));
+
+static void simulator_command PARAMS ((char *args, int from_tty));
+
 /* Naming convention:
 
    sim_* are the interface to the simulator (see remote-sim.h).
-
    gdbsim_* are stuff which is internal to gdb.  */
 
 /* Forward data declarations */
@@ -71,6 +118,122 @@ dump_mem (buf, len)
     }
 }
 
+static host_callback gdb_callback;
+static int callbacks_initialized = 0;
+
+/* Initialize gdb_callback.  */
+
+static void
+init_callbacks ()
+{
+  if (! callbacks_initialized)
+    {
+      gdb_callback = default_callback;
+      gdb_callback.init (&gdb_callback);
+      gdb_callback.write_stdout = gdb_os_write_stdout;
+      gdb_callback.printf_filtered = gdb_os_printf_filtered;
+      gdb_callback.error = gdb_os_error;
+      sim_set_callbacks (&gdb_callback);
+      callbacks_initialized = 1;
+    }
+}
+
+/* Release callbacks (free resources used by them).  */
+
+static void
+end_callbacks ()
+{
+  if (callbacks_initialized)
+    {
+      gdb_callback.shutdown (&gdb_callback);
+      callbacks_initialized = 0;
+    }
+}
+
+/* GDB version of os_write_stdout callback.  */
+
+static int 
+gdb_os_write_stdout (p, buf, len)
+     host_callback *p;
+     const char *buf;
+     int len;
+{
+  int i;
+  char b[2];
+
+  for (i = 0; i < len; i++) 
+    {
+      b[0] = buf[i];
+      b[1] = 0;
+      if (target_output_hook)
+       target_output_hook (b);
+      else
+       fputs_filtered (b, gdb_stdout);
+    }
+  return len;
+}
+
+/* GDB version of printf_filtered callback.  */
+
+/* VARARGS */
+static void
+#ifdef ANSI_PROTOTYPES
+gdb_os_printf_filtered (host_callback *p, const char *format, ...)
+#else
+gdb_os_printf_filtered (p, va_alist)
+     host_callback *p;
+     va_dcl
+#endif
+{
+  va_list args;
+#ifdef ANSI_PROTOTYPES
+  va_start (args, format);
+#else
+  char *format;
+
+  va_start (args);
+  format = va_arg (args, char *);
+#endif
+
+  vfprintf_filtered (gdb_stdout, format, args);
+
+  va_end (args);
+}
+
+/* GDB version of error callback.  */
+
+/* VARARGS */
+static void
+#ifdef ANSI_PROTOTYPES
+gdb_os_error (host_callback *p, const char *format, ...)
+#else
+gdb_os_error (p, va_alist)
+     host_callback *p;
+     va_dcl
+#endif
+{
+  if (error_hook)
+    (*error_hook) ();
+  else 
+    {
+      va_list args;
+#ifdef ANSI_PROTOTYPES
+      va_start (args, format);
+#else
+      char *format;
+
+      va_start (args);
+      format = va_arg (args, char *);
+#endif
+
+      error_begin ();
+      vfprintf_filtered (gdb_stderr, format, args);
+      fprintf_filtered (gdb_stderr, "\n");
+      va_end (args);
+      return_to_top_level (RETURN_ERROR);
+    }
+}
+
 static void
 gdbsim_fetch_register (regno)
 int regno;
@@ -95,6 +258,7 @@ int regno;
     }
 }
 
+
 static void
 gdbsim_store_register (regno)
 int regno;
@@ -150,13 +314,13 @@ gdbsim_load (prog, fromtty)
   program_loaded = 1;
 
   if (sim_load (prog, fromtty) != 0)
-    gr_load_image (prog, fromtty);
+    generic_load (prog, fromtty);
 }
 
 
 /* Start an inferior process and set inferior_pid to its pid.
    EXEC_FILE is the file to run.
-   ALLARGS is a string containing the arguments to the program.
+   ARGS is a string containing the arguments to the program.
    ENV is the environment vector to pass.  Errors reported with error().
    On VxWorks and various standalone systems, we ignore exec_file.  */
 /* This is called not only when we first attach, but also when the
@@ -184,11 +348,11 @@ gdbsim_create_inferior (exec_file, args, env)
 
   entry_pt = (CORE_ADDR) bfd_get_start_address (exec_bfd);
 
-  gdbsim_kill (NULL, NULL);     
+  gdbsim_kill ();       
   remove_breakpoints ();
   init_wait_for_inferior ();
 
-  len = 5 + strlen (exec_file) + 1 + strlen (args) + 1 + /*slop*/ 10;
+  len = strlen (exec_file) + 1 + strlen (args) + 1 + /*slop*/ 10;
   arg_buf = (char *) alloca (len);
   arg_buf[0] = '\0';
   strcat (arg_buf, exec_file);
@@ -216,11 +380,12 @@ gdbsim_open (args, from_tty)
   if (sr_get_debug ())
     printf_filtered ("gdbsim_open: args \"%s\"\n", args ? args : "(null)");
 
+  init_callbacks ();
+
   sim_open (args);
 
   push_target (&gdbsim_ops);
   target_fetch_registers (-1);
-
   printf_filtered ("Connected to the simulator.\n");
 }
 
@@ -243,6 +408,8 @@ gdbsim_close (quitting)
   program_loaded = 0;
 
   sim_close (quitting);
+
+  end_callbacks ();
 }
 
 /* Takes a program previously attached to and detaches it.
@@ -276,6 +443,9 @@ gdbsim_resume (pid, step, siggnal)
      int pid, step;
      enum target_signal siggnal;
 {
+  if (inferior_pid != 42)
+    error ("The program is not being run.");
+
   if (sr_get_debug ())
     printf_filtered ("gdbsim_resume: step %d, signal %d\n", step, siggnal);
 
@@ -385,7 +555,7 @@ gdbsim_files_info (target)
     }
 }
 
-/* Clear the sims notion of what the break points are.  */
+/* Clear the simulator's notion of what the break points are.  */
 
 static void
 gdbsim_mourn_inferior () 
@@ -397,35 +567,71 @@ gdbsim_mourn_inferior ()
   generic_mourn_inferior ();
 }
 
-/* Define the target subroutine names */
+/* Pass the command argument through to the simulator verbatim.  The
+   simulator must do any command interpretation work.  */
 
-struct target_ops gdbsim_ops = 
+static void
+simulator_command (args, from_tty)
+     char *args;
+     int from_tty;
 {
-  "sim", "simulator",
-  "Use the simulator",
-  gdbsim_open, gdbsim_close, 
-  0, gdbsim_detach, gdbsim_resume, gdbsim_wait, /* attach */
-  gdbsim_fetch_register, gdbsim_store_register,
-  gdbsim_prepare_to_store,
-  gdbsim_xfer_inferior_memory, 
-  gdbsim_files_info,
-  0, 0,                                /* Breakpoints */
-  0, 0, 0, 0, 0,               /* Terminal handling */
-  gdbsim_kill,                 /* kill */
-  gdbsim_load,                 /* load */
-  0,                           /* lookup_symbol */
-  gdbsim_create_inferior,      /* create_inferior */ 
-  gdbsim_mourn_inferior,       /* mourn_inferior */
-  0,                           /* can_run */
-  0,                           /* notice_signals */
-  process_stratum, 0,          /* next */
-  1, 1, 1, 1, 1,               /* all mem, mem, stack, regs, exec */
-  0, 0,                                /* Section pointers */
-  OPS_MAGIC,                   /* Always the last thing */
+  /* The user may give a command before the simulator is opened, so
+     ensure that the callbacks have been set up.  */
+  init_callbacks ();
+
+  sim_do_command (args);
+}
+
+/* Define the target subroutine names */
+
+struct target_ops gdbsim_ops = {
+  "sim",                       /* to_shortname */
+  "simulator",                 /* to_longname */
+  "Use the compiled-in simulator.",  /* to_doc */
+  gdbsim_open,                 /* to_open */
+  gdbsim_close,                        /* to_close */
+  NULL,                                /* to_attach */
+  gdbsim_detach,               /* to_detach */
+  gdbsim_resume,               /* to_resume */
+  gdbsim_wait,                 /* to_wait */
+  gdbsim_fetch_register,       /* to_fetch_registers */
+  gdbsim_store_register,       /* to_store_registers */
+  gdbsim_prepare_to_store,     /* to_prepare_to_store */
+  gdbsim_xfer_inferior_memory, /* to_xfer_memory */
+  gdbsim_files_info,           /* to_files_info */
+  memory_insert_breakpoint,    /* to_insert_breakpoint */
+  memory_remove_breakpoint,    /* to_remove_breakpoint */
+  NULL,                                /* to_terminal_init */
+  NULL,                                /* to_terminal_inferior */
+  NULL,                                /* to_terminal_ours_for_output */
+  NULL,                                /* to_terminal_ours */
+  NULL,                                /* to_terminal_info */
+  gdbsim_kill,                 /* to_kill */
+  gdbsim_load,                 /* to_load */
+  NULL,                                /* to_lookup_symbol */
+  gdbsim_create_inferior,      /* to_create_inferior */ 
+  gdbsim_mourn_inferior,       /* to_mourn_inferior */
+  0,                           /* to_can_run */
+  0,                           /* to_notice_signals */
+  0,                           /* to_thread_alive */
+  0,                           /* to_stop */
+  process_stratum,             /* to_stratum */
+  NULL,                                /* to_next */
+  1,                           /* to_has_all_memory */
+  1,                           /* to_has_memory */
+  1,                           /* to_has_stack */
+  1,                           /* to_has_registers */
+  1,                           /* to_has_execution */
+  NULL,                                /* sections */
+  NULL,                                /* sections_end */
+  OPS_MAGIC,                   /* to_magic */
 };
 
 void
 _initialize_remote_sim ()
 {
   add_target (&gdbsim_ops);
+
+  add_com ("sim <command>", class_obscure, simulator_command,
+          "Send a command to the simulator."); 
 }
This page took 0.026866 seconds and 4 git commands to generate.