[13/30] gdb: add interp::on_inferior_added method

Message ID 20230502205011.132151-14-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 previous patches, but for inferior_added.

mi_interp::init avoided using mi_inferior_added, since, as the comment
used to say, it would notify all MI interpreters.  Now, it's easy to
only notify the new interpreter, so it's possible to just call the
on_inferior_added method in mi_interp::init.

Change-Id: I0eddbd5367217d1c982516982089913019ef309f
---
 gdb/inferior.c     | 12 +++++++++++-
 gdb/interps.c      |  8 ++++++++
 gdb/interps.h      |  7 +++++++
 gdb/mi/mi-interp.c | 46 ++++++++--------------------------------------
 gdb/mi/mi-interp.h |  1 +
 5 files changed, 35 insertions(+), 39 deletions(-)
  

Patch

diff --git a/gdb/inferior.c b/gdb/inferior.c
index 8962b6401e34..a70bbe412471 100644
--- a/gdb/inferior.c
+++ b/gdb/inferior.c
@@ -38,6 +38,7 @@ 
 #include "progspace-and-thread.h"
 #include "gdbsupport/buildargv.h"
 #include "cli/cli-style.h"
+#include "interps.h"
 
 intrusive_list<inferior> inferior_list;
 static int highest_inferior_num;
@@ -189,6 +190,15 @@  inferior::do_all_continuations ()
     }
 }
 
+/* Notify interpreters and observers that inferior INF was added.  */
+
+static void
+notify_inferior_added (inferior *inf)
+{
+  interps_notify_inferior_added (inf);
+  gdb::observers::inferior_added.notify (inf);
+}
+
 struct inferior *
 add_inferior_silent (int pid)
 {
@@ -196,7 +206,7 @@  add_inferior_silent (int pid)
 
   inferior_list.push_back (*inf);
 
-  gdb::observers::inferior_added.notify (inf);
+  notify_inferior_added (inf);
 
   if (pid != 0)
     inferior_appeared (inf, pid);
diff --git a/gdb/interps.c b/gdb/interps.c
index be11fd835189..f64869953088 100644
--- a/gdb/interps.c
+++ b/gdb/interps.c
@@ -462,6 +462,14 @@  interps_notify_thread_exited (thread_info *t, int silent)
   interps_notify (&interp::on_thread_exited, t, silent);
 }
 
+/* See interps.h.  */
+
+void
+interps_notify_inferior_added (inferior *inf)
+{
+  interps_notify (&interp::on_inferior_added, inf);
+}
+
 /* This just adds the "interpreter-exec" command.  */
 void _initialize_interpreter ();
 void
diff --git a/gdb/interps.h b/gdb/interps.h
index cd099381d002..0c73a1a7ce8a 100644
--- a/gdb/interps.h
+++ b/gdb/interps.h
@@ -30,6 +30,7 @@  struct interp;
 struct ui;
 class completion_tracker;
 struct thread_info;
+struct inferior;
 
 typedef struct interp *(*interp_factory_func) (const char *name);
 
@@ -121,6 +122,9 @@  class interp : public intrusive_list_node<interp>
   /* Notify the interpreter that thread T has exited.  */
   virtual void on_thread_exited (thread_info *, int silent) {}
 
+  /* Notify the intepreter that inferior INF was added.  */
+  virtual void on_inferior_added (inferior *inf) {}
+
 private:
   /* The memory for this is static, it comes from literal strings (e.g. "cli").  */
   const char *m_name;
@@ -238,6 +242,9 @@  extern void interps_notify_new_thread (thread_info *t);
 /* Notify all interpreters that thread T has exited.  */
 extern void interps_notify_thread_exited (thread_info *t, int silent);
 
+/* Notify all intepreters that inferior INF was added.  */
+extern void interps_notify_inferior_added (inferior *inf);
+
 /* 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 a1960b14046d..c4903d7db3b7 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_record_changed (struct inferior*, int, const char *,
 			       const char *);
-static void mi_inferior_added (struct inferior *inf);
 static void mi_inferior_appeared (struct inferior *inf);
 static void mi_inferior_exit (struct inferior *inf);
 static void mi_inferior_removed (struct inferior *inf);
@@ -137,20 +136,10 @@  mi_interp::init (bool top_level)
 
 	 This is also called when additional MI interpreters are added (using
 	 the new-ui command), when multiple inferiors possibly exist, so we need
-	 to use iteration to report all the inferiors.  mi_inferior_added can't
-	 be used, because it would print the event on all the other MI UIs.  */
+	 to use iteration to report all the inferiors.  */
 
       for (inferior *inf : all_inferiors ())
-	{
-	  target_terminal::scoped_restore_terminal_state term_state;
-	  target_terminal::ours_for_output ();
-
-	  gdb_printf (mi->event_channel,
-		      "thread-group-added,id=\"i%d\"",
-		      inf->num);
-
-	  gdb_flush (mi->event_channel);
-	}
+	mi->on_inferior_added (inf);
   }
 }
 
@@ -369,32 +358,14 @@  mi_record_changed (struct inferior *inferior, int started, const char *method,
     }
 }
 
-static void
-mi_inferior_added (struct inferior *inf)
+void
+mi_interp::on_inferior_added (inferior *inf)
 {
-  SWITCH_THRU_ALL_UIS ()
-    {
-      struct interp *interp;
-      struct mi_interp *mi;
-
-      /* We'll be called once for the initial inferior, before the top
-	 level interpreter is set.  */
-      interp = top_level_interpreter ();
-      if (interp == NULL)
-	continue;
-
-      mi = as_mi_interp (interp);
-      if (mi == NULL)
-	continue;
-
-      target_terminal::scoped_restore_terminal_state term_state;
-      target_terminal::ours_for_output ();
+  target_terminal::scoped_restore_terminal_state term_state;
+  target_terminal::ours_for_output ();
 
-      gdb_printf (mi->event_channel,
-		  "thread-group-added,id=\"i%d\"",
-		  inf->num);
-      gdb_flush (mi->event_channel);
-    }
+  gdb_printf (this->event_channel, "thread-group-added,id=\"i%d\"", inf->num);
+  gdb_flush (this->event_channel);
 }
 
 static void
@@ -1168,7 +1139,6 @@  _initialize_mi_interp ()
   interp_factory_register (INTERP_MI4, mi_interp_factory);
   interp_factory_register (INTERP_MI, mi_interp_factory);
 
-  gdb::observers::inferior_added.attach (mi_inferior_added, "mi-interp");
   gdb::observers::inferior_appeared.attach (mi_inferior_appeared, "mi-interp");
   gdb::observers::inferior_exit.attach (mi_inferior_exit, "mi-interp");
   gdb::observers::inferior_removed.attach (mi_inferior_removed, "mi-interp");
diff --git a/gdb/mi/mi-interp.h b/gdb/mi/mi-interp.h
index 443ec03465a7..1be0d8771cf5 100644
--- a/gdb/mi/mi-interp.h
+++ b/gdb/mi/mi-interp.h
@@ -52,6 +52,7 @@  class mi_interp final : public interp
   void on_user_selected_context_changed (user_selected_what selection) override;
   void on_new_thread (thread_info *t) override;
   void on_thread_exited (thread_info *t, int silent) override;
+  void on_inferior_added (inferior *inf) override;
 
   /* MI's output channels */
   mi_console_file *out;