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 1/7] Introduce type-safe variant of gdb_bfd_openr_iovec
Date: Thu, 24 Aug 2023 11:12:18 -0600	[thread overview]
Message-ID: <20230824-gdb-bfd-vec-v1-1-850e4e907ed1@adacore.com> (raw)
In-Reply-To: <20230824-gdb-bfd-vec-v1-0-850e4e907ed1@adacore.com>

This patch adds a new, type-safe variant of gdb_bfd_openr_iovec.  In
this approach, the underlying user data is simply an object, the
callbacks are methods, and the "open" function is a function view.
Nothing uses this new code yet.
---
 gdb/gdb_bfd.c | 42 ++++++++++++++++++++++++++++++++++++++++++
 gdb/gdb_bfd.h | 31 +++++++++++++++++++++++++++++++
 2 files changed, 73 insertions(+)

diff --git a/gdb/gdb_bfd.c b/gdb/gdb_bfd.c
index 3765561cbe9..0bb97460898 100644
--- a/gdb/gdb_bfd.c
+++ b/gdb/gdb_bfd.c
@@ -907,6 +907,48 @@ gdb_bfd_openw (const char *filename, const char *target)
 
 /* See gdb_bfd.h.  */
 
+gdb_bfd_ref_ptr
+gdb_bfd_openr_iovec (const char *filename, const char *target,
+		     gdb_iovec_opener_ftype open_fn)
+{
+  auto do_open = [] (bfd *nbfd, void *closure) -> void *
+  {
+    auto real_opener = (gdb_iovec_opener_ftype *) closure;
+    return (*real_opener) (nbfd);
+  };
+
+  auto read_trampoline = [] (bfd *nbfd, void *stream, void *buf,
+			     file_ptr nbytes, file_ptr offset) -> file_ptr
+  {
+    gdb_bfd_iovec_base *obj = (gdb_bfd_iovec_base *) stream;
+    return obj->read (nbfd, buf, nbytes, offset);
+  };
+
+  auto stat_trampoline = [] (struct bfd *abfd, void *stream,
+			     struct stat *sb) -> int
+  {
+    gdb_bfd_iovec_base *obj = (gdb_bfd_iovec_base *) stream;
+    return obj->stat (abfd, sb);
+  };
+
+  auto close_trampoline = [] (struct bfd *nbfd, void *stream) -> int
+  {
+    gdb_bfd_iovec_base *obj = (gdb_bfd_iovec_base *) stream;
+    delete obj;
+    /* Success.  */
+    return 0;
+  };
+
+  bfd *result = bfd_openr_iovec (filename, target,
+				 do_open, &open_fn,
+				 read_trampoline, close_trampoline,
+				 stat_trampoline);
+
+  return gdb_bfd_ref_ptr::new_reference (result);
+}
+
+/* See gdb_bfd.h.  */
+
 gdb_bfd_ref_ptr
 gdb_bfd_openr_iovec (const char *filename, const char *target,
 		     void *(*open_func) (struct bfd *nbfd,
diff --git a/gdb/gdb_bfd.h b/gdb/gdb_bfd.h
index d15b1106d9a..ae374f5d7ae 100644
--- a/gdb/gdb_bfd.h
+++ b/gdb/gdb_bfd.h
@@ -22,6 +22,7 @@
 
 #include "registry.h"
 #include "gdbsupport/byte-vector.h"
+#include "gdbsupport/function-view.h"
 #include "gdbsupport/gdb_ref_ptr.h"
 #include "gdbsupport/iterator-range.h"
 #include "gdbsupport/next-iterator.h"
@@ -150,6 +151,36 @@ gdb_bfd_ref_ptr gdb_bfd_openr (const char *, const char *);
 
 gdb_bfd_ref_ptr gdb_bfd_openw (const char *, const char *);
 
+/* The base class for BFD "iovec" implementations.  This is used by
+   gdb_bfd_openr_iovec and enables better type safety.  */
+
+class gdb_bfd_iovec_base
+{
+protected:
+
+  gdb_bfd_iovec_base () = default;
+
+public:
+
+  virtual ~gdb_bfd_iovec_base () = default;
+
+  /* The "read" callback.  */
+  virtual file_ptr read (bfd *abfd, void *buffer, file_ptr nbytes,
+			 file_ptr offset) = 0;
+
+  /* The "stat" callback.  */
+  virtual int stat (struct bfd *abfd, struct stat *sb) = 0;
+};
+
+/* The type of the function used to open a new iovec-based BFD.  */
+using gdb_iovec_opener_ftype
+     = gdb::function_view<gdb_bfd_iovec_base * (bfd *)>;
+
+/* A type-safe wrapper for bfd_openr_iovec.  */
+
+gdb_bfd_ref_ptr gdb_bfd_openr_iovec (const char *filename, const char *target,
+				     gdb_iovec_opener_ftype open_fn);
+
 /* A wrapper for bfd_openr_iovec that initializes the gdb-specific
    reference count.  */
 

-- 
2.40.1


  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 ` Tom Tromey [this message]
2023-09-18 10:29   ` [PATCH 1/7] Introduce type-safe variant of gdb_bfd_openr_iovec 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 ` [PATCH 3/7] Convert mem_bfd_iovec to new type-safe gdb_bfd_openr_iovec Tom Tromey
2023-08-24 17:12 ` [PATCH 4/7] Convert target fileio " 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-1-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).