From: Simon Marchi <simon.marchi@efficios.com>
To: gdb-patches@sourceware.org
Cc: Simon Marchi <simon.marchi@efficios.com>
Subject: [PATCH 04/11] gdb/solib-rocm: add cached_fd to manage cached fd lifetime
Date: Tue, 9 Dec 2025 14:32:08 -0500 [thread overview]
Message-ID: <20251209193610.296085-5-simon.marchi@efficios.com> (raw)
In-Reply-To: <20251209193610.296085-1-simon.marchi@efficios.com>
Make the management of the cached fds automatic.
Change rocm_solib_fd_cache::open to return a cached_fd, an RAII type
that calls rocm_solib_fd_cache::close on destruction. cached_fd holds
the actual fd and a reference to the cache it comes from.
Change rocm_code_object_stream_file to hold a cached_fd and remove the
explicit destructor.
This is not essential, I just thought it would be nice.
Change-Id: I2529ac1caebbe6b7e6e60d228a403064f6f38d06
---
gdb/solib-rocm.c | 91 ++++++++++++++++++++++++++++++------------------
1 file changed, 58 insertions(+), 33 deletions(-)
diff --git a/gdb/solib-rocm.c b/gdb/solib-rocm.c
index bde34bafd8b1..5560a6035184 100644
--- a/gdb/solib-rocm.c
+++ b/gdb/solib-rocm.c
@@ -39,22 +39,58 @@ struct rocm_solib_fd_cache
explicit rocm_solib_fd_cache (inferior *inf) : m_inferior (inf) {}
DISABLE_COPY_AND_ASSIGN (rocm_solib_fd_cache);
+ /* The open method returns an object of this type.
+
+ On destruction, automatically call close to indicate the caller no longer
+ uses the fd. */
+ struct cached_fd
+ {
+ cached_fd (int fd, rocm_solib_fd_cache &cache)
+ : fd (fd),
+ cache (cache)
+ {
+ }
+
+ cached_fd (cached_fd &&other)
+ : fd (other.fd),
+ cache (other.cache)
+ {
+ other.fd = -1;
+ }
+
+ ~cached_fd ()
+ {
+ if (fd == -1)
+ return;
+
+ fileio_error target_errno;
+ if (cache.close (fd, &target_errno) != 0)
+ warning (_("Failed to close solib: %s"),
+ strerror (fileio_error_to_host (target_errno)));
+ }
+
+ DISABLE_COPY_AND_ASSIGN (cached_fd);
+
+ int fd;
+ rocm_solib_fd_cache &cache;
+ };
+
/* Return a read-only file descriptor to FILENAME and increment the
associated reference count.
Open the file FILENAME if it is not already opened, reuse the existing file
descriptor otherwise.
- On error -1 is returned, and TARGET_ERRNO is set. */
- int open (const std::string &filename, fileio_error *target_errno);
+ On error, return a cached_fd with .fd == -1 and set *TARGET_ERRNO. */
+ cached_fd open (const std::string &filename, fileio_error *target_errno);
+private:
/* Decrement the reference count to FD and close FD if the reference count
reaches 0.
On success, return 0. On error, return -1 and set TARGET_ERRNO. */
int close (int fd, fileio_error *target_errno);
-private:
struct refcnt_fd
{
refcnt_fd (int fd, int refcnt) : fd (fd), refcnt (refcnt) {}
@@ -72,7 +108,7 @@ struct rocm_solib_fd_cache
gdb::unordered_map<std::string, refcnt_fd> m_cache;
};
-int
+rocm_solib_fd_cache::cached_fd
rocm_solib_fd_cache::open (const std::string &filename,
fileio_error *target_errno)
{
@@ -83,11 +119,14 @@ rocm_solib_fd_cache::open (const std::string &filename,
int fd
= target_fileio_open (m_inferior, filename.c_str (), FILEIO_O_RDONLY,
false, 0, target_errno);
- if (fd != -1)
- m_cache.emplace (std::piecewise_construct,
- std::forward_as_tuple (filename),
- std::forward_as_tuple (fd, 1));
- return fd;
+ if (fd == -1)
+ return cached_fd { -1, *this };
+
+ m_cache.emplace (std::piecewise_construct,
+ std::forward_as_tuple (filename),
+ std::forward_as_tuple (fd, 1));
+
+ return cached_fd { fd, *this };
}
else
{
@@ -95,7 +134,7 @@ rocm_solib_fd_cache::open (const std::string &filename,
already opened FD. */
it->second.refcnt++;
gdb_assert (it->second.fd != -1);
- return it->second.fd;
+ return cached_fd { it->second.fd, *this };
}
}
@@ -360,7 +399,7 @@ struct rocm_code_object_stream_file final : rocm_code_object_stream
{
DISABLE_COPY_AND_ASSIGN (rocm_code_object_stream_file);
- rocm_code_object_stream_file (rocm_solib_fd_cache &fd_cache, int fd,
+ rocm_code_object_stream_file (rocm_solib_fd_cache::cached_fd fd,
ULONGEST offset, ULONGEST size);
file_ptr read (bfd *abfd, void *buf, file_ptr size,
@@ -368,14 +407,9 @@ struct rocm_code_object_stream_file final : rocm_code_object_stream
LONGEST size () override;
- ~rocm_code_object_stream_file () override;
-
protected:
- /* The fd cache owning this code object stream. */
- rocm_solib_fd_cache &m_fd_cache;
-
/* The target file descriptor for this stream. */
- int m_fd;
+ rocm_solib_fd_cache::cached_fd m_fd;
/* The offset of the ELF file image in the target file. */
ULONGEST m_offset;
@@ -386,9 +420,8 @@ struct rocm_code_object_stream_file final : rocm_code_object_stream
};
rocm_code_object_stream_file::rocm_code_object_stream_file
- (rocm_solib_fd_cache &fd_cache, int fd, ULONGEST offset,
- ULONGEST size)
- : m_fd_cache (fd_cache), m_fd (fd), m_offset (offset), m_size (size)
+ (rocm_solib_fd_cache::cached_fd fd, ULONGEST offset, ULONGEST size)
+ : m_fd (std::move (fd)), m_offset (offset), m_size (size)
{
}
@@ -403,7 +436,7 @@ rocm_code_object_stream_file::read (bfd *, void *buf, file_ptr size,
QUIT;
file_ptr bytes_read
- = target_fileio_pread (m_fd, static_cast<gdb_byte *> (buf) + nbytes,
+ = target_fileio_pread (m_fd.fd, static_cast<gdb_byte *> (buf) + nbytes,
size, m_offset + offset + nbytes,
&target_errno);
@@ -431,7 +464,7 @@ rocm_code_object_stream_file::size ()
{
fileio_error target_errno;
struct stat stat;
- if (target_fileio_fstat (m_fd, &stat, &target_errno) < 0)
+ if (target_fileio_fstat (m_fd.fd, &stat, &target_errno) < 0)
{
errno = fileio_error_to_host (target_errno);
bfd_set_error (bfd_error_system_call);
@@ -451,14 +484,6 @@ rocm_code_object_stream_file::size ()
return m_size;
}
-rocm_code_object_stream_file::~rocm_code_object_stream_file ()
-{
- fileio_error target_errno;
- if (m_fd_cache.close (m_fd, &target_errno) != 0)
- warning (_("Failed to close solib: %s"),
- strerror (fileio_error_to_host (target_errno)));
-}
-
/* Interface to a code object which lives in the inferior's memory. */
struct rocm_code_object_stream_memory final : public rocm_code_object_stream
@@ -598,16 +623,16 @@ rocm_bfd_iovec_open (bfd *abfd, inferior *inferior)
{
auto info = get_solib_info (inferior);
fileio_error target_errno;
- int fd = info->fd_cache.open (decoded_path, &target_errno);
+ auto fd = info->fd_cache.open (decoded_path, &target_errno);
- if (fd == -1)
+ if (fd.fd == -1)
{
errno = fileio_error_to_host (target_errno);
bfd_set_error (bfd_error_system_call);
return nullptr;
}
- return new rocm_code_object_stream_file (info->fd_cache, fd, offset,
+ return new rocm_code_object_stream_file (std::move (fd), offset,
size);
}
--
2.52.0
next prev parent reply other threads:[~2025-12-09 19:37 UTC|newest]
Thread overview: 35+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-12-09 19:32 [PATCH 00/11] Multiple solib_ops in a program_space Simon Marchi
2025-12-09 19:32 ` [PATCH 01/11] gdb/solib-rocm: assert that host ops isn't rocm_solib_ops Simon Marchi
2026-05-07 17:56 ` [PATCH 1/11] " Lancelot SIX
2026-05-08 1:37 ` Simon Marchi
2026-05-08 10:36 ` Lancelot SIX
2026-06-08 18:36 ` Simon Marchi
2025-12-09 19:32 ` [PATCH 02/11] gdb/solib: use early return in solib_read_symbols Simon Marchi
2026-04-28 16:16 ` Tom Tromey
2026-05-08 1:44 ` Simon Marchi
2025-12-09 19:32 ` [PATCH 03/11] gdb/solib-rocm: pass reference to cache to rocm_code_object_stream_file Simon Marchi
2026-05-07 18:20 ` [PATCH 3/11] " Lancelot SIX
2026-05-08 1:48 ` Simon Marchi
2025-12-09 19:32 ` Simon Marchi [this message]
2026-05-07 20:17 ` [PATCH 4/11] gdb/solib-rocm: add cached_fd to manage cached fd lifetime Lancelot SIX
2026-05-08 1:52 ` Simon Marchi
2025-12-09 19:32 ` [PATCH 05/11] gdb: de-constify some methods of solib_ops Simon Marchi
2025-12-09 19:32 ` [PATCH 06/11] gdb/solib-rocm: move per-inferior data to rocm_solib_ops Simon Marchi
2026-04-28 16:19 ` Tom Tromey
2026-05-08 2:02 ` Simon Marchi
2025-12-09 19:32 ` [PATCH 07/11] gdb/solib-rocm: save inferior in rocm_solib_ops Simon Marchi
[not found] ` <87a4unm59w.fsf@tromey.com>
2026-05-08 2:10 ` Simon Marchi
2025-12-09 19:32 ` [PATCH 08/11] gdb/solib: add remove_solib function Simon Marchi
2026-04-28 16:23 ` Tom Tromey
2025-12-09 19:32 ` [PATCH 09/11] gdb: add objfile -> solib backlink Simon Marchi
2026-04-28 16:28 ` Tom Tromey
2026-05-08 2:25 ` Simon Marchi
2025-12-09 19:32 ` [PATCH 10/11] gdb: change default objfile iteration order to start with current objfile Simon Marchi
2026-04-28 16:42 ` Tom Tromey
2026-05-08 2:40 ` Simon Marchi
2025-12-09 19:32 ` [PATCH 11/11] gdb: multiple solib_ops per program space Simon Marchi
2026-04-28 17:27 ` Tom Tromey
2026-05-08 2:52 ` Simon Marchi
2026-04-28 16:00 ` [PATCH 00/11] Multiple solib_ops in a program_space Simon Marchi
2026-04-28 17:28 ` Tom Tromey
2026-05-08 2:40 ` Simon Marchi
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=20251209193610.296085-5-simon.marchi@efficios.com \
--to=simon.marchi@efficios.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