[PATCHv2 08/16] gdb/tui: fix 'tui reg next/prev' command when data window is hidden

Andrew Burgess via Gdb-patches
2022-04-06T12:09:54Z

Start GDB like:

$ gdb -q executable
(gdb) start
(gdb) layout src
... tui windows are now displayed ...
(gdb) tui reg next

At this point the data (register) window should be displayed, but will
contain the message 'Register Values Unavailable', and at the console
you'll see the message "unknown register group 'next'".

The same happens with 'tui reg prev' (but the error message is
slightly different).

At this point you can continue to use 'tui reg next' and/or 'tui reg
prev' and you'll keep getting the error message.

The problem is that when the data (register) window is first
displayed, it's current register group is nullptr. As a consequence
tui_reg_next and tui_reg_prev (tui/tui-regs.c) will always just return
nullptr, which triggers an error in tui_reg_command.

In this commit I change tui_reg_next and tui_reg_prev so that they
instead return the first and last register group respectively if the
current register group is nullptr.

So, after this, using 'tui reg next' will (in the above case) show the
first register group, while 'tui reg prev' will display the last
register group.
---
gdb/testsuite/gdb.tui/regs.exp | 26 ++++++++++++++++++++++++++
gdb/tui/tui-regs.c | 34 ++++++++++++++--------------------
2 files changed, 40 insertions(+), 20 deletions(-)

diff --git a/gdb/testsuite/gdb.tui/regs.exp b/gdb/testsuite/gdb.tui/regs.exp
index 2f3482f5d38..4f34ced990c 100644
--- a/gdb/testsuite/gdb.tui/regs.exp
+++ b/gdb/testsuite/gdb.tui/regs.exp
@@ -44,3 +44,29 @@ Term::check_box "source box in regs layout" 0 7 80 8
set text [Term::get_line 1]
# Just check for any register window content at all.
Term::check_contents "any register contents" "\\|.*\[^ \].*\\|"
+
+
+# Check that we can successfully cause the register window to appear
+# using the 'tui reg next' and 'tui reg prev' commands.
+foreach_with_prefix cmd { next prev } {
+ Term::clean_restart 24 80 $testfile
+
+ if {![runto_main]} {
+ perror "test suppressed"
+ return
+ }
+
+ if {![Term::enter_tui]} {
+ unsupported "TUI not supported"
+ return
+ }
+
+ Term::command "tui reg ${cmd}"
+ Term::check_box "register box" 0 0 80 8
+ Term::check_box "source box in regs layout" 0 7 80 8
+ Term::check_region_contents "check register group title" \
+ 0 0 80 1 "Register group: "
+ set contents [Term::get_region 0 15 80 8 "\r\n"]
+ gdb_assert {![regexp -- "unknown register group '${cmd}'" $contents]} \
+ "check register group is known"
+}
diff --git a/gdb/tui/tui-regs.c b/gdb/tui/tui-regs.c
index 75ffa9babbf..b968947fa1c 100644
--- a/gdb/tui/tui-regs.c
+++ b/gdb/tui/tui-regs.c
@@ -515,38 +515,32 @@ tui_data_item_window::rerender (WINDOW *handle, int field_width)
}

/* Helper for "tui reg next", wraps a call to REGGROUP_NEXT, but adds wrap
- around behaviour. Returns the next register group, or NULL if the
- register window is not currently being displayed. */
+ around behaviour. Will never return nullptr. If CURRENT_GROUP is
+ nullptr (e.g. if the tui register window has only just been displayed
+ and has no current group selected), then the first register group will
+ be returned. */

static const reggroup *
tui_reg_next (const reggroup *current_group, struct gdbarch *gdbarch)
{
- const reggroup *group = NULL;
-
- if (current_group != NULL)
- {
- group = reggroup_next (gdbarch, current_group);
- if (group == NULL)
- group = reggroup_next (gdbarch, NULL);
- }
+ const reggroup *group = reggroup_next (gdbarch, current_group);
+ if (group == NULL)
+ group = reggroup_next (gdbarch, NULL);
return group;
}

/* Helper for "tui reg prev", wraps a call to REGGROUP_PREV, but adds wrap
- around behaviour. Returns the previous register group, or NULL if the
- register window is not currently being displayed. */
+ around behaviour. Will never return nullptr. If CURRENT_GROUP is
+ nullptr (e.g. if the tui register window has only just been displayed
+ and has no current group selected), then the last register group will
+ be returned. */

static const reggroup *
tui_reg_prev (const reggroup *current_group, struct gdbarch *gdbarch)
{
- const reggroup *group = NULL;
-
- if (current_group != NULL)
- {
- group = reggroup_prev (gdbarch, current_group);
- if (group == NULL)
- group = reggroup_prev (gdbarch, NULL);
- }
+ const reggroup *group = reggroup_prev (gdbarch, current_group);
+ if (group == NULL)
+ group = reggroup_prev (gdbarch, NULL);
return group;
}

--
2.25.4