[04/30] gdb: add interp::on_normal_stop method

Message ID 20230502205011.132151-5-simon.marchi@efficios.com
State New
Headers
Series Switch interpreters to use virtual methods |

Commit Message

Simon Marchi May 2, 2023, 8:49 p.m. UTC
  Same idea as the previous patch, but for the normal_stop event.

Change-Id: I4fc8ca8a51c63829dea390a2b6ce30b77f9fb863
---
 gdb/cli/cli-interp.c | 21 +++++-------------
 gdb/cli/cli-interp.h |  1 +
 gdb/infrun.c         | 19 +++++++++++------
 gdb/infrun.h         |  4 ++++
 gdb/interps.c        |  8 +++++++
 gdb/interps.h        |  7 ++++++
 gdb/mi/mi-interp.c   | 51 ++++++++++++++------------------------------
 gdb/mi/mi-interp.h   |  1 +
 gdb/remote.c         |  3 ++-
 9 files changed, 57 insertions(+), 58 deletions(-)
  

Patch

diff --git a/gdb/cli/cli-interp.c b/gdb/cli/cli-interp.c
index 51c78d96ee20..9508171e87d5 100644
--- a/gdb/cli/cli-interp.c
+++ b/gdb/cli/cli-interp.c
@@ -112,10 +112,8 @@  should_print_stop_to_console (struct interp *console_interp,
    interpreter-exec), print nothing.  These are named "cli_base" as
    they print to both CLI interpreters and TUI interpreters.  */
 
-/* Observer for the normal_stop notification.  */
-
-static void
-cli_base_on_normal_stop (struct bpstat *bs, int print_frame)
+void
+cli_interp_base::on_normal_stop (struct bpstat *bs, int print_frame)
 {
   if (!print_frame)
     return;
@@ -124,17 +122,10 @@  cli_base_on_normal_stop (struct bpstat *bs, int print_frame)
   if (cli_suppress_notification.normal_stop)
     return;
 
-  SWITCH_THRU_ALL_UIS ()
-    {
-      struct interp *interp = top_level_interpreter ();
-      cli_interp_base *cli = as_cli_interp_base (interp);
-      if (cli == nullptr)
-	continue;
+  thread_info *thread = inferior_thread ();
+  if (should_print_stop_to_console (this, thread))
+    print_stop_event (this->interp_ui_out ());
 
-      thread_info *thread = inferior_thread ();
-      if (should_print_stop_to_console (interp, thread))
-	print_stop_event (cli->interp_ui_out ());
-    }
 }
 
 void
@@ -397,8 +388,6 @@  _initialize_cli_interp ()
   interp_factory_register (INTERP_CONSOLE, cli_interp_factory);
 
   /* Note these all work for both the CLI and TUI interpreters.  */
-  gdb::observers::normal_stop.attach (cli_base_on_normal_stop,
-				      "cli-interp-base");
   gdb::observers::signal_exited.attach (cli_base_on_signal_exited,
 					"cli-interp-base");
   gdb::observers::exited.attach (cli_base_on_exited, "cli-interp-base");
diff --git a/gdb/cli/cli-interp.h b/gdb/cli/cli-interp.h
index f7bee4530b42..4fca801d4fd6 100644
--- a/gdb/cli/cli-interp.h
+++ b/gdb/cli/cli-interp.h
@@ -34,6 +34,7 @@  class cli_interp_base : public interp
   bool supports_command_editing () override;
 
   void on_signal_received (gdb_signal sig) override;
+  void on_normal_stop(bpstat *bs, int print_frame) override;
 
 private:
   struct saved_output_files
diff --git a/gdb/infrun.c b/gdb/infrun.c
index 4716d73571c5..8fa2cacc4149 100644
--- a/gdb/infrun.c
+++ b/gdb/infrun.c
@@ -6273,6 +6273,15 @@  notify_signal_received (gdb_signal sig)
   gdb::observers::signal_received.notify (sig);
 }
 
+/* See infrun.h.  */
+
+void
+notify_normal_stop (bpstat *bs, int print_frame)
+{
+  interps_notify_normal_stop (bs, print_frame);
+  gdb::observers::normal_stop.notify (bs, print_frame);
+}
+
 /* Come here when the program has stopped with a signal.  */
 
 static void
@@ -8955,12 +8964,10 @@  normal_stop ()
 
   /* Notify observers about the stop.  This is where the interpreters
      print the stop event.  */
-  if (inferior_ptid != null_ptid)
-    gdb::observers::normal_stop.notify (inferior_thread ()->control.stop_bpstat,
-					stop_print_frame);
-  else
-    gdb::observers::normal_stop.notify (nullptr, stop_print_frame);
-
+  notify_normal_stop ((inferior_ptid != null_ptid
+		       ? inferior_thread ()->control.stop_bpstat
+		       : nullptr),
+		      stop_print_frame);
   annotate_stopped ();
 
   if (target_has_execution ())
diff --git a/gdb/infrun.h b/gdb/infrun.h
index f7b60a4d9fb7..cbafc3ea4407 100644
--- a/gdb/infrun.h
+++ b/gdb/infrun.h
@@ -214,6 +214,10 @@  extern void set_step_info (thread_info *tp,
    signal SIG.  */
 extern void notify_signal_received (gdb_signal sig);
 
+/* Notify interpreters and observers that the current inferior has stopped
+   normally.  */
+extern void notify_normal_stop (bpstat *bs, int print_frame);
+
 /* Several print_*_reason helper functions to print why the inferior
    has stopped to the passed in UIOUT.  */
 
diff --git a/gdb/interps.c b/gdb/interps.c
index 5d061ad52aff..80160bc45f34 100644
--- a/gdb/interps.c
+++ b/gdb/interps.c
@@ -406,6 +406,14 @@  interps_notify_signal_received (gdb_signal sig)
   interps_notify (&interp::on_signal_received, sig);
 }
 
+/* See interps.h.  */
+
+void
+interps_notify_normal_stop (bpstat *bs, int print_frame)
+{
+  interps_notify (&interp::on_normal_stop, bs, print_frame);
+}
+
 /* This just adds the "interpreter-exec" command.  */
 void _initialize_interpreter ();
 void
diff --git a/gdb/interps.h b/gdb/interps.h
index 4762c4c93ff1..dd5a9b70e339 100644
--- a/gdb/interps.h
+++ b/gdb/interps.h
@@ -24,6 +24,7 @@ 
 
 #include "gdbsupport/intrusive_list.h"
 
+struct bpstat;
 struct ui_out;
 struct interp;
 struct ui;
@@ -86,6 +87,9 @@  class interp : public intrusive_list_node<interp>
      SIG.  */
   virtual void on_signal_received (gdb_signal sig) {}
 
+  /* Notify the interpreter that the current inferior has stopped normally.  */
+  virtual void on_normal_stop (bpstat *bs, int print_frame) {}
+
 private:
   /* The memory for this is static, it comes from literal strings (e.g. "cli").  */
   const char *m_name;
@@ -178,6 +182,9 @@  extern void interpreter_completer (struct cmd_list_element *ignore,
    SIG.  */
 extern void interps_notify_signal_received (gdb_signal sig);
 
+/* Notify all interpreters that the current inferior has stopped normally.  */
+extern void interps_notify_normal_stop (bpstat *bs, int print_frame);
+
 /* well-known interpreters */
 #define INTERP_CONSOLE		"console"
 #define INTERP_MI2             "mi2"
diff --git a/gdb/mi/mi-interp.c b/gdb/mi/mi-interp.c
index 43e49a9b94af..5aeba46c4c28 100644
--- a/gdb/mi/mi-interp.c
+++ b/gdb/mi/mi-interp.c
@@ -62,7 +62,6 @@  static void mi_remove_notify_hooks (void);
 
 static void mi_on_signal_exited (enum gdb_signal siggnal);
 static void mi_on_exited (int exitstatus);
-static void mi_on_normal_stop (struct bpstat *bs, int print_frame);
 static void mi_on_no_history (void);
 
 static void mi_new_thread (struct thread_info *t);
@@ -581,33 +580,28 @@  mi_on_no_history (void)
     }
 }
 
-static void
-mi_on_normal_stop_1 (struct bpstat *bs, int print_frame)
+void
+mi_interp::on_normal_stop (struct bpstat *bs, int print_frame)
 {
   /* Since this can be called when CLI command is executing,
      using cli interpreter, be sure to use MI uiout for output,
      not the current one.  */
-  struct ui_out *mi_uiout = top_level_interpreter ()->interp_ui_out ();
-  struct mi_interp *mi = (struct mi_interp *) top_level_interpreter ();
+  struct ui_out *mi_uiout = this->interp_ui_out ();
 
   if (print_frame)
     {
-      struct thread_info *tp;
-      int core;
-      struct interp *console_interp;
-
-      tp = inferior_thread ();
+      thread_info *tp = inferior_thread ();
 
       if (tp->thread_fsm () != nullptr
 	  && tp->thread_fsm ()->finished_p ())
 	{
-	  enum async_reply_reason reason;
-
-	  reason = tp->thread_fsm ()->async_reply_reason ();
+	  async_reply_reason reason
+	    = tp->thread_fsm ()->async_reply_reason ();
 	  mi_uiout->field_string ("reason", async_reason_lookup (reason));
 	}
 
-      console_interp = interp_lookup (current_ui, INTERP_CONSOLE);
+      interp *console_interp = interp_lookup (current_ui, INTERP_CONSOLE);
+
       /* We only want to print the displays once, and we want it to
 	 look just how it would on the console, so we use this to
 	 decide whether the MI stop should include them.  */
@@ -615,7 +609,7 @@  mi_on_normal_stop_1 (struct bpstat *bs, int print_frame)
       print_stop_event (mi_uiout, !console_print);
 
       if (console_print)
-	print_stop_event (mi->cli_uiout);
+	print_stop_event (this->cli_uiout);
 
       mi_uiout->field_signed ("thread-id", tp->global_num);
       if (non_stop)
@@ -627,29 +621,17 @@  mi_on_normal_stop_1 (struct bpstat *bs, int print_frame)
       else
 	mi_uiout->field_string ("stopped-threads", "all");
 
-      core = target_core_of_thread (tp->ptid);
+      int core = target_core_of_thread (tp->ptid);
       if (core != -1)
 	mi_uiout->field_signed ("core", core);
     }
-  
-  gdb_puts ("*stopped", mi->raw_stdout);
-  mi_out_put (mi_uiout, mi->raw_stdout);
-  mi_out_rewind (mi_uiout);
-  mi_print_timing_maybe (mi->raw_stdout);
-  gdb_puts ("\n", mi->raw_stdout);
-  gdb_flush (mi->raw_stdout);
-}
 
-static void
-mi_on_normal_stop (struct bpstat *bs, int print_frame)
-{
-  SWITCH_THRU_ALL_UIS ()
-    {
-      if (as_mi_interp (top_level_interpreter ()) == NULL)
-	continue;
-
-      mi_on_normal_stop_1 (bs, print_frame);
-    }
+  gdb_puts ("*stopped", this->raw_stdout);
+  mi_out_put (mi_uiout, this->raw_stdout);
+  mi_out_rewind (mi_uiout);
+  mi_print_timing_maybe (this->raw_stdout);
+  gdb_puts ("\n", this->raw_stdout);
+  gdb_flush (this->raw_stdout);
 }
 
 static void
@@ -1302,7 +1284,6 @@  _initialize_mi_interp ()
   gdb::observers::inferior_exit.attach (mi_inferior_exit, "mi-interp");
   gdb::observers::inferior_removed.attach (mi_inferior_removed, "mi-interp");
   gdb::observers::record_changed.attach (mi_record_changed, "mi-interp");
-  gdb::observers::normal_stop.attach (mi_on_normal_stop, "mi-interp");
   gdb::observers::target_resumed.attach (mi_on_resume, "mi-interp");
   gdb::observers::solib_loaded.attach (mi_solib_loaded, "mi-interp");
   gdb::observers::solib_unloaded.attach (mi_solib_unloaded, "mi-interp");
diff --git a/gdb/mi/mi-interp.h b/gdb/mi/mi-interp.h
index 75c17568d6fc..444625f22c88 100644
--- a/gdb/mi/mi-interp.h
+++ b/gdb/mi/mi-interp.h
@@ -43,6 +43,7 @@  class mi_interp final : public interp
   void pre_command_loop () override;
 
   void on_signal_received (gdb_signal sig) override;
+  void on_normal_stop (struct bpstat *bs, int print_frame) override;
 
   /* MI's output channels */
   mi_console_file *out;
diff --git a/gdb/remote.c b/gdb/remote.c
index 829cdbdef757..ec0dba239303 100644
--- a/gdb/remote.c
+++ b/gdb/remote.c
@@ -4722,7 +4722,8 @@  remote_target::print_one_stopped_thread (thread_info *thread)
       if (signal_print_state (sig))
 	notify_signal_received (sig);
     }
-  gdb::observers::normal_stop.notify (NULL, 1);
+
+  notify_normal_stop (nullptr, 1);
 }
 
 /* Process all initial stop replies the remote side sent in response