[11/30] gdb: add interp::on_new_thread method

Message ID 20230502205011.132151-12-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 new_thread.

Change-Id: Ib70ae3421b736fd69d86c4e7c708bec349aa256c
---
 gdb/interps.c      |  8 ++++++++
 gdb/interps.h      |  7 +++++++
 gdb/mi/mi-interp.c | 25 +++++++------------------
 gdb/mi/mi-interp.h |  1 +
 gdb/thread.c       | 12 +++++++++++-
 5 files changed, 34 insertions(+), 19 deletions(-)
  

Patch

diff --git a/gdb/interps.c b/gdb/interps.c
index 55abcb3c6e40..896484530aad 100644
--- a/gdb/interps.c
+++ b/gdb/interps.c
@@ -446,6 +446,14 @@  interps_notify_user_selected_context_changed (user_selected_what selection)
   interps_notify (&interp::on_user_selected_context_changed, selection);
 }
 
+/* See interps.h.  */
+
+void
+interps_notify_new_thread (thread_info *t)
+{
+  interps_notify (&interp::on_new_thread, t);
+}
+
 /* This just adds the "interpreter-exec" command.  */
 void _initialize_interpreter ();
 void
diff --git a/gdb/interps.h b/gdb/interps.h
index 4706c9bb63e7..05ea91e0cdaa 100644
--- a/gdb/interps.h
+++ b/gdb/interps.h
@@ -29,6 +29,7 @@  struct ui_out;
 struct interp;
 struct ui;
 class completion_tracker;
+struct thread_info;
 
 typedef struct interp *(*interp_factory_func) (const char *name);
 
@@ -114,6 +115,9 @@  class interp : public intrusive_list_node<interp>
   virtual void on_user_selected_context_changed (user_selected_what selection)
     {}
 
+  /* Notify the interpreter that thread T has been created.  */
+  virtual void on_new_thread (thread_info *t) {}
+
 private:
   /* The memory for this is static, it comes from literal strings (e.g. "cli").  */
   const char *m_name;
@@ -225,6 +229,9 @@  extern void interps_notify_exited (int status);
 extern void interps_notify_user_selected_context_changed
   (user_selected_what selection);
 
+/* Notify all interpreters that thread T has been created.  */
+extern void interps_notify_new_thread (thread_info *t);
+
 /* 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 ab8c3efbb128..42ec1a7b522e 100644
--- a/gdb/mi/mi-interp.c
+++ b/gdb/mi/mi-interp.c
@@ -60,7 +60,6 @@  static int mi_interp_query_hook (const char *ctlstr, va_list ap)
 static void mi_insert_notify_hooks (void);
 static void mi_remove_notify_hooks (void);
 
-static void mi_new_thread (struct thread_info *t);
 static void mi_thread_exit (struct thread_info *t, int silent);
 static void mi_record_changed (struct inferior*, int, const char *,
 			       const char *);
@@ -306,24 +305,15 @@  mi_interp::pre_command_loop ()
   display_mi_prompt (mi);
 }
 
-static void
-mi_new_thread (struct thread_info *t)
+void
+mi_interp::on_new_thread (thread_info *t)
 {
-  SWITCH_THRU_ALL_UIS ()
-    {
-      struct mi_interp *mi = as_mi_interp (top_level_interpreter ());
-
-      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-created,id=\"%d\",group-id=\"i%d\"",
-		  t->global_num, t->inf->num);
-      gdb_flush (mi->event_channel);
-    }
+  gdb_printf (this->event_channel, "thread-created,id=\"%d\",group-id=\"i%d\"",
+	      t->global_num, t->inf->num);
+  gdb_flush (this->event_channel);
 }
 
 static void
@@ -1188,7 +1178,6 @@  _initialize_mi_interp ()
   interp_factory_register (INTERP_MI4, mi_interp_factory);
   interp_factory_register (INTERP_MI, mi_interp_factory);
 
-  gdb::observers::new_thread.attach (mi_new_thread, "mi-interp");
   gdb::observers::thread_exit.attach (mi_thread_exit, "mi-interp");
   gdb::observers::inferior_added.attach (mi_inferior_added, "mi-interp");
   gdb::observers::inferior_appeared.attach (mi_inferior_appeared, "mi-interp");
diff --git a/gdb/mi/mi-interp.h b/gdb/mi/mi-interp.h
index 430c61dbbf0a..77fa708397ac 100644
--- a/gdb/mi/mi-interp.h
+++ b/gdb/mi/mi-interp.h
@@ -50,6 +50,7 @@  class mi_interp final : public interp
   void on_sync_execution_done () override;
   void on_command_error () override;
   void on_user_selected_context_changed (user_selected_what selection) override;
+  void on_new_thread (thread_info *t) override;
 
   /* MI's output channels */
   mi_console_file *out;
diff --git a/gdb/thread.c b/gdb/thread.c
index 13db9f8747ea..223ba1df96d9 100644
--- a/gdb/thread.c
+++ b/gdb/thread.c
@@ -49,6 +49,7 @@ 
 #include "gdbsupport/gdb_optional.h"
 #include "inline-frame.h"
 #include "stack.h"
+#include "interps.h"
 
 /* See gdbthread.h.  */
 
@@ -260,6 +261,15 @@  new_thread (struct inferior *inf, ptid_t ptid)
   return tp;
 }
 
+/* Notify interpreters and observers that thread T has been created.  */
+
+static void
+notify_new_thread (thread_info *t)
+{
+  interps_notify_new_thread (t);
+  gdb::observers::new_thread.notify (t);
+}
+
 struct thread_info *
 add_thread_silent (process_stratum_target *targ, ptid_t ptid)
 {
@@ -280,7 +290,7 @@  add_thread_silent (process_stratum_target *targ, ptid_t ptid)
     delete_thread (tp);
 
   tp = new_thread (inf, ptid);
-  gdb::observers::new_thread.notify (tp);
+  notify_new_thread (tp);
 
   return tp;
 }