[v4,17/22] Convert all_bfds to new hash table

Message ID 20240823184910.883268-18-simon.marchi@efficios.com
State New
Headers
Series Add a C++ hash table to gdbsupport |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gdb_build--master-arm success Build passed
linaro-tcwg-bot/tcwg_gdb_build--master-aarch64 success Build passed
linaro-tcwg-bot/tcwg_gdb_check--master-aarch64 success Test passed

Commit Message

Simon Marchi Aug. 23, 2024, 6:48 p.m. UTC
  This converts gdb_bfd.c to use the new hash table for all_bfds.

This patch slightly changes the htab_t pretty-printer test, which was
relying on all_bfds.  Note that with the new hash table, gdb-specific
printers aren't needed; the libstdc++ printers suffice -- in fact,
they are better, because the true types of the contents are available.

Change-Id: I48b7bd142085287b34bdef8b6db5587581f94280
Co-Authored-By: Tom Tromey <tom@tromey.com>
---
 gdb/gdb_bfd.c                           | 47 +++++++++----------------
 gdb/testsuite/gdb.gdb/python-helper.exp |  3 +-
 2 files changed, 19 insertions(+), 31 deletions(-)
  

Comments

Tom Tromey Oct. 4, 2024, 8:38 p.m. UTC | #1
>>>>> "Simon" == Simon Marchi <simon.marchi@efficios.com> writes:

Simon> This patch slightly changes the htab_t pretty-printer test, which was
Simon> relying on all_bfds.  Note that with the new hash table, gdb-specific
Simon> printers aren't needed; the libstdc++ printers suffice -- in fact,
Simon> they are better, because the true types of the contents are available.

I wonder if this text remains true.
I mean, the types are there, but is the representation relatively
readable without a specialized printer?

Tom
  
Simon Marchi Oct. 7, 2024, 4:56 p.m. UTC | #2
On 2024-10-04 16:38, Tom Tromey wrote:
>>>>>> "Simon" == Simon Marchi <simon.marchi@efficios.com> writes:
> 
> Simon> This patch slightly changes the htab_t pretty-printer test, which was
> Simon> relying on all_bfds.  Note that with the new hash table, gdb-specific
> Simon> printers aren't needed; the libstdc++ printers suffice -- in fact,
> Simon> they are better, because the true types of the contents are available.
> 
> I wonder if this text remains true.
> I mean, the types are there, but is the representation relatively
> readable without a specialized printer?

It's similar to your version.  With your version (v1):

    (top-gdb) p all_bfds
    $1 = {
      m_entries = 13,
      m_data = std::__debug::vector of length 31, capacity 31 = {0x0, 0x0, 0x0, 0x5120000cacc0, 0x0, 0x5120000d0240, 0x5120000cb8c0, 0x0, 0x0, 0x0, 0x5120000c7b40, 0x5120000cbd40, 
        0x5120000cff40, 0x0, 0x0, 0x0, 0x0, 0x5120000d03c0, 0x5120000cba40, 0x0, 0x5120000cb2c0, 0x0, 0x0, 0x5120000cbec0, 0x5120000d00c0, 0x5120000cb740, 0x0, 0x0, 0x0, 0x0, 
        0x5120000cbbc0}
    }
    (top-gdb) p all_bfds.m_data
    $2 = std::__debug::vector of length 31, capacity 31 = {0x0, 0x0, 0x0, 0x5120000cacc0, 0x0, 0x5120000d0240, 0x5120000cb8c0, 0x0, 0x0, 0x0, 0x5120000c7b40, 0x5120000cbd40, 
      0x5120000cff40, 0x0, 0x0, 0x0, 0x0, 0x5120000d03c0, 0x5120000cba40, 0x0, 0x5120000cb2c0, 0x0, 0x0, 0x5120000cbec0, 0x5120000d00c0, 0x5120000cb740, 0x0, 0x0, 0x0, 0x0, 
      0x5120000cbbc0}

And with this version:

    (top-gdb) p all_bfds
    $1 = {
      <ankerl::unordered_dense::v4_4_0::detail::base_table_type_set> = {<No data fields>}, 
      members of ankerl::unordered_dense::v4_4_0::detail::table<bfd*, void, ankerl::unordered_dense::v4_4_0::hash<bfd*, void>, std::equal_to<bfd*>, std::allocator<bfd*>, ankerl::unordered_dense::v4_4_0::bucket_type::standard, false>:
      m_values = std::__debug::vector of length 13, capacity 16 = {0x5120000d3240, 0x5120000d5ac0, 0x5120000d6cc0, 0x5120000d78c0, 0x5120000d7a40, 0x5120000d7bc0, 0x5120000d7d40, 
        0x5120000d7ec0, 0x5120000d8040, 0x5120000d81c0, 0x5120000d8340, 0x5120000d84c0, 0x5120000d8640},
      m_buckets = 0x51100005d600,
      m_num_buckets = 32,
      m_max_bucket_capacity = 25,
      m_max_load_factor = 0.800000012,
      m_hash = {<No data fields>},
      m_equal = {
        <std::binary_function<bfd*, bfd*, bool>> = {<No data fields>}, <No data fields>},
      m_shifts = 59 ';'
    }
    (top-gdb) p all_bfds.m_values
    $2 = std::__debug::vector of length 13, capacity 16 = {0x5120000d3240, 0x5120000d5ac0, 0x5120000d6cc0, 0x5120000d78c0, 0x5120000d7a40, 0x5120000d7bc0, 0x5120000d7d40, 
      0x5120000d7ec0, 0x5120000d8040, 0x5120000d81c0, 0x5120000d8340, 0x5120000d84c0, 0x5120000d8640}
    (top-gdb) 

Simon
  

Patch

diff --git a/gdb/gdb_bfd.c b/gdb/gdb_bfd.c
index 0854d571ecf9..e3d095834a9f 100644
--- a/gdb/gdb_bfd.c
+++ b/gdb/gdb_bfd.c
@@ -34,6 +34,7 @@ 
 #include "inferior.h"
 #include "cli/cli-style.h"
 #include <unordered_map>
+#include "gdbsupport/unordered_set.h"
 
 #if CXX_STD_THREAD
 
@@ -80,12 +81,12 @@  struct gdb_bfd_section_data
   void *map_addr;
 };
 
-/* A hash table holding every BFD that gdb knows about.  This is not
+/* A hash set holding every BFD that gdb knows about.  This is not
    to be confused with 'gdb_bfd_cache', which is used for sharing
    BFDs; in contrast, this hash is used just to implement
    "maint info bfd".  */
 
-static htab_t all_bfds;
+static gdb::unordered_set<bfd *> all_bfds;
 
 /* An object of this type is stored in each BFD's user data.  */
 
@@ -482,7 +483,6 @@  static void
 gdb_bfd_init_data (struct bfd *abfd, struct stat *st)
 {
   struct gdb_bfd_data *gdata;
-  void **slot;
 
   gdb_assert (bfd_usrdata (abfd) == nullptr);
 
@@ -493,9 +493,8 @@  gdb_bfd_init_data (struct bfd *abfd, struct stat *st)
   bfd_set_usrdata (abfd, gdata);
 
   /* This is the first we've seen it, so add it to the hash table.  */
-  slot = htab_find_slot (all_bfds, abfd, INSERT);
-  gdb_assert (slot && !*slot);
-  *slot = abfd;
+  bool inserted = all_bfds.emplace (abfd).second;
+  gdb_assert (inserted);
 }
 
 /* See gdb_bfd.h.  */
@@ -734,7 +733,7 @@  gdb_bfd_unref (struct bfd *abfd)
   delete gdata;
   bfd_set_usrdata (abfd, NULL);  /* Paranoia.  */
 
-  htab_remove_elt (all_bfds, abfd);
+  all_bfds.erase (abfd);
 
   gdb_bfd_close_or_warn (abfd);
 
@@ -1145,25 +1144,6 @@  gdb_bfd_errmsg (bfd_error_type error_tag, char **matching)
   return ret;
 }
 
-/* A callback for htab_traverse that prints a single BFD.  */
-
-static int
-print_one_bfd (void **slot, void *data)
-{
-  bfd *abfd = (struct bfd *) *slot;
-  struct gdb_bfd_data *gdata = (struct gdb_bfd_data *) bfd_usrdata (abfd);
-  struct ui_out *uiout = (struct ui_out *) data;
-
-  ui_out_emit_tuple tuple_emitter (uiout, NULL);
-  uiout->field_signed ("refcount", gdata->refc);
-  uiout->field_string ("addr", host_address_to_string (abfd));
-  uiout->field_string ("filename", bfd_get_filename (abfd),
-		       file_name_style.style ());
-  uiout->text ("\n");
-
-  return 1;
-}
-
 /* Implement the 'maint info bfd' command.  */
 
 static void
@@ -1177,7 +1157,17 @@  maintenance_info_bfds (const char *arg, int from_tty)
   uiout->table_header (40, ui_left, "filename", "Filename");
 
   uiout->table_body ();
-  htab_traverse_noresize (all_bfds, print_one_bfd, uiout);
+
+  for (auto abfd : all_bfds)
+    {
+      auto gdata = static_cast<gdb_bfd_data *> (bfd_usrdata (abfd));
+      ui_out_emit_tuple tuple_emitter (uiout, nullptr);
+      uiout->field_signed ("refcount", gdata->refc);
+      uiout->field_string ("addr", host_address_to_string (abfd));
+      uiout->field_string ("filename", bfd_get_filename (abfd),
+			   file_name_style.style ());
+      uiout->text ("\n");
+    }
 }
 
 /* BFD related per-inferior data.  */
@@ -1272,9 +1262,6 @@  void _initialize_gdb_bfd ();
 void
 _initialize_gdb_bfd ()
 {
-  all_bfds = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
-				NULL, xcalloc, xfree);
-
   add_cmd ("bfds", class_maintenance, maintenance_info_bfds, _("\
 List the BFDs that are currently open."),
 	   &maintenanceinfolist);
diff --git a/gdb/testsuite/gdb.gdb/python-helper.exp b/gdb/testsuite/gdb.gdb/python-helper.exp
index 4589edc388fd..99524e075094 100644
--- a/gdb/testsuite/gdb.gdb/python-helper.exp
+++ b/gdb/testsuite/gdb.gdb/python-helper.exp
@@ -261,7 +261,8 @@  proc test_python_helper {} {
     }
 
     # Test the htab_t pretty-printer.
-    gdb_test -prompt $outer_prompt_re "print all_bfds" "htab_t with ${::decimal} elements = \\{${::hex}.*\\}"
+    gdb_test -prompt $outer_prompt_re "print varobj_table" \
+	"htab_t with ${::decimal} elements"
 
     # Test the intrusive_list pretty-printer.  A bug occured in the
     # pretty-printer for lists with more than one element.  Verify that