[binutils-gdb] gdb: remove pre-loop callback from gdbarch_read_core_file_mappings

Andrew Burgess via Gdb-cvs <[email protected]>
Newsgroups gmane.comp.gdb.cvs
Message-ID <[email protected]>
https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=1357605123d7cda93a0fa0364af04f387f509de3

commit 1357605123d7cda93a0fa0364af04f387f509de3
Author: Andrew Burgess <[email protected]>
Date:   Tue Mar 10 16:38:31 2026 +0000

    gdb: remove pre-loop callback from gdbarch_read_core_file_mappings
    
    Currently only one target, Linux, implements
    gdbarch_read_core_file_mappings, with
    linux_read_core_file_mappings. There is one use of
    gdbarch_read_core_file_mappings in corelow.c, and one direct use of
    linux_read_core_file_mappings in linux-tdep.c.
    
    The gdbarch_read_core_file_mappings takes two callbacks, a pre-loop
    callback, which is called once, then a loop callback which is called
    multiple times for each mapping that is discovered.
    
    The only user of the pre-loop callback is in linux-tdep.c.  Within
    corelow.c, the pre-loop callback is not used.
    
    In the next commit I plan to change linux_read_core_file_mappings, and
    as a result of this change the use of linux_read_core_file_mappings in
    linux-tdep.c will no longer be able to make use of the pre-loop
    callback.  This means that, after the next commit, there will be no
    users of the pre-loop callback.
    
    Additionally, the pre-loop callback takes an argument, the number of
    mappings found.
    
    After the next commit it is no longer clear what number we should pass
    here as the next commit will introduce the idea of there being two
    types of mapping, anonymous and non-anonymous.  Should the number
    passed to the pre-loop callback be the combined total?  Or should we
    count each separately?
    
    I could try to answer this question.
    
    Or I could just delete the pre-loop callback from
    gdbarch_read_core_file_mappings.
    
    This commit takes the second approach and deletes the callback.
    
    As part of this work I've updated linux_core_info_proc_mappings, which
    is the function that calls linux_read_core_file_mappings, so that the
    pre-loop callback is no longer used.  The lambda capture on the loop
    callback needed to change from [=] to [&] with this commit so
    `emitter` from the enclosing scope can be modified.
    
    There is one subtle change of behaviour in
    linux_core_info_proc_mappings after this commit.  Previously,
    linux_core_info_proc_mappings would print the table header so long as
    the core file had a valid NT_FILE note, even if that note contained no
    actual file mappings.
    
    With the removal of the pre-loop callback I had a choice, either
    always print the table header, or only print the table header if I saw
    some entries being printed.  I selected the second choice as that
    seemed like the smallest change, but there is a change here.  If a
    user has a core file with an NT_FILE note containing no mapped files,
    then the table header will no longer be printed.  Hopefully this isn't
    too disruptive.
    
    This is a refactoring commit in preparation for the next one.
    
    Reviewed-By: Keith Seitz <[email protected]>

Diff:
---
 gdb/arch-utils.c          |  1 -
 gdb/arch-utils.h          |  1 -
 gdb/corelow.c             | 10 ++--------
 gdb/gdbarch-gen.c         |  4 ++--
 gdb/gdbarch-gen.h         |  4 ++--
 gdb/gdbarch.h             |  5 +----
 gdb/gdbarch_components.py |  1 -
 gdb/linux-tdep.c          | 41 ++++++++++++++++-------------------------
 8 files changed, 23 insertions(+), 44 deletions(-)

diff --git a/gdb/arch-utils.c b/gdb/arch-utils.c
index ea0b3c111f9..5e15fa5b434 100644
--- a/gdb/arch-utils.c
+++ b/gdb/arch-utils.c
@@ -1080,7 +1080,6 @@ void
 default_read_core_file_mappings
   (struct gdbarch *gdbarch,
    struct bfd *cbfd,
-   read_core_file_mappings_pre_loop_ftype pre_loop_cb,
    read_core_file_mappings_loop_ftype loop_cb)
 {
 }
diff --git a/gdb/arch-utils.h b/gdb/arch-utils.h
index d5772575c7a..06902050043 100644
--- a/gdb/arch-utils.h
+++ b/gdb/arch-utils.h
@@ -378,7 +378,6 @@ extern std::string default_get_pc_address_flags (const frame_info_ptr &frame,
 extern void default_read_core_file_mappings
   (struct gdbarch *gdbarch,
    struct bfd *cbfd,
-   read_core_file_mappings_pre_loop_ftype pre_loop_cb,
    read_core_file_mappings_loop_ftype loop_cb);
 
 /* Default implementation of gdbarch_core_parse_exec_context.  Returns
diff --git a/gdb/corelow.c b/gdb/corelow.c
index e8e3e85e0c9..407515ee45a 100644
--- a/gdb/corelow.c
+++ b/gdb/corelow.c
@@ -2168,14 +2168,8 @@ gdb_read_core_file_mappings (struct gdbarch *gdbarch, struct bfd *cbfd)
   /* See linux_read_core_file_mappings() in linux-tdep.c for an example
      read_core_file_mappings method.  */
   gdbarch_read_core_file_mappings (gdbarch, cbfd,
-    /* After determining the number of mappings, read_core_file_mappings
-       will invoke this lambda.  */
-    [&] (ULONGEST)
-      {
-      },
-
-    /* read_core_file_mappings will invoke this lambda for each mapping
-       that it finds.  */
+    /* gdbarch_read_core_file_mappings will invoke this lambda for each
+       mapping that it finds.  */
     [&] (ULONGEST start, ULONGEST end, ULONGEST file_ofs,
 	 const char *filename, const bfd_build_id *build_id)
       {
diff --git a/gdb/gdbarch-gen.c b/gdb/gdbarch-gen.c
index 1a8f21091b2..f424fa2a86e 100644
--- a/gdb/gdbarch-gen.c
+++ b/gdb/gdbarch-gen.c
@@ -5196,13 +5196,13 @@ set_gdbarch_get_pc_address_flags (struct gdbarch *gdbarch,
 }
 
 void
-gdbarch_read_core_file_mappings (struct gdbarch *gdbarch, struct bfd *cbfd, read_core_file_mappings_pre_loop_ftype pre_loop_cb, read_core_file_mappings_loop_ftype loop_cb)
+gdbarch_read_core_file_mappings (struct gdbarch *gdbarch, struct bfd *cbfd, read_core_file_mappings_loop_ftype loop_cb)
 {
   gdb_assert (gdbarch != nullptr);
   gdb_assert (gdbarch->read_core_file_mappings != nullptr);
   if (gdbarch_debug >= 2)
     gdb_printf (gdb_stdlog, "gdbarch_read_core_file_mappings called\n");
-  gdbarch->read_core_file_mappings (gdbarch, cbfd, pre_loop_cb, loop_cb);
+  gdbarch->read_core_file_mappings (gdbarch, cbfd, loop_cb);
 }
 
 void
diff --git a/gdb/gdbarch-gen.h b/gdb/gdbarch-gen.h
index c1394978568..678b308fba5 100644
--- a/gdb/gdbarch-gen.h
+++ b/gdb/gdbarch-gen.h
@@ -1696,8 +1696,8 @@ void set_gdbarch_get_pc_address_flags (struct gdbarch *gdbarch, gdbarch_get_pc_a
 
 /* Read core file mappings */
 
-using gdbarch_read_core_file_mappings_ftype = void (struct gdbarch *gdbarch, struct bfd *cbfd, read_core_file_mappings_pre_loop_ftype pre_loop_cb, read_core_file_mappings_loop_ftype loop_cb);
-void gdbarch_read_core_file_mappings (struct gdbarch *gdbarch, struct bfd *cbfd, read_core_file_mappings_pre_loop_ftype pre_loop_cb, read_core_file_mappings_loop_ftype loop_cb);
+using gdbarch_read_core_file_mappings_ftype = void (struct gdbarch *gdbarch, struct bfd *cbfd, read_core_file_mappings_loop_ftype loop_cb);
+void gdbarch_read_core_file_mappings (struct gdbarch *gdbarch, struct bfd *cbfd, read_core_file_mappings_loop_ftype loop_cb);
 void set_gdbarch_read_core_file_mappings (struct gdbarch *gdbarch, gdbarch_read_core_file_mappings_ftype *read_core_file_mappings);
 
 /* Return true if the target description for all threads should be read from the
diff --git a/gdb/gdbarch.h b/gdb/gdbarch.h
index f5ee3074c38..7077f9262e9 100644
--- a/gdb/gdbarch.h
+++ b/gdb/gdbarch.h
@@ -122,10 +122,7 @@ enum class memtag_type
   allocation,
 };
 
-/* Callback types for 'read_core_file_mappings' gdbarch method.  */
-
-using read_core_file_mappings_pre_loop_ftype =
-  gdb::function_view<void (ULONGEST count)>;
+/* Callback type for 'read_core_file_mappings' gdbarch method.  */
 
 using read_core_file_mappings_loop_ftype =
   gdb::function_view<void (ULONGEST start,
diff --git a/gdb/gdbarch_components.py b/gdb/gdbarch_components.py
index 7a329d92166..b9304d3036d 100644
--- a/gdb/gdbarch_components.py
+++ b/gdb/gdbarch_components.py
@@ -2706,7 +2706,6 @@ Read core file mappings
     name="read_core_file_mappings",
     params=[
         ("struct bfd *", "cbfd"),
-        ("read_core_file_mappings_pre_loop_ftype", "pre_loop_cb"),
         ("read_core_file_mappings_loop_ftype", "loop_cb"),
     ],
     predefault="default_read_core_file_mappings",
diff --git a/gdb/linux-tdep.c b/gdb/linux-tdep.c
index 508f9f88457..43df05d1529 100644
--- a/gdb/linux-tdep.c
+++ b/gdb/linux-tdep.c
@@ -1109,11 +1109,6 @@ linux_info_proc (struct gdbarch *gdbarch, const char *args,
 
    CBFD is the BFD of the core file.
 
-   PRE_LOOP_CB is the callback function to invoke prior to starting
-   the loop which processes individual entries.  This callback will
-   only be executed after the note has been examined in enough
-   detail to verify that it's not malformed in some way.
-
    LOOP_CB is the callback function that will be executed once
    for each mapping.  */
 
@@ -1121,7 +1116,6 @@ static void
 linux_read_core_file_mappings
   (struct gdbarch *gdbarch,
    struct bfd *cbfd,
-   read_core_file_mappings_pre_loop_ftype pre_loop_cb,
    read_core_file_mappings_loop_ftype  loop_cb)
 {
   /* Ensure that ULONGEST is big enough for reading 64-bit core files.  */
@@ -1207,7 +1201,6 @@ linux_read_core_file_mappings
     }
 
   cbfd->build_id = orig_build_id;
-  pre_loop_cb (count);
 
   /* Vector to collect proc mappings.  */
   struct proc_mapping
@@ -1249,11 +1242,8 @@ linux_read_core_file_mappings
 	       });
 
   /* Call loop_cb with sorted proc mappings.  */
-  for (int i = 0; i < count; i++)
-    {
-      const auto &m = proc_mappings[i];
-      loop_cb (m.start, m.end, m.file_ofs, m.filename, m.build_id);
-    }
+  for (const auto &m : proc_mappings)
+    loop_cb (m.start, m.end, m.file_ofs, m.filename, m.build_id);
 }
 
 /* Implement "info proc mappings" for corefile CBFD.  */
@@ -1265,21 +1255,22 @@ linux_core_info_proc_mappings (struct gdbarch *gdbarch, struct bfd *cbfd,
   std::optional<ui_out_emit_table> emitter;
 
   linux_read_core_file_mappings (gdbarch, cbfd,
-    [&] (ULONGEST count)
-      {
-	gdb_printf (_("Mapped address spaces:\n\n"));
-	emitter.emplace (current_uiout, 5, -1, "ProcMappings");
-	int width = gdbarch_addr_bit (gdbarch) == 32 ? 10 : 18;
-	current_uiout->table_header (width, ui_left, "start", "Start Addr");
-	current_uiout->table_header (width, ui_left, "end", "End Addr");
-	current_uiout->table_header (width, ui_left, "size", "Size");
-	current_uiout->table_header (width, ui_left, "offset", "Offset");
-	current_uiout->table_header (0, ui_left, "objfile", "File");
-	current_uiout->table_body ();
-      },
-    [=] (ULONGEST start, ULONGEST end, ULONGEST file_ofs,
+    [&] (ULONGEST start, ULONGEST end, ULONGEST file_ofs,
 	 const char *filename, const bfd_build_id *build_id)
       {
+	if (!emitter.has_value ())
+	  {
+	    gdb_printf (_("Mapped address spaces:\n\n"));
+	    emitter.emplace (current_uiout, 5, -1, "ProcMappings");
+	    int width = gdbarch_addr_bit (gdbarch) == 32 ? 10 : 18;
+	    current_uiout->table_header (width, ui_left, "start", "Start Addr");
+	    current_uiout->table_header (width, ui_left, "end", "End Addr");
+	    current_uiout->table_header (width, ui_left, "size", "Size");
+	    current_uiout->table_header (width, ui_left, "offset", "Offset");
+	    current_uiout->table_header (0, ui_left, "objfile", "File");
+	    current_uiout->table_body ();
+	  }
+
 	ui_out_emit_tuple tuple_emitter (current_uiout, nullptr);
 	current_uiout->field_core_addr ("start", gdbarch, start);
 	current_uiout->field_core_addr ("end", gdbarch, end);
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.