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 v2 5/7] Convert minidebug to new type-safe gdb_bfd_openr_iovec
Date: Mon, 18 Sep 2023 08:52:50 -0600	[thread overview]
Message-ID: <20230918-gdb-bfd-vec-v2-5-162c0e9a2bc9@adacore.com> (raw)
In-Reply-To: <20230918-gdb-bfd-vec-v2-0-162c0e9a2bc9@adacore.com>

This converts the minidebug BFD iovec implementation to the new
type-safe gdb_bfd_openr_iovec.
---
 gdb/minidebug.c | 98 +++++++++++++++++++++++----------------------------------
 1 file changed, 40 insertions(+), 58 deletions(-)

diff --git a/gdb/minidebug.c b/gdb/minidebug.c
index 979e569e639..e2b60fb959c 100644
--- a/gdb/minidebug.c
+++ b/gdb/minidebug.c
@@ -57,7 +57,7 @@ static lzma_allocator gdb_lzma_allocator = { alloc_lzma, free_lzma, NULL };
    a section.  This keeps only the last decompressed block in memory
    to allow larger data without using to much memory.  */
 
-struct gdb_lzma_stream
+struct gdb_lzma_stream : public gdb_bfd_iovec_base
 {
   /* Section of input BFD from which we are decoding data.  */
   asection *section = nullptr;
@@ -69,19 +69,25 @@ struct gdb_lzma_stream
   bfd_size_type data_start = 0;
   bfd_size_type data_end = 0;
   gdb::byte_vector data;
-};
 
-/* bfd_openr_iovec OPEN_P implementation for
-   find_separate_debug_file_in_section.  OPEN_CLOSURE is 'asection *'
-   of the section to decompress.
 
-   Return 'struct gdb_lzma_stream *' must be freed by caller by delete,
-   together with its INDEX lzma data.  */
+  ~gdb_lzma_stream ()
+  {
+    lzma_index_end (index, &gdb_lzma_allocator);
+  }
 
-static void *
-lzma_open (struct bfd *nbfd, void *open_closure)
+  file_ptr read (bfd *abfd, void *buffer, file_ptr nbytes,
+		 file_ptr offset) override;
+
+  int stat (struct bfd *abfd, struct stat *sb) override;
+};
+
+/* bfd_openr_iovec implementation helper for
+   find_separate_debug_file_in_section.  */
+
+static gdb_lzma_stream *
+lzma_open (struct bfd *nbfd, asection *section)
 {
-  asection *section = (asection *) open_closure;
   bfd_size_type size, offset;
   lzma_stream_flags options;
   gdb_byte footer[LZMA_STREAM_HEADER_SIZE];
@@ -127,15 +133,13 @@ lzma_open (struct bfd *nbfd, void *open_closure)
   return lstream;
 }
 
-/* bfd_openr_iovec PREAD_P implementation for
-   find_separate_debug_file_in_section.  Passed STREAM
-   is 'struct gdb_lzma_stream *'.  */
+/* bfd_openr_iovec read implementation for
+   find_separate_debug_file_in_section.  */
 
-static file_ptr
-lzma_pread (struct bfd *nbfd, void *stream, void *buf, file_ptr nbytes,
-	    file_ptr offset)
+file_ptr
+gdb_lzma_stream::read (struct bfd *nbfd, void *buf, file_ptr nbytes,
+		       file_ptr offset)
 {
-  struct gdb_lzma_stream *lstream = (struct gdb_lzma_stream *) stream;
   bfd_size_type chunk_size;
   lzma_index_iter iter;
   file_ptr block_offset;
@@ -147,12 +151,9 @@ lzma_pread (struct bfd *nbfd, void *stream, void *buf, file_ptr nbytes,
   res = 0;
   while (nbytes > 0)
     {
-      if (lstream->data.empty ()
-	  || lstream->data_start > offset || offset >= lstream->data_end)
+      if (data.empty () || data_start > offset || offset >= data_end)
 	{
-	  asection *section = lstream->section;
-
-	  lzma_index_iter_init (&iter, lstream->index);
+	  lzma_index_iter_init (&iter, index);
 	  if (lzma_index_iter_locate (&iter, offset))
 	    break;
 
@@ -184,15 +185,14 @@ lzma_pread (struct bfd *nbfd, void *stream, void *buf, file_ptr nbytes,
 	      != LZMA_OK)
 	    break;
 
-	  lstream->data = std::move (uncompressed);
-	  lstream->data_start = iter.block.uncompressed_file_offset;
-	  lstream->data_end = (iter.block.uncompressed_file_offset
-			       + iter.block.uncompressed_size);
+	  data = std::move (uncompressed);
+	  data_start = iter.block.uncompressed_file_offset;
+	  data_end = (iter.block.uncompressed_file_offset
+		      + iter.block.uncompressed_size);
 	}
 
-      chunk_size = std::min (nbytes, (file_ptr) lstream->data_end - offset);
-      memcpy (buf, lstream->data.data () + offset - lstream->data_start,
-	      chunk_size);
+      chunk_size = std::min (nbytes, (file_ptr) data_end - offset);
+      memcpy (buf, data.data () + offset - data_start, chunk_size);
       buf = (gdb_byte *) buf + chunk_size;
       offset += chunk_size;
       nbytes -= chunk_size;
@@ -202,36 +202,14 @@ lzma_pread (struct bfd *nbfd, void *stream, void *buf, file_ptr nbytes,
   return res;
 }
 
-/* bfd_openr_iovec CLOSE_P implementation for
-   find_separate_debug_file_in_section.  Passed STREAM
-   is 'struct gdb_lzma_stream *'.  */
-
-static int
-lzma_close (struct bfd *nbfd,
-	    void *stream)
-{
-  struct gdb_lzma_stream *lstream = (struct gdb_lzma_stream *) stream;
-
-  lzma_index_end (lstream->index, &gdb_lzma_allocator);
-  delete lstream;
-
-  /* Zero means success.  */
-  return 0;
-}
-
-/* bfd_openr_iovec STAT_P implementation for
-   find_separate_debug_file_in_section.  Passed STREAM
-   is 'struct gdb_lzma_stream *'.  */
+/* bfd_openr_iovec stat implementation for
+   find_separate_debug_file_in_section.  */
 
-static int
-lzma_stat (struct bfd *abfd,
-	   void *stream,
-	   struct stat *sb)
+int
+gdb_lzma_stream::stat (struct bfd *abfd, struct stat *sb)
 {
-  struct gdb_lzma_stream *lstream = (struct gdb_lzma_stream *) stream;
-
   memset (sb, 0, sizeof (struct stat));
-  sb->st_size = lzma_index_uncompressed_size (lstream->index);
+  sb->st_size = lzma_index_uncompressed_size (index);
   return 0;
 }
 
@@ -265,8 +243,12 @@ find_separate_debug_file_in_section (struct objfile *objfile)
   std::string filename = string_printf (_(".gnu_debugdata for %s"),
 					objfile_name (objfile));
 
-  abfd = gdb_bfd_openr_iovec (filename.c_str (), gnutarget, lzma_open,
-			      section, lzma_pread, lzma_close, lzma_stat);
+  auto open = [&] (bfd *nbfd) -> gdb_lzma_stream *
+  {
+    return lzma_open (nbfd, section);
+  };
+
+  abfd = gdb_bfd_openr_iovec (filename.c_str (), gnutarget, open);
   if (abfd == NULL)
     return NULL;
 

-- 
2.40.1


  parent reply	other threads:[~2023-09-18 14:52 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-18 14:52 [PATCH v2 0/7] Rewrite gdb_bfd_openr_iovec to be type-safe Tom Tromey
2023-09-18 14:52 ` [PATCH v2 1/7] Introduce type-safe variant of gdb_bfd_openr_iovec Tom Tromey
2023-09-18 14:52 ` [PATCH v2 2/7] Small constructor change to target_buffer Tom Tromey
2023-09-18 14:52 ` [PATCH v2 3/7] Convert mem_bfd_iovec to new type-safe gdb_bfd_openr_iovec Tom Tromey
2023-09-18 14:52 ` [PATCH v2 4/7] Convert target fileio " Tom Tromey
2023-09-18 14:52 ` Tom Tromey [this message]
2023-09-18 14:52 ` [PATCH v2 6/7] Convert solib-rocm " Tom Tromey
2023-09-28  9:57   ` Lancelot SIX
2023-09-18 14:52 ` [PATCH v2 7/7] Remove old gdb_bfd_openr_iovec Tom Tromey
2023-09-28 10:02 ` [PATCH v2 0/7] Rewrite gdb_bfd_openr_iovec to be type-safe Lancelot SIX
2023-09-28 16:45   ` Tom Tromey

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=20230918-gdb-bfd-vec-v2-5-162c0e9a2bc9@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).