Skip to content

Commit

Permalink
gdb/tui: asm window handles invalid memory and scrolls better
Browse files Browse the repository at this point in the history
This started as a patch to enable the asm window to handle attempts to
disassemble invalid memory, but it ended up expanding into a
significant rewrite of how the asm window handles scrolling.  These
two things ended up being tied together as it was impossible to
correctly test scrolling into invalid memory when the asm window would
randomly behave weirdly while scrolling.

Things that should work nicely now; scrolling to the bottom or top of
the listing with PageUp, PageDown, Up Arrow, Down Arrow and we should
be able to scroll past small areas of memory that don't have symbols
associated with them.  It should also be possible to scroll to the
start of a section even if there's no symbol at the start of the
section.

Adding tests for this scrolling was a little bit of a problem.  First
I would have liked to add tests for PageUp / PageDown, but the tuiterm
library we use doesn't support these commands right now due to only
emulating a basic ascii terminal.  Changing this to emulate a more
complex terminal would require adding support for more escape sequence
control codes, so I've not tried to tackle that in this patch.

Next, I would have liked to test scrolling to the start or end of the
assembler listing and then trying to scroll even more, however, this
is a problem because in a well behaving GDB a scroll at the start/end
has no effect.  What we need to do is:

  - Move to start of assembler listing,
  - Send scroll up command,
  - Wait for all curses output,
  - Ensure the assembler listing is unchanged, we're still at the
    start of the listing.

The problem is that there is no curses output, so how long do we wait
at step 3?  The same problem exists for scrolling to the bottom of the
assembler listing.  However, when scrolling down you can at least see
the end coming, so I added a test for this case, however, this feels
like an area of code that is massively under tested.

gdb/ChangeLog:

	PR tui/9765
	* minsyms.c (lookup_minimal_symbol_by_pc_section): Update header
	comment, add extra parameter, and update to store previous symbol
	when appropriate.
	* minsyms.h (lookup_minimal_symbol_by_pc_section): Update comment,
	add extra parameter.
	* tui/tui-disasm.c (tui_disassemble): Update header comment,
	remove unneeded parameter, add try/catch around gdb_print_insn,
	rewrite to add items to asm_lines vector.
	(tui_find_backward_disassembly_start_address): New function.
	(tui_find_disassembly_address): Updated throughout.
	(tui_disasm_window::set_contents): Update for changes to
	tui_disassemble.
	(tui_disasm_window::do_scroll_vertical): No need to adjust the
	number of lines to scroll.

gdb/testsuite/ChangeLog:

	PR tui/9765
	* gdb.tui/tui-layout-asm.exp: Add scrolling test for asm window.

Change-Id: I323987c8fd316962c937e73c17d952ccd3cfa66c
  • Loading branch information
T-J-Teru committed Jan 24, 2020
1 parent 2f26767 commit 733d0a6
Show file tree
Hide file tree
Showing 6 changed files with 288 additions and 78 deletions.
18 changes: 18 additions & 0 deletions gdb/ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
2020-01-24 Andrew Burgess <[email protected]>

PR tui/9765
* minsyms.c (lookup_minimal_symbol_by_pc_section): Update header
comment, add extra parameter, and update to store previous symbol
when appropriate.
* minsyms.h (lookup_minimal_symbol_by_pc_section): Update comment,
add extra parameter.
* tui/tui-disasm.c (tui_disassemble): Update header comment,
remove unneeded parameter, add try/catch around gdb_print_insn,
rewrite to add items to asm_lines vector.
(tui_find_backward_disassembly_start_address): New function.
(tui_find_disassembly_address): Updated throughout.
(tui_disasm_window::set_contents): Update for changes to
tui_disassemble.
(tui_disasm_window::do_scroll_vertical): No need to adjust the
number of lines to scroll.

2020-01-23 Simon Marchi <[email protected]>

* objfiles.h (ALL_OBJFILE_OSECTIONS): Move up.
Expand Down
41 changes: 28 additions & 13 deletions gdb/minsyms.c
Original file line number Diff line number Diff line change
Expand Up @@ -666,24 +666,18 @@ msym_prefer_to_msym_type (lookup_msym_prefer prefer)
gdb_assert_not_reached ("unhandled lookup_msym_prefer");
}

/* Search through the minimal symbol table for each objfile and find
the symbol whose address is the largest address that is still less
than or equal to PC, and matches SECTION (which is not NULL).
Returns a pointer to the minimal symbol if such a symbol is found,
or NULL if PC is not in a suitable range.
/* See minsyms.h.
Note that we need to look through ALL the minimal symbol tables
before deciding on the symbol that comes closest to the specified PC.
This is because objfiles can overlap, for example objfile A has .text
at 0x100 and .data at 0x40000 and objfile B has .text at 0x234 and
.data at 0x40048.
If WANT_TRAMPOLINE is set, prefer mst_solib_trampoline symbols when
there are text and trampoline symbols at the same address.
Otherwise prefer mst_text symbols. */
.data at 0x40048. */

bound_minimal_symbol
lookup_minimal_symbol_by_pc_section (CORE_ADDR pc_in, struct obj_section *section,
lookup_msym_prefer prefer)
lookup_msym_prefer prefer,
bound_minimal_symbol *previous)
{
int lo;
int hi;
Expand All @@ -693,6 +687,12 @@ lookup_minimal_symbol_by_pc_section (CORE_ADDR pc_in, struct obj_section *sectio
struct objfile *best_objfile = NULL;
struct bound_minimal_symbol result;

if (previous != nullptr)
{
previous->minsym = nullptr;
previous->objfile = nullptr;
}

if (section == NULL)
{
section = find_pc_section (pc_in);
Expand Down Expand Up @@ -886,8 +886,23 @@ lookup_minimal_symbol_by_pc_section (CORE_ADDR pc_in, struct obj_section *sectio
if (best_zero_sized != -1)
hi = best_zero_sized;
else
/* Go on to the next object file. */
continue;
{
/* If needed record this symbol as the closest
previous symbol. */
if (previous != nullptr)
{
if (previous->minsym == nullptr
|| (MSYMBOL_VALUE_RAW_ADDRESS (&msymbol[hi])
> MSYMBOL_VALUE_RAW_ADDRESS
(previous->minsym)))
{
previous->minsym = &msymbol[hi];
previous->objfile = objfile;
}
}
/* Go on to the next object file. */
continue;
}
}

/* The minimal symbol indexed by hi now is the best one in this
Expand Down
17 changes: 12 additions & 5 deletions gdb/minsyms.h
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,9 @@ enum class lookup_msym_prefer

/* Search through the minimal symbol table for each objfile and find
the symbol whose address is the largest address that is still less
than or equal to PC, and which matches SECTION.
than or equal to PC_IN, and which matches SECTION. A matching symbol
must either be zero sized and have address PC_IN, or PC_IN must fall
within the range of addresses covered by the matching symbol.
If SECTION is NULL, this uses the result of find_pc_section
instead.
Expand All @@ -249,12 +251,17 @@ enum class lookup_msym_prefer
found, or NULL if PC is not in a suitable range.
See definition of lookup_msym_prefer for description of PREFER. By
default mst_text symbols are preferred. */
default mst_text symbols are preferred.
If the PREVIOUS pointer is non-NULL, and no matching symbol is found,
then the contents will be set to reference the closest symbol before
PC_IN. */

struct bound_minimal_symbol lookup_minimal_symbol_by_pc_section
(CORE_ADDR,
struct obj_section *,
lookup_msym_prefer prefer = lookup_msym_prefer::TEXT);
(CORE_ADDR pc_in,
struct obj_section *section,
lookup_msym_prefer prefer = lookup_msym_prefer::TEXT,
bound_minimal_symbol *previous = nullptr);

/* Backward compatibility: search through the minimal symbol table
for a matching PC (no section given).
Expand Down
5 changes: 5 additions & 0 deletions gdb/testsuite/ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
2020-01-24 Andrew Burgess <[email protected]>

PR tui/9765
* gdb.tui/tui-layout-asm.exp: Add scrolling test for asm window.

2020-01-19 Tom Tromey <[email protected]>

* gdb.tui/main.exp: Add check for plain "file".
Expand Down
42 changes: 42 additions & 0 deletions gdb/testsuite/gdb.tui/tui-layout-asm.exp
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,45 @@ if {![Term::prepare_for_tui]} {
# This puts us into TUI mode, and should display the ASM window.
Term::command "layout asm"
Term::check_box_contents "check asm box contents" 0 0 80 15 "<main>"

# Scroll the ASM window down using the down arrow key. In an ideal
# world we'd like to use PageDown here, but currently our terminal
# library doesn't support such advanced things.
set testname "scroll to end of assembler"
set down_count 0
while (1) {
# Grab the second line, this is about to become the first line.
set line [Term::get_line 2]

# Except, if the second line is blank then we are at the end of
# the available asm output. Pressing down again _shouldn't_
# change the output, however, if GDB is working, and we press down
# then the screen won't change, so the call to Term::wait_for
# below will just timeout. So for now we avoid testing the edge
# case.
if {[regexp -- "^\\| +\\|$" $line]} {
# Second line is blank, we're at the end of the assembler.
pass $testname
break
}

# Send the down key to GDB.
send_gdb "\033\[B"
incr down_count
if {[Term::wait_for [string_to_regexp $line]] \
&& [Term::get_line 1] == $line} {
# We scrolled successfully.
} else {
fail "$testname (scroll failed)"
Term::dump_screen
break
}

if { $down_count > 250 } {
# Maybe we should accept this as a pass in case a target
# really does have loads of assembler to scroll through.
fail "$testname (too much assembler)"
Term::dump_screen
break
}
}
Loading

0 comments on commit 733d0a6

Please sign in to comment.