public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Simon Marchi <simon.marchi@efficios.com>
To: gdb-patches@sourceware.org
Cc: Simon Marchi <simon.marchi@efficios.com>
Subject: [PATCH 10/30] gdb: add interp::on_user_selected_context_changed method
Date: Tue,  2 May 2023 16:49:50 -0400	[thread overview]
Message-ID: <20230502205011.132151-11-simon.marchi@efficios.com> (raw)
In-Reply-To: <20230502205011.132151-1-simon.marchi@efficios.com>

Same as previous patches, but for user_selected_context_changed.

Change-Id: I40de15be897671227d4bcf3e747f0fd595f0d5be
---
 gdb/cli/cli-interp.c | 36 +++++--------------------
 gdb/cli/cli-interp.h |  1 +
 gdb/inferior.c       |  4 +--
 gdb/infrun.c         |  8 ++++++
 gdb/infrun.h         |  3 +++
 gdb/interps.c        |  8 ++++++
 gdb/interps.h        |  8 ++++++
 gdb/mi/mi-interp.c   | 64 +++++++++++++++-----------------------------
 gdb/mi/mi-interp.h   |  1 +
 gdb/mi/mi-main.c     |  2 +-
 gdb/stack.c          |  8 +++---
 gdb/thread.c         |  6 ++---
 12 files changed, 66 insertions(+), 83 deletions(-)

diff --git a/gdb/cli/cli-interp.c b/gdb/cli/cli-interp.c
index ffb40729fc61..6a175f7baa13 100644
--- a/gdb/cli/cli-interp.c
+++ b/gdb/cli/cli-interp.c
@@ -67,15 +67,6 @@ cli_interp::cli_interp (const char *name)
 /* Suppress notification struct.  */
 struct cli_suppress_notification cli_suppress_notification;
 
-/* Returns the INTERP's data cast as cli_interp_base if INTERP is a
-   console-like interpreter, and returns NULL otherwise.  */
-
-static cli_interp_base *
-as_cli_interp_base (interp *interp)
-{
-  return dynamic_cast<cli_interp_base *> (interp);
-}
-
 /* See cli-interp.h.
 
    Breakpoint hits should always be mirrored to a console.  Deciding
@@ -164,10 +155,8 @@ cli_interp_base::on_command_error ()
   display_gdb_prompt (NULL);
 }
 
-/* Observer for the user_selected_context_changed notification.  */
-
-static void
-cli_base_on_user_selected_context_changed (user_selected_what selection)
+void
+cli_interp_base::on_user_selected_context_changed (user_selected_what selection)
 {
   /* This event is suppressed.  */
   if (cli_suppress_notification.user_selected_context)
@@ -175,19 +164,12 @@ cli_base_on_user_selected_context_changed (user_selected_what selection)
 
   thread_info *tp = inferior_ptid != null_ptid ? inferior_thread () : nullptr;
 
-  SWITCH_THRU_ALL_UIS ()
-    {
-      cli_interp_base *cli = as_cli_interp_base (top_level_interpreter ());
-      if (cli == nullptr)
-	continue;
-
-      if (selection & USER_SELECTED_INFERIOR)
-	print_selected_inferior (cli->interp_ui_out ());
+  if (selection & USER_SELECTED_INFERIOR)
+    print_selected_inferior (this->interp_ui_out ());
 
-      if (tp != nullptr
-	  && ((selection & (USER_SELECTED_THREAD | USER_SELECTED_FRAME))))
-	print_selected_thread_frame (cli->interp_ui_out (), selection);
-    }
+  if (tp != nullptr
+      && ((selection & (USER_SELECTED_THREAD | USER_SELECTED_FRAME))))
+    print_selected_thread_frame (this->interp_ui_out (), selection);
 }
 
 /* pre_command_loop implementation.  */
@@ -347,8 +329,4 @@ void
 _initialize_cli_interp ()
 {
   interp_factory_register (INTERP_CONSOLE, cli_interp_factory);
-
-  /* Note these all work for both the CLI and TUI interpreters.  */
-  gdb::observers::user_selected_context_changed.attach
-    (cli_base_on_user_selected_context_changed, "cli-interp-base");
 }
diff --git a/gdb/cli/cli-interp.h b/gdb/cli/cli-interp.h
index 5020b2bc6ef4..a1a20b678942 100644
--- a/gdb/cli/cli-interp.h
+++ b/gdb/cli/cli-interp.h
@@ -40,6 +40,7 @@ class cli_interp_base : public interp
   void on_no_history () override;
   void on_sync_execution_done () override;
   void on_command_error () override;
+  void on_user_selected_context_changed (user_selected_what selection) override;
 
 private:
   struct saved_output_files
diff --git a/gdb/inferior.c b/gdb/inferior.c
index fd451c87e709..8962b6401e34 100644
--- a/gdb/inferior.c
+++ b/gdb/inferior.c
@@ -745,7 +745,7 @@ inferior_command (const char *args, int from_tty)
 	      switch_to_thread (tp);
 	    }
 
-	  gdb::observers::user_selected_context_changed.notify
+	  notify_user_selected_context_changed
 	    (USER_SELECTED_INFERIOR
 	     | USER_SELECTED_THREAD
 	     | USER_SELECTED_FRAME);
@@ -754,7 +754,7 @@ inferior_command (const char *args, int from_tty)
 	{
 	  switch_to_inferior_no_thread (inf);
 
-	  gdb::observers::user_selected_context_changed.notify
+	  notify_user_selected_context_changed
 	    (USER_SELECTED_INFERIOR);
 	}
     }
diff --git a/gdb/infrun.c b/gdb/infrun.c
index 8d648cf04f0a..aa2d34aa6b35 100644
--- a/gdb/infrun.c
+++ b/gdb/infrun.c
@@ -6282,6 +6282,14 @@ notify_normal_stop (bpstat *bs, int print_frame)
   gdb::observers::normal_stop.notify (bs, print_frame);
 }
 
+/* See infrun.h.  */
+
+void notify_user_selected_context_changed (user_selected_what selection)
+{
+  interps_notify_user_selected_context_changed (selection);
+  gdb::observers::user_selected_context_changed.notify (selection);
+}
+
 /* Come here when the program has stopped with a signal.  */
 
 static void
diff --git a/gdb/infrun.h b/gdb/infrun.h
index cbafc3ea4407..a343d27f72de 100644
--- a/gdb/infrun.h
+++ b/gdb/infrun.h
@@ -218,6 +218,9 @@ extern void notify_signal_received (gdb_signal sig);
    normally.  */
 extern void notify_normal_stop (bpstat *bs, int print_frame);
 
+/* Notify interpreters and observers that the user focus has changed.  */
+extern void notify_user_selected_context_changed (user_selected_what selection);
+
 /* 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 885bfc36ec70..55abcb3c6e40 100644
--- a/gdb/interps.c
+++ b/gdb/interps.c
@@ -438,6 +438,14 @@ interps_notify_exited (int status)
   interps_notify (&interp::on_exited, status);
 }
 
+/* See interps.h.  */
+
+void
+interps_notify_user_selected_context_changed (user_selected_what selection)
+{
+  interps_notify (&interp::on_user_selected_context_changed, selection);
+}
+
 /* This just adds the "interpreter-exec" command.  */
 void _initialize_interpreter ();
 void
diff --git a/gdb/interps.h b/gdb/interps.h
index 53d951b8d637..4706c9bb63e7 100644
--- a/gdb/interps.h
+++ b/gdb/interps.h
@@ -110,6 +110,10 @@ class interp : public intrusive_list_node<interp>
      command on this interpreter.  */
   virtual void on_command_error () {}
 
+  /* Notify the interpreter that the user focus has changed.  */
+  virtual void on_user_selected_context_changed (user_selected_what selection)
+    {}
+
 private:
   /* The memory for this is static, it comes from literal strings (e.g. "cli").  */
   const char *m_name;
@@ -217,6 +221,10 @@ extern void interps_notify_no_history ();
    status STATUS.  */
 extern void interps_notify_exited (int status);
 
+/* Notify all interpreters that the user focus has changed.  */
+extern void interps_notify_user_selected_context_changed
+  (user_selected_what selection);
+
 /* 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 45160571ce98..ab8c3efbb128 100644
--- a/gdb/mi/mi-interp.c
+++ b/gdb/mi/mi-interp.c
@@ -1086,60 +1086,40 @@ mi_memory_changed (struct inferior *inferior, CORE_ADDR memaddr,
     }
 }
 
-/* Emit an event when the selection context (inferior, thread, frame)
-   changed.  */
-
-static void
-mi_user_selected_context_changed (user_selected_what selection)
+void
+mi_interp::on_user_selected_context_changed (user_selected_what selection)
 {
-  struct thread_info *tp;
-
   /* Don't send an event if we're responding to an MI command.  */
   if (mi_suppress_notification.user_selected_context)
     return;
 
-  if (inferior_ptid != null_ptid)
-    tp = inferior_thread ();
-  else
-    tp = NULL;
+  thread_info *tp = inferior_ptid != null_ptid ? inferior_thread () : nullptr;
+  ui_out *mi_uiout = this->interp_ui_out ();
+  ui_out_redirect_pop redirect_popper (mi_uiout, this->event_channel);
 
-  SWITCH_THRU_ALL_UIS ()
-    {
-      struct mi_interp *mi = as_mi_interp (top_level_interpreter ());
-      struct ui_out *mi_uiout;
+  target_terminal::scoped_restore_terminal_state term_state;
+  target_terminal::ours_for_output ();
 
-      if (mi == NULL)
-	continue;
+  if (selection & USER_SELECTED_INFERIOR)
+    print_selected_inferior (this->cli_uiout);
 
-      mi_uiout = top_level_interpreter ()->interp_ui_out ();
-
-      ui_out_redirect_pop redirect_popper (mi_uiout, mi->event_channel);
-
-      target_terminal::scoped_restore_terminal_state term_state;
-      target_terminal::ours_for_output ();
+  if (tp != NULL
+      && (selection & (USER_SELECTED_THREAD | USER_SELECTED_FRAME)))
+    {
+      print_selected_thread_frame (this->cli_uiout, selection);
 
-      if (selection & USER_SELECTED_INFERIOR)
-	print_selected_inferior (mi->cli_uiout);
+      gdb_printf (this->event_channel, "thread-selected,id=\"%d\"",
+		  tp->global_num);
 
-      if (tp != NULL
-	  && (selection & (USER_SELECTED_THREAD | USER_SELECTED_FRAME)))
+      if (tp->state != THREAD_RUNNING)
 	{
-	  print_selected_thread_frame (mi->cli_uiout, selection);
-
-	  gdb_printf (mi->event_channel,
-		      "thread-selected,id=\"%d\"",
-		      tp->global_num);
-
-	  if (tp->state != THREAD_RUNNING)
-	    {
-	      if (has_stack_frames ())
-		print_stack_frame_to_uiout (mi_uiout, get_selected_frame (NULL),
-					    1, SRC_AND_LOC, 1);
-	    }
+	  if (has_stack_frames ())
+	    print_stack_frame_to_uiout (mi_uiout, get_selected_frame (NULL),
+					1, SRC_AND_LOC, 1);
 	}
-
-      gdb_flush (mi->event_channel);
     }
+
+  gdb_flush (this->event_channel);
 }
 
 ui_out *
@@ -1233,6 +1213,4 @@ _initialize_mi_interp ()
   gdb::observers::command_param_changed.attach (mi_command_param_changed,
 						"mi-interp");
   gdb::observers::memory_changed.attach (mi_memory_changed, "mi-interp");
-  gdb::observers::user_selected_context_changed.attach
-    (mi_user_selected_context_changed, "mi-interp");
 }
diff --git a/gdb/mi/mi-interp.h b/gdb/mi/mi-interp.h
index 08edaceb99cd..430c61dbbf0a 100644
--- a/gdb/mi/mi-interp.h
+++ b/gdb/mi/mi-interp.h
@@ -49,6 +49,7 @@ class mi_interp final : public interp
   void on_no_history () override;
   void on_sync_execution_done () override;
   void on_command_error () override;
+  void on_user_selected_context_changed (user_selected_what selection) override;
 
   /* MI's output channels */
   mi_console_file *out;
diff --git a/gdb/mi/mi-main.c b/gdb/mi/mi-main.c
index 1be5d3becfcf..29bd236ef711 100644
--- a/gdb/mi/mi-main.c
+++ b/gdb/mi/mi-main.c
@@ -2107,7 +2107,7 @@ mi_cmd_execute (struct mi_parse *parse)
 
   if (!parse->cmd->preserve_user_selected_context ()
       && current_user_selected_context.has_changed ())
-    gdb::observers::user_selected_context_changed.notify
+    interps_notify_user_selected_context_changed
       (USER_SELECTED_THREAD | USER_SELECTED_FRAME);
 }
 
diff --git a/gdb/stack.c b/gdb/stack.c
index b1b25aa1c7e1..474f1ce5b879 100644
--- a/gdb/stack.c
+++ b/gdb/stack.c
@@ -1837,7 +1837,7 @@ select_frame_command_core (frame_info_ptr fi, bool ignored)
   frame_info_ptr prev_frame = get_selected_frame ();
   select_frame (fi);
   if (get_selected_frame () != prev_frame)
-    gdb::observers::user_selected_context_changed.notify (USER_SELECTED_FRAME);
+    notify_user_selected_context_changed (USER_SELECTED_FRAME);
 }
 
 /* The core of all the "frame" sub-commands.  Select frame FI, and if this
@@ -1850,7 +1850,7 @@ frame_command_core (frame_info_ptr fi, bool ignored)
   frame_info_ptr prev_frame = get_selected_frame ();
   select_frame (fi);
   if (get_selected_frame () != prev_frame)
-    gdb::observers::user_selected_context_changed.notify (USER_SELECTED_FRAME);
+    notify_user_selected_context_changed (USER_SELECTED_FRAME);
   else
     print_selected_thread_frame (current_uiout, USER_SELECTED_FRAME);
 }
@@ -2650,7 +2650,7 @@ static void
 up_command (const char *count_exp, int from_tty)
 {
   up_silently_base (count_exp);
-  gdb::observers::user_selected_context_changed.notify (USER_SELECTED_FRAME);
+  notify_user_selected_context_changed (USER_SELECTED_FRAME);
 }
 
 /* Select the frame down one or COUNT_EXP stack levels from the previously
@@ -2689,7 +2689,7 @@ static void
 down_command (const char *count_exp, int from_tty)
 {
   down_silently_base (count_exp);
-  gdb::observers::user_selected_context_changed.notify (USER_SELECTED_FRAME);
+  notify_user_selected_context_changed (USER_SELECTED_FRAME);
 }
 
 void
diff --git a/gdb/thread.c b/gdb/thread.c
index e9432f945e09..13db9f8747ea 100644
--- a/gdb/thread.c
+++ b/gdb/thread.c
@@ -1846,10 +1846,8 @@ thread_command (const char *tidstr, int from_tty)
 				       | USER_SELECTED_FRAME);
 	}
       else
-	{
-	  gdb::observers::user_selected_context_changed.notify
-	    (USER_SELECTED_THREAD | USER_SELECTED_FRAME);
-	}
+	notify_user_selected_context_changed
+	  (USER_SELECTED_THREAD | USER_SELECTED_FRAME);
     }
 }
 
-- 
2.40.1


  parent reply	other threads:[~2023-05-02 20:52 UTC|newest]

Thread overview: 39+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-05-02 20:49 [PATCH 00/30] Switch interpreters to use virtual methods Simon Marchi
2023-05-02 20:49 ` [PATCH 01/30] gdb/mi: fix ^running record with multiple MI interpreters Simon Marchi
2023-05-03 14:42   ` Alexandra Petlanova Hajkova
2023-05-03 15:02     ` Simon Marchi
2023-05-29 14:53   ` Simon Marchi
2023-05-02 20:49 ` [PATCH 02/30] gdb/mi: make current_token a field of mi_interp Simon Marchi
2023-05-04 12:31   ` Alexandra Petlanova Hajkova
2023-05-02 20:49 ` [PATCH 03/30] gdb: add interp::on_signal_received method Simon Marchi
2023-05-04 14:38   ` Alexandra Petlanova Hajkova
2023-05-02 20:49 ` [PATCH 04/30] gdb: add interp::on_normal_stop method Simon Marchi
2023-05-02 20:49 ` [PATCH 05/30] gdb: add interp::on_signal_exited method Simon Marchi
2023-05-02 20:49 ` [PATCH 06/30] gdb: add interp::on_exited method Simon Marchi
2023-05-02 20:49 ` [PATCH 07/30] gdb: add interp::on_no_history method Simon Marchi
2023-05-02 20:49 ` [PATCH 08/30] gdb: add interp::on_sync_execution_done method Simon Marchi
2023-05-02 20:49 ` [PATCH 09/30] gdb: add interp::on_command_error method Simon Marchi
2023-05-02 20:49 ` Simon Marchi [this message]
2023-05-02 20:49 ` [PATCH 11/30] gdb: add interp::on_new_thread method Simon Marchi
2023-05-02 20:49 ` [PATCH 12/30] gdb: add interp::on_thread_exited method Simon Marchi
2023-05-02 20:49 ` [PATCH 13/30] gdb: add interp::on_inferior_added method Simon Marchi
2023-05-02 20:49 ` [PATCH 14/30] gdb: add interp::on_inferior_appeared method Simon Marchi
2023-05-02 20:49 ` [PATCH 15/30] gdb: add interp::on_inferior_disappeared method Simon Marchi
2023-05-02 20:49 ` [PATCH 16/30] gdb: add interp::on_inferior_removed method Simon Marchi
2023-05-02 20:49 ` [PATCH 17/30] gdb: add interp::on_record_changed method Simon Marchi
2023-05-02 20:49 ` [PATCH 18/30] gdb: add interp::on_target_resumed method Simon Marchi
2023-05-02 20:49 ` [PATCH 19/30] gdb: add interp::on_solib_loaded method Simon Marchi
2023-05-02 20:50 ` [PATCH 20/30] gdb: add interp::on_solib_unloaded method Simon Marchi
2023-05-02 20:50 ` [PATCH 21/30] gdb: add interp::on_about_to_proceed method Simon Marchi
2023-05-02 20:50 ` [PATCH 22/30] gdb: add interp::on_traceframe_changed method Simon Marchi
2023-05-02 20:50 ` [PATCH 23/30] gdb: add interp::on_tsv_created method Simon Marchi
2023-05-02 20:50 ` [PATCH 24/30] gdb: add interp::on_tsv_deleted method Simon Marchi
2023-05-02 20:50 ` [PATCH 25/30] gdb: add interp::on_tsv_modified method Simon Marchi
2023-05-02 20:50 ` [PATCH 26/30] gdb: add interp::on_breakpoint_created method Simon Marchi
2023-05-02 20:50 ` [PATCH 27/30] gdb: add interp::on_breakpoint_deleted method Simon Marchi
2023-05-02 20:50 ` [PATCH 28/30] gdb: add interp::on_breakpoint_modified method Simon Marchi
2023-05-02 20:50 ` [PATCH 29/30] gdb: add interp::on_param_changed method Simon Marchi
2023-05-02 20:50 ` [PATCH 30/30] gdb: add interp::on_memory_changed method Simon Marchi
2023-05-02 20:50 ` [PATCH 00/30] Make interpreters use virtual methods (instead of observers) Simon Marchi
2023-05-23 12:43 ` [PATCH 00/30] Switch interpreters to use virtual methods Simon Marchi
2023-05-30 19:09   ` Simon Marchi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230502205011.132151-11-simon.marchi@efficios.com \
    --to=simon.marchi@efficios.com \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).