Restrict simulator commands to an enabled simulator.
[binutils-gdb.git] / gdb / remote-sim.c
index 8a7d1df63d924c3bee2a27facc8cca2994ed59fb..7574cbe76685935b94680369cc7ac548edc52a44 100644 (file)
@@ -161,8 +161,6 @@ init_callbacks ()
       gdb_callback.error = gdb_os_error;
       gdb_callback.poll_quit = gdb_os_poll_quit;
       gdb_callback.magic = HOST_CALLBACK_MAGIC;
-      sim_set_callbacks (gdbsim_desc, &gdb_callback);
-      
       callbacks_initialized = 1;
     }
 }
@@ -497,13 +495,13 @@ gdbsim_open (args, from_tty)
   if (gdbsim_desc != NULL)
     unpush_target (&gdbsim_ops);
 
-  init_callbacks ();
-
   len = 7 + 1 + (args ? strlen (args) : 0) + 50;
   arg_buf = (char *) alloca (len);
   sprintf (arg_buf, "gdbsim%s%s",
           args ? " " : "", args ? args : "");
 #ifdef TARGET_BYTE_ORDER_SELECTABLE
+  /* Since GDB always closes the target and updates byte-order when
+     opening a new file, TARGET_BYTE_ORDER is normally correct. */
   if (TARGET_BYTE_ORDER == BIG_ENDIAN)
     strcat (arg_buf, " -E big");
   else
@@ -514,7 +512,9 @@ gdbsim_open (args, from_tty)
     error ("Insufficient memory available to allocate simulator arg list.");
   make_cleanup (freeargv, (char *) argv);
 
-  gdbsim_desc = sim_open (SIM_OPEN_DEBUG, argv);
+  init_callbacks ();
+  gdbsim_desc = sim_open (SIM_OPEN_DEBUG, &gdb_callback, argv);
+
   if (gdbsim_desc == 0)
     error ("unable to create simulator instance");
 
@@ -779,15 +779,22 @@ simulator_command (args, from_tty)
      char *args;
      int from_tty;
 {
-  /* The user may give a command before the simulator is opened, so
-     ensure that the callbacks have been set up.  */
-  init_callbacks ();
+  if (gdbsim_desc == NULL)
+    {
+
+      /* PREVIOUSLY: The user may give a command before the simulator
+         is opened. [...] (??? assuming of course one wishes to
+         continue to allow commands to be sent to unopened simulators,
+         which isn't entirely unreasonable). */
+
+      /* The simulator is a builtin abstraction of a remote target.
+         Consistent with that model, access to the simulator, via sim
+         commands, is restricted to the period when the channel to the
+         simulator is open. */
+
+      error ("Not connected to the simulator target");
+    }
 
-  /* Note that if the simulator hasn't been opened, gdbsim_desc == NULL
-     which is correct (??? assuming of course one wishes to continue to
-     allow commands to be sent to unopened simulators, which isn't entirely
-     unreasonable).  Simulators should be prepared to deal with any
-     combination of NULL or empty args. */
   sim_do_command (gdbsim_desc, args);
 }