From 02a79309925c71591d825f8fc4e2b79ce0faa45b Mon Sep 17 00:00:00 2001 From: Andrew Burgess Date: Fri, 12 Feb 2021 16:10:56 +0000 Subject: [PATCH] gdb: add a new 'maint info target-sections' command We already have a command 'maint info sections', this command prints all sections from all known object files. However, GDB maintains a second section table internally. This section table is used when GDB wants to read directly from an object file rather than actually reading memory on the target. As such only some sections (the allocatable ones) are added to this secondary section table. I recently ran into a situation where some of GDB's optimisations for reading directly from the files were not working. In 'maint info sections' I could see that GDB knew about the object file, and did know about the sections that it _should_ have been reading from. But I couldn't ask GDB which sections it had copied into its secondary section table. This commit adds a new command 'maint info target-sections' that fills this gap. This command lists only those sections that GDB has copied into its secondary table. You'll notice that the testsuite includes a comment indicating that there's a bug in GDB. Normally this is not something I would add to the testsuite, instead we should raise an actual bugzilla bug and then mark an xfail, however, a later patch in this series will remove this comment once the actual bug in GDB is fixed. gdb/ChangeLog: * NEWS: Mention new 'maint info target-sections' command. * maint.c (maintenance_info_target_sections): New function. (_initialize_maint_cmds): Register new command. gdb/doc/ChangeLog: * gdb.texinfo (Files): Document new 'maint info target-sections' command. gdb/testsuite/ChangeLog: * gdb.base/maint-info-sections.exp: Add new tests. (check_maint_info_target_sections_output): New proc. --- gdb/ChangeLog | 6 ++ gdb/NEWS | 3 + gdb/doc/ChangeLog | 5 ++ gdb/doc/gdb.texinfo | 8 +++ gdb/maint.c | 59 ++++++++++++++++ gdb/testsuite/ChangeLog | 5 ++ .../gdb.base/maint-info-sections.exp | 68 ++++++++++++++++++- 7 files changed, 153 insertions(+), 1 deletion(-) diff --git a/gdb/ChangeLog b/gdb/ChangeLog index e136c98985f..41e480ab6d2 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,3 +1,9 @@ +2021-02-24 Andrew Burgess + + * NEWS: Mention new 'maint info target-sections' command. + * maint.c (maintenance_info_target_sections): New function. + (_initialize_maint_cmds): Register new command. + 2021-02-24 Andrew Burgess * riscv-tdep.c (riscv_features_from_gdbarch_info): Rename to... diff --git a/gdb/NEWS b/gdb/NEWS index 1dfbbc648eb..7f5a745d0c0 100644 --- a/gdb/NEWS +++ b/gdb/NEWS @@ -47,6 +47,9 @@ maintenance flush register-cache maintenance flush dcache A new command to flush the dcache. +maintenance info target-sections + Print GDB's internal target sections table. + * Changed commands break [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] diff --git a/gdb/doc/ChangeLog b/gdb/doc/ChangeLog index 7f324dedade..add31a0cc39 100644 --- a/gdb/doc/ChangeLog +++ b/gdb/doc/ChangeLog @@ -1,3 +1,8 @@ +2021-02-24 Andrew Burgess + + * gdb.texinfo (Files): Document new 'maint info target-sections' + command. + 2021-02-17 Lancelot Six PR cli/17290 diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo index 90f0c7683f6..80ccf74a049 100644 --- a/gdb/doc/gdb.texinfo +++ b/gdb/doc/gdb.texinfo @@ -20668,6 +20668,14 @@ COFF shared library information. Section contains common symbols. @end table @end table + +@kindex maint info target-sections +@item maint info target-sections +This command prints @value{GDBN}'s internal section table. For each +target @value{GDBN} maintains a table containing the allocatable +sections from all currently mapped objects, along with information +about where the section is mapped. + @kindex set trust-readonly-sections @cindex read-only sections @item set trust-readonly-sections on diff --git a/gdb/maint.c b/gdb/maint.c index 707d156ec2a..bfdd1d5ca3e 100644 --- a/gdb/maint.c +++ b/gdb/maint.c @@ -464,6 +464,56 @@ maintenance_info_sections (const char *arg, int from_tty) maint_print_all_sections (_("Core file: "), core_bfd, nullptr, arg); } +/* Implement the "maintenance info target-sections" command. */ + +static void +maintenance_info_target_sections (const char *arg, int from_tty) +{ + bfd *abfd = nullptr; + int digits = 0; + const target_section_table *table + = target_get_section_table (current_top_target ()); + if (table == nullptr) + return; + + for (const target_section &sec : *table) + { + if (abfd == nullptr || sec.the_bfd_section->owner != abfd) + { + abfd = sec.the_bfd_section->owner; + digits = std::max (index_digits (gdb_bfd_count_sections (abfd)), + digits); + } + } + + struct gdbarch *gdbarch = nullptr; + int addr_size = 0; + abfd = nullptr; + for (const target_section &sec : *table) + { + if (sec.the_bfd_section->owner != abfd) + { + abfd = sec.the_bfd_section->owner; + gdbarch = gdbarch_from_bfd (abfd); + addr_size = gdbarch_addr_bit (gdbarch) / 8; + + printf_filtered (_("From '%s', file type %s:\n"), + bfd_get_filename (abfd), bfd_get_target (abfd)); + } + print_bfd_section_info (abfd, + sec.the_bfd_section, + nullptr, + digits); + /* The magic '8 + digits' here ensures that the 'Start' is aligned + with the output of print_bfd_section_info. */ + printf_filtered ("%*sStart: %s, End: %s, Owner token: %p\n", + (8 + digits), "", + hex_string_custom (sec.addr, addr_size), + hex_string_custom (sec.endaddr, addr_size), + sec.owner); + } +} + static void maintenance_print_statistics (const char *args, int from_tty) { @@ -1122,6 +1172,15 @@ Options:\n\ &maintenanceinfolist); set_cmd_completer_handle_brkchars (cmd, maint_info_sections_completer); + add_cmd ("target-sections", class_maintenance, + maintenance_info_target_sections, _("\ +List GDB's internal section table.\n\ +\n\ +Print the current targets section list. This is a sub-set of all\n\ +sections, from all objects currently loaded. Usually the ALLOC\n\ +sectoins."), + &maintenanceinfolist); + add_basic_prefix_cmd ("print", class_maintenance, _("Maintenance command for printing GDB internal state."), &maintenanceprintlist, "maintenance print ", 0, diff --git a/gdb/testsuite/ChangeLog b/gdb/testsuite/ChangeLog index 4d2d5434aa0..ddc7c183bd0 100644 --- a/gdb/testsuite/ChangeLog +++ b/gdb/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2021-02-24 Andrew Burgess + + * gdb.base/maint-info-sections.exp: Add new tests. + (check_maint_info_target_sections_output): New proc. + 2021-02-24 Andrew Burgess * gdb.arch/riscv-default-tdesc.exp: New file. diff --git a/gdb/testsuite/gdb.base/maint-info-sections.exp b/gdb/testsuite/gdb.base/maint-info-sections.exp index 07c53b16177..17e38ebc416 100644 --- a/gdb/testsuite/gdb.base/maint-info-sections.exp +++ b/gdb/testsuite/gdb.base/maint-info-sections.exp @@ -13,7 +13,8 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . -# Test just for the 'maintenance info sections' command. +# Test just for the 'maintenance info sections' command and the +# 'maintenance info target-sections' command. load_lib completion-support.exp @@ -29,6 +30,65 @@ if ![runto_main] then { return -1 } +# Check the output of 'maint info target-sections' command. +proc check_maint_info_target_sections_output {prefix} { + global hex gdb_prompt + + with_test_prefix $prefix { + # Check the output of the 'maint info target-sections' command. + # Ensures that all the lines have the expected format, and that we see + # an auxiliary information line after every section information line. + # + # We also check that every bfd section has the ALLOC flag. + set seen_header false + set seen_a_section false + set seen_aux_info false + set expecting_aux_info false + gdb_test_multiple "maint info target-sections" "" { + -re "^maint info target-sections\r\n" { + # Consume the command we sent to GDB that the terminal echo'd + # back. + exp_continue + } + -re "^From '\[^\r\n\]+', file type \[^\r\n\]+:\r\n" { + # There might be more than one header, but there should be at + # least one. + set seen_header true + if { $expecting_aux_info } { + fail $gdb_test_name + } else { + exp_continue + } + } + -re "^ \\\[\[0-9\]+\\\]\\s+$hex->$hex at $hex: \[^*\r\]+\\yALLOC\\y\[^*\r\]*\r\n" { + # A bfd section description line. + set seen_a_section true + if { $expecting_aux_info } { + fail $gdb_test_name + } else { + set expecting_aux_info true + exp_continue + } + } + -re "^\\s+Start: $hex, End: $hex, Owner token: $hex\r\n" { + # A target section auxiliary information line. + set seen_aux_info true + if { !$expecting_aux_info } { + fail $gdb_test_name + } else { + set expecting_aux_info false + exp_continue + } + } + -re "^$gdb_prompt $" { + gdb_assert { $seen_header && $seen_a_section && $seen_aux_info } + gdb_assert { !$expecting_aux_info } + pass $gdb_test_name + } + } + } +} + # Check that 'maint info sections' output looks correct. When # checking the lines for each section we reject section names starting # with a '*' character, the internal *COM*, *UND*, *ABS*, and *IND* @@ -128,6 +188,8 @@ gdb_test_multiple "maint info sections DATA" "" { } } +check_maint_info_target_sections_output "with executable" + # Restart GDB, but don't load the executable. clean_restart @@ -161,6 +223,10 @@ gdb_test_multiple "maint info sections -all-objects" "" { } } +# NOTE: We would like to check 'maint info target-sections' again +# here, but GDB currently doesn't display the target sections table in +# this case. This is a bug and will be fixed shortly!! + # Test the command line completion on 'maint info sections'. First # the command line flag. test_gdb_complete_unique \ -- 2.30.2