[2/3,gdb/tui] Use ">" marker in register window

Message ID 20230516084352.27225-3-tdevries@suse.de
State New
Headers
Series Allow highlighting in windows contents to be switched off |

Commit Message

Tom de Vries May 16, 2023, 8:43 a.m. UTC
  In TUI's source and assembly windows, we show the current execution point
using both:
- highlighting, and
- a ">" marker.

In TUI's register window, we show changed registers just using highlighting.

Make behaviour more similar by also using a ">" marker for changed registers.

This in preparation for a "set tui contents-highlight on/off" that optionally
disables the highlighting, leaving just the ">" markers.

Tested on x86_64-linux.
---
 gdb/testsuite/gdb.tui/regs-2.c   | 36 +++++++++++++++++
 gdb/testsuite/gdb.tui/regs-2.exp | 66 ++++++++++++++++++++++++++++++++
 gdb/tui/tui-regs.c               | 45 +++++++++++++++++++++-
 3 files changed, 145 insertions(+), 2 deletions(-)
 create mode 100644 gdb/testsuite/gdb.tui/regs-2.c
 create mode 100644 gdb/testsuite/gdb.tui/regs-2.exp
  

Patch

diff --git a/gdb/testsuite/gdb.tui/regs-2.c b/gdb/testsuite/gdb.tui/regs-2.c
new file mode 100644
index 00000000000..d65293e31b2
--- /dev/null
+++ b/gdb/testsuite/gdb.tui/regs-2.c
@@ -0,0 +1,36 @@ 
+/* This testcase is part of GDB, the GNU debugger.
+
+   Copyright 2023 Free Software Foundation, Inc.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+int
+foo (void)
+{
+  return 1;
+}
+
+int
+bar (void)
+{
+  return 2;
+}
+
+int
+main (void)
+{
+  foo ();
+  bar ();
+  return 0;
+}
diff --git a/gdb/testsuite/gdb.tui/regs-2.exp b/gdb/testsuite/gdb.tui/regs-2.exp
new file mode 100644
index 00000000000..50f3d48512d
--- /dev/null
+++ b/gdb/testsuite/gdb.tui/regs-2.exp
@@ -0,0 +1,66 @@ 
+# Copyright 2023 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+# Test of ">" marker in TUI register window.
+
+require allow_tui_tests
+require {!is_remote host}
+require is_x86_64_m64_target
+
+tuiterm_env
+
+standard_testfile
+
+if { [build_executable "failed to prepare" ${testfile} ${srcfile}] == -1 } {
+    return -1
+}
+
+set cols 80
+set lines 24
+set reg_lines 8
+set screen_dim [list $lines $cols]
+set reg_box [list 0 0 $cols $reg_lines]
+
+Term::clean_restart {*}$screen_dim $testfile
+
+if {![runto_main]} {
+    perror "test suppressed"
+    return
+}
+
+if {![Term::enter_tui]} {
+    unsupported "TUI not supported"
+    return
+}
+
+Term::command "layout regs"
+Term::check_box "register box" {*}$reg_box
+
+set re_border "\\|"
+set re_ws " "
+set re_any "\[^\r\n\]"
+
+set re "B\\+>$re_ws*$decimal$re_ws*foo \\(\\);$re_ws*"
+Term::check_contents "before call to foo" "$re_border$re$re_border"
+
+# Step over foo, set rax to 1.
+Term::command "next"
+set re "rax$re_ws*0x1$re_ws$re_any*"
+Term::check_contents "rax after foo" "$re_border$re$re_border"
+
+# Step over bar, set rax to 2.  Check that the ">" marker is used.
+Term::command "next"
+set re "rax$re_ws*>0x2$re_ws$re_any*"
+Term::check_contents "rax after bar" "$re_border$re$re_border"
diff --git a/gdb/tui/tui-regs.c b/gdb/tui/tui-regs.c
index 50a238401df..af3b21b0f9d 100644
--- a/gdb/tui/tui-regs.c
+++ b/gdb/tui/tui-regs.c
@@ -494,8 +494,49 @@  tui_data_item_window::rerender (WINDOW *handle, int field_width)
        to code that causes the compiler to generate an unused-value
        warning.  */
     (void) wstandout (handle);
-      
-  mvwaddnstr (handle, y, x, content.c_str (), field_width - 1);
+
+  const char *s = content.c_str ();
+  int print_width = field_width - 1;
+  if (highlight)
+    {
+      off_t last_space_offset;
+
+      /* Calculate last_space_offset.  */
+      {
+	const char *i = s;
+	const char *last_space = nullptr;
+	/* Skip register name.  */
+	while (*i != ' ')
+	  i++;
+	/* Find last space before value.  */
+	while (*i == ' ')
+	  {
+	    last_space = i;
+	    i++;
+	  }
+	last_space_offset = last_space - s;
+      }
+
+      /* Write the bit before the last space.  */
+      int total = 0;
+      int n = last_space_offset;
+      n = std::min (n, print_width);
+      mvwaddnstr (handle, y, x, s, n);
+      total += n;
+      print_width -= n;
+
+      /* Replace the last space with ">".  */
+      n = 1;
+      n = std::min (n, print_width);
+      waddnstr (handle, ">", n);
+      total += n;
+      print_width -= n;
+
+      /* Write the rest.  */
+      waddnstr (handle, s + total, print_width);
+    }
+  else
+    mvwaddnstr (handle, y, x, s, print_width);
   if (content.size () < field_width)
     waddstr (handle, n_spaces (field_width - content.size ()));