section_table_xfer_memory: Replace section name with callback predicate
authorKevin Buettner <kevinb@redhat.com>
Thu, 5 Mar 2020 00:42:41 +0000 (17:42 -0700)
committerKevin Buettner <kevinb@redhat.com>
Wed, 22 Jul 2020 19:36:42 +0000 (12:36 -0700)
This patch is motivated by the need to be able to select sections
that section_table_xfer_memory_partial should consider for memory
transfers.  I'll use this facility in the next patch in this series.

section_table_xfer_memory_partial() can currently be passed a section
name which may be used to make name-based selections.  This is similar
to what I want to do, except that I want to be able to consider
section flags instead of the name.

I'm replacing the section name parameter with a predicate that,
when passed a pointer to a target_section struct, will return
true if that section should be further considered, or false which
indicates that it shouldn't.

I've converted the one existing use where a non-NULL section
name is passed to section_table_xfer_memory_partial().   Instead
of passing the section name, it now looks like this:

  auto match_cb = [=] (const struct target_section *s)
    {
      return (strcmp (section_name, s->the_bfd_section->name) == 0);
    };

  return section_table_xfer_memory_partial (readbuf, writebuf,
    memaddr, len, xfered_len,
    table->sections,
    table->sections_end,
    match_cb);

The other callers all passed NULL; they've been simplified somewhat
in that they no longer need to pass NULL.

gdb/ChangeLog:

* exec.h (section_table_xfer_memory): Revise declaration,
replacing section name parameter with an optional callback
predicate.
* exec.c (section_table_xfer_memory): Likewise.
* bfd-target.c, exec.c, target.c, corelow.c: Adjust all callers
of section_table_xfer_memory.

gdb/ChangeLog
gdb/bfd-target.c
gdb/corelow.c
gdb/exec.c
gdb/exec.h
gdb/target.c

index 6fc88bdfb48bd881f647ef0bfd0d1a1c89470670..aaba31821a5b6860e8dfec9a3653ebdd2fea59fc 100644 (file)
@@ -1,3 +1,12 @@
+2020-07-22  Kevin Buettner  <kevinb@redhat.com>
+
+       * exec.h (section_table_xfer_memory): Revise declaration,
+       replacing section name parameter with an optional callback
+       predicate.
+       * exec.c (section_table_xfer_memory): Likewise.
+       * bfd-target.c, exec.c, target.c, corelow.c: Adjust all callers
+       of section_table_xfer_memory.
+
 2020-07-22  Tom Tromey  <tromey@adacore.com>
 
        * mi/mi-cmd-stack.c (list_args_or_locals): Use
index b75abd7fb0e62952865862503c48f528b95a1c08..3d266951c5aeb6852dc33ea2b48bbfb65856e177 100644 (file)
@@ -77,8 +77,7 @@ target_bfd::xfer_partial (target_object object,
        return section_table_xfer_memory_partial (readbuf, writebuf,
                                                  offset, len, xfered_len,
                                                  m_table.sections,
-                                                 m_table.sections_end,
-                                                 NULL);
+                                                 m_table.sections_end);
       }
     default:
       return TARGET_XFER_E_IO;
index 3958af1e67a68a5284df5d4c92085ce79dc19b6c..5697a02dc14c70bfae9b389a3f0b6f1fbdf45905 100644 (file)
@@ -618,8 +618,7 @@ core_target::xfer_partial (enum target_object object, const char *annex,
              (readbuf, writebuf,
               offset, len, xfered_len,
               m_core_section_table.sections,
-              m_core_section_table.sections_end,
-              NULL));
+              m_core_section_table.sections_end));
 
     case TARGET_OBJECT_AUXV:
       if (readbuf)
index 2ff5846c0e7b43c8ffe5bb3094565732f26346d5..e50f38899d6c7a608a8738383b2ca41d0a221462 100644 (file)
@@ -956,7 +956,8 @@ section_table_xfer_memory_partial (gdb_byte *readbuf, const gdb_byte *writebuf,
                                   ULONGEST *xfered_len,
                                   struct target_section *sections,
                                   struct target_section *sections_end,
-                                  const char *section_name)
+                                  gdb::function_view<bool
+                                    (const struct target_section *)> match_cb)
 {
   int res;
   struct target_section *p;
@@ -970,7 +971,7 @@ section_table_xfer_memory_partial (gdb_byte *readbuf, const gdb_byte *writebuf,
       struct bfd_section *asect = p->the_bfd_section;
       bfd *abfd = asect->owner;
 
-      if (section_name && strcmp (section_name, asect->name) != 0)
+      if (match_cb != nullptr && !match_cb (p))
        continue;               /* not the section we need.  */
       if (memaddr >= p->addr)
         {
@@ -1043,8 +1044,7 @@ exec_target::xfer_partial (enum target_object object,
     return section_table_xfer_memory_partial (readbuf, writebuf,
                                              offset, len, xfered_len,
                                              table->sections,
-                                             table->sections_end,
-                                             NULL);
+                                             table->sections_end);
   else
     return TARGET_XFER_E_IO;
 }
index 54e6ff4d9bab6c2c6f730b1faaf5a4062e20ce65..82eb39c55d830076e0c56bfb20e001c8bb5babf5 100644 (file)
@@ -65,8 +65,13 @@ extern enum target_xfer_status
    Request to transfer up to LEN 8-bit bytes of the target sections
    defined by SECTIONS and SECTIONS_END.  The OFFSET specifies the
    starting address.
-   If SECTION_NAME is not NULL, only access sections with that same
-   name.
+
+   The MATCH_CB predicate is optional; when provided it will be called
+   for each section under consideration.  When MATCH_CB evaluates as
+   true, the section remains under consideration; a false result
+   removes it from consideration for performing the memory transfers
+   noted above.  See memory_xfer_partial_1() in target.c for an
+   example.
 
    Return the number of bytes actually transfered, or zero when no
    data is available for the requested range.
@@ -83,7 +88,9 @@ extern enum target_xfer_status
                                     ULONGEST, ULONGEST, ULONGEST *,
                                     struct target_section *,
                                     struct target_section *,
-                                    const char *);
+                                    gdb::function_view<bool
+                                      (const struct target_section *)> match_cb
+                                        = nullptr);
 
 /* Read from mappable read-only sections of BFD executable files.
    Similar to exec_read_partial_read_only, but return
index cd66675e8a41f52da1e9ea759299e643930e214b..d03f0d5f384d02ebb6a007a444d9e3e92d3908a7 100644 (file)
@@ -980,11 +980,17 @@ memory_xfer_partial_1 (struct target_ops *ops, enum target_object object,
          const char *section_name = section->the_bfd_section->name;
 
          memaddr = overlay_mapped_address (memaddr, section);
+
+         auto match_cb = [=] (const struct target_section *s)
+           {
+             return (strcmp (section_name, s->the_bfd_section->name) == 0);
+           };
+
          return section_table_xfer_memory_partial (readbuf, writebuf,
                                                    memaddr, len, xfered_len,
                                                    table->sections,
                                                    table->sections_end,
-                                                   section_name);
+                                                   match_cb);
        }
     }
 
@@ -1002,8 +1008,7 @@ memory_xfer_partial_1 (struct target_ops *ops, enum target_object object,
          return section_table_xfer_memory_partial (readbuf, writebuf,
                                                    memaddr, len, xfered_len,
                                                    table->sections,
-                                                   table->sections_end,
-                                                   NULL);
+                                                   table->sections_end);
        }
     }