From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id EfGcEWh6OGksTikAWB0awg (envelope-from ) for ; Tue, 09 Dec 2025 14:37:12 -0500 Received: by simark.ca (Postfix, from userid 112) id 4679F1E0BC; Tue, 09 Dec 2025 14:37:12 -0500 (EST) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-2.3 required=5.0 tests=ARC_SIGNED,ARC_VALID,BAYES_00, MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED,RCVD_IN_VALIDITY_RPBL_BLOCKED, RCVD_IN_VALIDITY_SAFE_BLOCKED autolearn=ham autolearn_force=no version=4.0.1 Received: from vm01.sourceware.org (vm01.sourceware.org [38.145.34.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 5060F1E0AB for ; Tue, 09 Dec 2025 14:37:11 -0500 (EST) Received: from vm01.sourceware.org (localhost [127.0.0.1]) by sourceware.org (Postfix) with ESMTP id E1D9E4BA2E22 for ; Tue, 9 Dec 2025 19:37:10 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E1D9E4BA2E22 Received: from simark.ca (simark.ca [158.69.221.121]) by sourceware.org (Postfix) with ESMTPS id 15BCA4BA2E04 for ; Tue, 9 Dec 2025 19:36:16 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 15BCA4BA2E04 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 15BCA4BA2E04 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=158.69.221.121 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1765308976; cv=none; b=j8/7QDvqBIItZiE/JYVHxvcLsFYYjpSDnHmiyVAwTnZY9dGl+kYlJ2JNMde5Z+5yTbx+c4uDj3phuhwGeWROpJuiLZGLBZQkwPsnhhnMGBkYoyUD6PH/0CxgQd4S8AeDDPfxpnVNZOVR5Od9Oqcl6bpdubIoi3azEE4Pw0fd0a0= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1765308976; c=relaxed/simple; bh=MEzlPE8ylEVtif/lFckAU+SEvDFfB2aWNfDtAmkl6Bg=; h=From:To:Subject:Date:Message-ID:MIME-Version; b=V6XbaVp1S/WwzA4Wtf5Rsqi/B2qMZdC5m4d46ULr7zXtwOdumR0SPHhubtVJQdORUbCPzJNixeCFK2+ySlFpVMcf39sMg/V2mfkZVeVqTI+nS8FQaCHG8AQ5G8LM26/fN0rnOPBUy3AS1Ml+0l7kIDTGTkvSGHLAwfGpNNXSS0c= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 15BCA4BA2E04 Received: by simark.ca (Postfix) id 7744B1E0C2; Tue, 09 Dec 2025 14:36:15 -0500 (EST) From: Simon Marchi To: gdb-patches@sourceware.org Cc: Simon Marchi Subject: [PATCH 04/11] gdb/solib-rocm: add cached_fd to manage cached fd lifetime Date: Tue, 9 Dec 2025 14:32:08 -0500 Message-ID: <20251209193610.296085-5-simon.marchi@efficios.com> X-Mailer: git-send-email 2.52.0 In-Reply-To: <20251209193610.296085-1-simon.marchi@efficios.com> References: <20251209193610.296085-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_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 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 (buf) + nbytes, + = target_fileio_pread (m_fd.fd, static_cast (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