[3/3] Have TUI react to disassembly parameter changes

Message ID 20251231-param-set-observ-v1-3-c6a63695f54d@tromey.com
State New
Headers
Series Update TUI disassembly window when parameters change |

Commit Message

Tom Tromey Dec. 31, 2025, 11:18 p.m. UTC
  With this patch, changes to "print symbol-filename",
"filename-display", or "print max-symbolic-offset" will now cause the
TUI disassembly window to update.

tui_source_window_base::style_changed is renamed to reflect its use as
a way to force a refresh.

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=33249
---
 gdb/testsuite/gdb.tui/tui-disasm-styling.exp | 23 +++++++++++++++++++++++
 gdb/tui/tui-disasm.c                         | 23 +++++++++++++++++++++++
 gdb/tui/tui-disasm.h                         | 10 +++++++++-
 gdb/tui/tui-source.c                         |  2 +-
 gdb/tui/tui-winsource.c                      |  4 ++--
 gdb/tui/tui-winsource.h                      |  5 +++--
 6 files changed, 61 insertions(+), 6 deletions(-)
  

Patch

diff --git a/gdb/testsuite/gdb.tui/tui-disasm-styling.exp b/gdb/testsuite/gdb.tui/tui-disasm-styling.exp
index 685974411dfbba1d83104056812447585ee6a41f..311e7af876324cc38d61947fba39c61482098777 100644
--- a/gdb/testsuite/gdb.tui/tui-disasm-styling.exp
+++ b/gdb/testsuite/gdb.tui/tui-disasm-styling.exp
@@ -66,3 +66,26 @@  check_asm_output true "asm output is styled once again"
 
 Term::command "set style sources off"
 check_asm_output true "asm output is styled when source styling is off"
+
+Term::command "set print max-symbolic-offset 1"
+set asm_output [Term::get_region 1 1 78 13 "\n"]
+# Look for a line that has a hex offset and some assembly, but that
+# does not have <main...>.  We can't really check for the assembly
+# directly so we just look for any letter.
+gdb_assert {[regexp -- "$hex\[^<\]* \[a-z\]" $asm_output]} \
+    "reacted to change to max-symbolic-offset"
+
+# These next tests are a bit simpler to write without styling.
+Term::command "set style enabled off"
+Term::command "set filename-display absolute"
+Term::command "set print symbol-filename on"
+set asm_output [Term::get_region 1 1 78 13 "\n"]
+# We can't assume the final part of the filename will be visible, so
+# just look for any directory separator.
+gdb_assert {[regexp -- "<main at \[/\\\\]" $asm_output]} \
+    "reacted to change to symbol-filename"
+
+Term::command "set filename-display basename"
+set asm_output [Term::get_region 1 1 78 13 "\n"]
+gdb_assert {[regexp -- "<main at tui-layout\\.c" $asm_output]} \
+    "reacted to change to filename-display"
diff --git a/gdb/tui/tui-disasm.c b/gdb/tui/tui-disasm.c
index b49a33b8413620ad76f01ed8159a88b62d8b6afe..05b87c26a49cb8ec03a4f965edf6ee846a941f55 100644
--- a/gdb/tui/tui-disasm.c
+++ b/gdb/tui/tui-disasm.c
@@ -38,6 +38,7 @@ 
 #include "tui/tui-location.h"
 #include "gdbsupport/selftest.h"
 #include "inferior.h"
+#include "observable.h"
 
 struct tui_asm_line
 {
@@ -526,6 +527,28 @@  tui_disasm_window::display_start_addr (struct gdbarch **gdbarch_p,
   *addr_p = m_start_line_or_addr.u.addr;
 }
 
+void
+tui_disasm_window::param_changed (const char *name, const char *value)
+{
+  if (strcmp (name, "print max-symbolic-offset") == 0
+      || strcmp (name, "print symbol-filename") == 0
+      || strcmp (name, "filename-display") == 0)
+    re_render ();
+}
+
+tui_disasm_window::tui_disasm_window ()
+{
+  using namespace std::placeholders;
+  gdb::observers::parameter_changed.attach
+    (std::bind (&tui_disasm_window::param_changed, this, _1, _2),
+     m_dis_observable, "tui-disasm");
+}
+
+tui_disasm_window::~tui_disasm_window ()
+{
+  gdb::observers::parameter_changed.detach (m_dis_observable);
+}
+
 #if GDB_SELF_TEST
 namespace selftests {
 namespace tui {
diff --git a/gdb/tui/tui-disasm.h b/gdb/tui/tui-disasm.h
index 9ebcdf9b26cd791a75a2802a6c4b51705e8fd8fe..70ff1cc31e3c1c8ff586da6f2122dc2046dbddea 100644
--- a/gdb/tui/tui-disasm.h
+++ b/gdb/tui/tui-disasm.h
@@ -31,7 +31,8 @@ 
 
 struct tui_disasm_window : public tui_source_window_base
 {
-  tui_disasm_window () = default;
+  tui_disasm_window ();
+  ~tui_disasm_window ();
 
   DISABLE_COPY_AND_ASSIGN (tui_disasm_window);
 
@@ -60,9 +61,16 @@  struct tui_disasm_window : public tui_source_window_base
 		     const struct symtab_and_line &sal) override;
 
 private:
+  /* Observer for the parameter changed event.  This updates the
+     contents in response to certain parameter changes.  */
+  void param_changed (const char *name, const char *value);
+
   /* Answer whether a particular line number or address is displayed
      in the current source window.  */
   bool addr_is_displayed (CORE_ADDR addr) const;
+
+  /* A token used to register and unregister an observer.  */
+  gdb::observers::token m_dis_observable;
 };
 
 /* Return the instance of the disassembly windows.  */
diff --git a/gdb/tui/tui-source.c b/gdb/tui/tui-source.c
index 4a2f42352739722bc44dc2ff739639c8d4b2d650..10d2072d0739e748173934cf74a7623f791eab62 100644
--- a/gdb/tui/tui-source.c
+++ b/gdb/tui/tui-source.c
@@ -38,7 +38,7 @@ 
 tui_source_window::tui_source_window ()
 {
   line_number_style.changed.attach
-    (std::bind (&tui_source_window::style_changed, this),
+    (std::bind (&tui_source_window::re_render, this),
      m_src_observable, "tui-source");
 }
 
diff --git a/gdb/tui/tui-winsource.c b/gdb/tui/tui-winsource.c
index e6bbe1535a3061e2e4915778f464a5ab1e2a1d1e..b51f9ec977643204c35b60ad65c872cb662a1942 100644
--- a/gdb/tui/tui-winsource.c
+++ b/gdb/tui/tui-winsource.c
@@ -136,7 +136,7 @@  tui_copy_source_line (const char **ptr, int *length)
 }
 
 void
-tui_source_window_base::style_changed ()
+tui_source_window_base::re_render ()
 {
   if (tui_active && is_visible ())
     refill ();
@@ -419,7 +419,7 @@  tui_source_window_base::tui_source_window_base ()
   m_start_line_or_addr.u.addr = 0;
 
   gdb::observers::styling_changed.attach
-    (std::bind (&tui_source_window::style_changed, this),
+    (std::bind (&tui_source_window::re_render, this),
      m_observable, "tui-winsource");
 }
 
diff --git a/gdb/tui/tui-winsource.h b/gdb/tui/tui-winsource.h
index b531f0e7f83029a7c4f8e87cada7afb1851e8fde..256284dd1b1f41cba0b4029888e80f52df403cb2 100644
--- a/gdb/tui/tui-winsource.h
+++ b/gdb/tui/tui-winsource.h
@@ -191,8 +191,9 @@  struct tui_source_window_base : public tui_win_info
 
 protected:
 
-  /* Called when a user style setting is changed.  */
-  void style_changed ();
+  /* Re-render the window from scratch.  This is called in response to
+     a style change or similar event.  */
+  void re_render ();
 
 private: