public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Tom Tromey <tromey@adacore.com>
To: gdb-patches@sourceware.org
Subject: [PATCH 3/7] Convert mem_bfd_iovec to new type-safe gdb_bfd_openr_iovec
Date: Thu, 24 Aug 2023 11:12:20 -0600	[thread overview]
Message-ID: <20230824-gdb-bfd-vec-v1-3-850e4e907ed1@adacore.com> (raw)
In-Reply-To: <20230824-gdb-bfd-vec-v1-0-850e4e907ed1@adacore.com>

This converts the mem_bfd_iovec / target_buffer code to use the new
type-safe gdb_bfd_openr_iovec.
---
 gdb/gdb_bfd.c | 61 ++++++++++++++++++++---------------------------------------
 1 file changed, 20 insertions(+), 41 deletions(-)

diff --git a/gdb/gdb_bfd.c b/gdb/gdb_bfd.c
index 733d5e109bb..2f8bb33997b 100644
--- a/gdb/gdb_bfd.c
+++ b/gdb/gdb_bfd.c
@@ -220,7 +220,7 @@ gdb_bfd_has_target_filename (struct bfd *abfd)
 /* For `gdb_bfd_open_from_target_memory`.  An object that manages the
    details of a BFD in target memory.  */
 
-struct target_buffer
+struct target_buffer : public gdb_bfd_iovec_base
 {
   /* Constructor.  BASE and SIZE define where the BFD can be found in
      target memory.  */
@@ -245,6 +245,11 @@ struct target_buffer
   const char *filename () const
   { return m_filename.get (); }
 
+  file_ptr read (bfd *abfd, void *buffer, file_ptr nbytes,
+		 file_ptr offset) override;
+
+  int stat (struct bfd *abfd, struct stat *sb) override;
+
 private:
   /* The base address of the in-memory BFD file.  */
   CORE_ADDR m_base;
@@ -256,47 +261,23 @@ struct target_buffer
   gdb::unique_xmalloc_ptr<char> m_filename;
 };
 
-/* For `gdb_bfd_open_from_target_memory`.  Opening the file is a no-op.  */
-
-static void *
-mem_bfd_iovec_open (struct bfd *abfd, void *open_closure)
-{
-  return open_closure;
-}
-
-/* For `gdb_bfd_open_from_target_memory`.  Closing the file is just freeing the
-   base/size pair on our side.  */
-
-static int
-mem_bfd_iovec_close (struct bfd *abfd, void *stream)
-{
-  struct target_buffer *buffer = (target_buffer *) stream;
-  delete buffer;
-
-  /* Zero means success.  */
-  return 0;
-}
-
 /* For `gdb_bfd_open_from_target_memory`.  For reading the file, we just need to
    pass through to target_read_memory and fix up the arguments and return
    values.  */
 
-static file_ptr
-mem_bfd_iovec_pread (struct bfd *abfd, void *stream, void *buf,
+file_ptr
+target_buffer::read (struct bfd *abfd, void *buf,
 		     file_ptr nbytes, file_ptr offset)
 {
-  struct target_buffer *buffer = (struct target_buffer *) stream;
-
   /* If this read will read all of the file, limit it to just the rest.  */
-  if (offset + nbytes > buffer->size ())
-    nbytes = buffer->size () - offset;
+  if (offset + nbytes > size ())
+    nbytes = size () - offset;
 
   /* If there are no more bytes left, we've reached EOF.  */
   if (nbytes == 0)
     return 0;
 
-  int err
-    = target_read_memory (buffer->base () + offset, (gdb_byte *) buf, nbytes);
+  int err = target_read_memory (base () + offset, (gdb_byte *) buf, nbytes);
   if (err)
     return -1;
 
@@ -306,13 +287,11 @@ mem_bfd_iovec_pread (struct bfd *abfd, void *stream, void *buf,
 /* For `gdb_bfd_open_from_target_memory`.  For statting the file, we only
    support the st_size attribute.  */
 
-static int
-mem_bfd_iovec_stat (struct bfd *abfd, void *stream, struct stat *sb)
+int
+target_buffer::stat (struct bfd *abfd, struct stat *sb)
 {
-  struct target_buffer *buffer = (struct target_buffer*) stream;
-
   memset (sb, 0, sizeof (struct stat));
-  sb->st_size = buffer->size ();
+  sb->st_size = size ();
   return 0;
 }
 
@@ -322,14 +301,14 @@ gdb_bfd_ref_ptr
 gdb_bfd_open_from_target_memory (CORE_ADDR addr, ULONGEST size,
 				 const char *target)
 {
-  struct target_buffer *buffer = new target_buffer (addr, size);
+  std::unique_ptr<target_buffer> buffer
+    = gdb::make_unique<target_buffer> (addr, size);
 
   return gdb_bfd_openr_iovec (buffer->filename (), target,
-			      mem_bfd_iovec_open,
-			      buffer,
-			      mem_bfd_iovec_pread,
-			      mem_bfd_iovec_close,
-			      mem_bfd_iovec_stat);
+			      [&] (bfd *nbfd)
+			      {
+				return buffer.release ();
+			      });
 }
 
 /* bfd_openr_iovec OPEN_CLOSURE data for gdb_bfd_open.  */

-- 
2.40.1


  parent reply	other threads:[~2023-08-24 17:12 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-24 17:12 [PATCH 0/7] Rewrite gdb_bfd_openr_iovec to be type-safe Tom Tromey
2023-08-24 17:12 ` [PATCH 1/7] Introduce type-safe variant of gdb_bfd_openr_iovec Tom Tromey
2023-09-18 10:29   ` Lancelot SIX
2023-09-18 14:44     ` Tom Tromey
2023-08-24 17:12 ` [PATCH 2/7] Small constructor change to target_buffer Tom Tromey
2023-08-24 17:12 ` Tom Tromey [this message]
2023-08-24 17:12 ` [PATCH 4/7] Convert target fileio to new type-safe gdb_bfd_openr_iovec Tom Tromey
2023-08-24 17:12 ` [PATCH 5/7] Convert minidebug " Tom Tromey
2023-08-24 17:12 ` [PATCH 6/7] Convert solib-rocm " Tom Tromey
2023-09-18  9:41   ` Lancelot SIX
2023-09-18 14:49     ` Tom Tromey
2023-08-24 17:12 ` [PATCH 7/7] Remove old gdb_bfd_openr_iovec Tom Tromey
2023-09-18 12:34 ` [PATCH 0/7] Rewrite gdb_bfd_openr_iovec to be type-safe Lancelot SIX

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230824-gdb-bfd-vec-v1-3-850e4e907ed1@adacore.com \
    --to=tromey@adacore.com \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).