gdb: add interp::on_user_selected_context_changed method
authorSimon Marchi <simon.marchi@efficios.com>
Thu, 20 Apr 2023 20:07:12 +0000 (16:07 -0400)
committerSimon Marchi <simon.marchi@efficios.com>
Tue, 30 May 2023 19:07:26 +0000 (15:07 -0400)
Same as previous patches, but for user_selected_context_changed.

Change-Id: I40de15be897671227d4bcf3e747f0fd595f0d5be

12 files changed:
gdb/cli/cli-interp.c
gdb/cli/cli-interp.h
gdb/inferior.c
gdb/infrun.c
gdb/infrun.h
gdb/interps.c
gdb/interps.h
gdb/mi/mi-interp.c
gdb/mi/mi-interp.h
gdb/mi/mi-main.c
gdb/stack.c
gdb/thread.c

index ffb40729fc61d56944aea3502cb72babc875f624..6a175f7baa1393fda645dcd4da2e20418bfb7d61 100644 (file)
@@ -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");
 }
index 5020b2bc6ef44a9b341669d8d179aa44a02a339e..a1a20b678942b612e37a5a61722a72dfdd51e4a7 100644 (file)
@@ -40,6 +40,7 @@ public:
   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
index fd451c87e709d390ad6959df2df16f6266befa52..8962b6401e3498033f1865841dd7bb420b6c2320 100644 (file)
@@ -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);
        }
     }
index 2d234edfbccf14aab5aa8871b76255273f014d93..d4334302a9963f6ffa6413c697f5334251fa68c9 100644 (file)
@@ -6283,6 +6283,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
index cbafc3ea440714b673cc3ddf9ccc2e29b591957c..a343d27f72de4d9ff85d27163504fa70dfa8eea6 100644 (file)
@@ -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.  */
 
index 885bfc36ec70cdd427bcfb29780bc3cec334edab..55abcb3c6e40207f5fc805be95fe3a99f6b18fd6 100644 (file)
@@ -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
index 53d951b8d637b2e940d3e47fb4affde249f236fa..4706c9bb63e72c8e8c57dd683f127c34f2ad45c2 100644 (file)
@@ -110,6 +110,10 @@ public:
      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"
index 313d75185bceda6213d3d2e3c2dd890f941960fb..2984cb9af11e017c18f62baacde9c6ca8d6b6c16 100644 (file)
@@ -1087,60 +1087,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;
-
-  SWITCH_THRU_ALL_UIS ()
-    {
-      struct mi_interp *mi = as_mi_interp (top_level_interpreter ());
-      struct ui_out *mi_uiout;
-
-      if (mi == NULL)
-       continue;
+  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);
 
-      mi_uiout = top_level_interpreter ()->interp_ui_out ();
+  target_terminal::scoped_restore_terminal_state term_state;
+  target_terminal::ours_for_output ();
 
-      ui_out_redirect_pop redirect_popper (mi_uiout, mi->event_channel);
+  if (selection & USER_SELECTED_INFERIOR)
+    print_selected_inferior (this->cli_uiout);
 
-      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 *
@@ -1234,6 +1214,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");
 }
index 6a02eeff8c1972680f6c0ab43fbafcc912f25ce0..bb66e6304329b85f578b758e485d58d1900b2463 100644 (file)
@@ -49,6 +49,7 @@ public:
   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;
index dc7d717ef60891fa12f913004f7d69e00edb8b88..7503ffd2f1836f2158bfe2378458de9f922e8771 100644 (file)
@@ -2138,7 +2138,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);
 }
 
index e941eb639dfd24af0f9a46c3ef0d0969ecfca58f..002bf58063454c20f70a9ba05be2f6cf711ad54f 100644 (file)
@@ -1831,7 +1831,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
@@ -1844,7 +1844,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);
 }
@@ -2644,7 +2644,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
@@ -2683,7 +2683,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
index e9432f945e098c0a0284160e35c9ac2a36d4299d..13db9f8747ea673548327a7412dfb289a673a85f 100644 (file)
@@ -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);
     }
 }