From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id hwoGFqAfJ2oVnDwAWB0awg (envelope-from ) for ; Mon, 08 Jun 2026 16:01:36 -0400 Received: by simark.ca (Postfix, from userid 112) id E7A691E0AA; Mon, 08 Jun 2026 16:01:35 -0400 (EDT) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-5.3 required=5.0 tests=ARC_SIGNED,ARC_VALID,BAYES_00, MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED autolearn=ham autolearn_force=no version=4.0.1 Received: from vm01.sourceware.org (vm01.sourceware.org [IPv6:2620:52:6:3111::32]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id 923361E0A3 for ; Mon, 08 Jun 2026 16:01:33 -0400 (EDT) Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 370CD44EBD74 for ; Mon, 8 Jun 2026 20:01:25 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 370CD44EBD74 Received: from simark.ca (simark.ca [158.69.221.121]) by sourceware.org (Postfix) with ESMTPS id 3404F51A4327 for ; Mon, 8 Jun 2026 20:01:02 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 3404F51A4327 Authentication-Results: sourceware.org; dmarc=fail (p=none dis=none) header.from=efficios.com Authentication-Results: sourceware.org; spf=fail smtp.mailfrom=efficios.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 3404F51A4327 Authentication-Results: sourceware.org; arc=none smtp.remote-ip=158.69.221.121 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1780948862; cv=none; b=un8mgEdIXOmx9xlHW1oLx/9itNdoXND/9bq41q8sWst3AOS0WyRl+A3CSWnHAR5z0WDegGSJLheXrGmxMSQxRCVxmxFPmA6I4BKTSPYuVT0RZ3DAGfS1qo/miZGlq9pbEp4eszpnWcyHSOYny62iXVvyWgAP2n3lWrfkFK7OIqM= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1780948862; c=relaxed/simple; bh=qE5DybsII1N1vE5YwhragoqsYf1mQ+zPVKIXejJ4NdY=; h=From:To:Subject:Date:Message-ID:MIME-Version; b=UGaEMryQDeUADG9IX9HJ4FquE71Pmh3GVNHqXKLZrUhdA1J15aZbCEJDlvIUYOaDzeSz3c36hfDOpXnThqpCAwFHakuoSycOj9nC4c/pNrzjRzXJqzrXWEXnZGJ/TMbNs0PE/lH0xrv/PZg58/7brRmM2wssIbioWi2cfbpr9W0= ARC-Authentication-Results: i=1; sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3404F51A4327 Received: by simark.ca (Postfix) id AB8E21E0A6; Mon, 08 Jun 2026 16:01:01 -0400 (EDT) From: Simon Marchi To: gdb-patches@sourceware.org Cc: Simon Marchi Subject: [PATCH RESEND v2 03/10] gdb/solib-rocm: add cached_target_fd to manage cached fd lifetime Date: Mon, 8 Jun 2026 16:00:27 -0400 Message-ID: <20260608200100.666134-4-simon.marchi@efficios.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20260608200100.666134-1-simon.marchi@efficios.com> References: <20260608200100.666134-1-simon.marchi@efficios.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces~public-inbox=simark.ca@sourceware.org Make the management of the cached fds automatic. Change rocm_solib_fd_cache::open to return a cached_target_fd, an RAII type that calls rocm_solib_fd_cache::close on destruction. cached_target_fd holds the actual fd and a reference to the cache it comes from. Change rocm_code_object_stream_file to hold a cached_target_fd and remove the explicit destructor. This is not essential, I just thought it would be nice. Change-Id: I2529ac1caebbe6b7e6e60d228a403064f6f38d06 --- gdb/solib-rocm.c | 90 +++++++++++++++++++++++++++++++----------------- 1 file changed, 59 insertions(+), 31 deletions(-) diff --git a/gdb/solib-rocm.c b/gdb/solib-rocm.c index f96f0823200e..60e64c494f88 100644 --- a/gdb/solib-rocm.c +++ b/gdb/solib-rocm.c @@ -40,22 +40,63 @@ 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_target_fd + { + cached_target_fd (target_fd fd, rocm_solib_fd_cache &cache) + : m_fd (fd), + m_cache (cache) + { + } + + cached_target_fd (cached_target_fd &&other) + : m_fd (other.m_fd), + m_cache (other.m_cache) + { + other.m_fd = target_fd::INVALID; + } + + ~cached_target_fd () + { + if (m_fd == target_fd::INVALID) + return; + + fileio_error target_errno; + if (m_cache.close (m_fd, &target_errno) != 0) + warning (_("Failed to close solib: %s"), + strerror (fileio_error_to_host (target_errno))); + } + + DISABLE_COPY_AND_ASSIGN (cached_target_fd); + + target_fd fd () const { return m_fd; } + +private: + target_fd m_fd; + rocm_solib_fd_cache &m_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 target_fd::INVALID is returned, and TARGET_ERRNO is set. */ - target_fd open (const std::string &filename, fileio_error *target_errno); + On error, return a cached_target_fd with target_fd::INVALID and set + *TARGET_ERRNO. */ + cached_target_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 (target_fd fd, fileio_error *target_errno); -private: struct refcnt_fd { refcnt_fd (target_fd fd, int refcnt) : fd (fd), refcnt (refcnt) {} @@ -73,7 +114,7 @@ struct rocm_solib_fd_cache gdb::unordered_string_map m_cache; }; -target_fd +rocm_solib_fd_cache::cached_target_fd rocm_solib_fd_cache::open (const std::string &filename, fileio_error *target_errno) { @@ -88,7 +129,7 @@ rocm_solib_fd_cache::open (const std::string &filename, m_cache.emplace (std::piecewise_construct, std::forward_as_tuple (filename), std::forward_as_tuple (fd, 1)); - return fd; + return cached_target_fd { fd, *this };; } else { @@ -96,7 +137,7 @@ rocm_solib_fd_cache::open (const std::string &filename, already opened FD. */ it->second.refcnt++; gdb_assert (it->second.fd != target_fd::INVALID); - return it->second.fd; + return cached_target_fd { it->second.fd, *this }; } } @@ -356,7 +397,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, target_fd fd, + rocm_code_object_stream_file (rocm_solib_fd_cache::cached_target_fd fd, ULONGEST offset, ULONGEST size); file_ptr read (bfd *abfd, void *buf, file_ptr size, @@ -364,14 +405,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. */ - target_fd m_fd; + rocm_solib_fd_cache::cached_target_fd m_fd; /* The offset of the ELF file image in the target file. */ ULONGEST m_offset; @@ -382,9 +418,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, target_fd fd, ULONGEST offset, - ULONGEST size) - : m_fd_cache (fd_cache), m_fd (fd), m_offset (offset), m_size (size) + (rocm_solib_fd_cache::cached_target_fd fd, ULONGEST offset, ULONGEST size) + : m_fd (std::move (fd)), m_offset (offset), m_size (size) { } @@ -399,9 +434,9 @@ rocm_code_object_stream_file::read (bfd *, void *buf, file_ptr size, QUIT; file_ptr bytes_read - = target_fileio_pread (m_fd, static_cast (buf) + nbytes, - size, m_offset + offset + nbytes, - &target_errno); + = target_fileio_pread (m_fd.fd (), + static_cast (buf) + nbytes, size, + m_offset + offset + nbytes, &target_errno); if (bytes_read == 0) break; @@ -427,7 +462,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); @@ -447,14 +482,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 @@ -610,16 +637,17 @@ rocm_bfd_iovec_open (bfd *abfd, inferior *inferior) auto info = get_solib_info (inferior); fileio_error target_errno; - target_fd fd = info->fd_cache.open (decoded_path, &target_errno); + rocm_solib_fd_cache::cached_target_fd fd + = info->fd_cache.open (decoded_path, &target_errno); - if (fd == target_fd::INVALID) + if (fd.fd () == target_fd::INVALID) { 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.54.0