From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id wRRPIp/GTmpG0iwAWB0awg (envelope-from ) for ; Wed, 08 Jul 2026 17:52:31 -0400 Authentication-Results: simark.ca; dkim=pass (2048-bit key; unprotected) header.d=polymtl.ca header.i=@polymtl.ca header.a=rsa-sha256 header.s=oct2025 header.b=mcN89O5z; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id 896891E098; Wed, 08 Jul 2026 17:52:31 -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.4 required=5.0 tests=ARC_SIGNED,ARC_VALID,BAYES_00, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,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 D6E861E04F for ; Wed, 08 Jul 2026 17:52:30 -0400 (EDT) Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id DB1ED4BA2E3E for ; Wed, 8 Jul 2026 21:52:28 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org DB1ED4BA2E3E Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=polymtl.ca header.i=@polymtl.ca header.a=rsa-sha256 header.s=oct2025 header.b=mcN89O5z Received: from smtp.polymtl.ca (smtp.polymtl.ca [132.207.4.11]) by sourceware.org (Postfix) with ESMTPS id 85D5D4BA2E14 for ; Wed, 8 Jul 2026 21:51:59 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 85D5D4BA2E14 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=polymtl.ca Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=polymtl.ca ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 85D5D4BA2E14 Authentication-Results: sourceware.org; arc=none smtp.remote-ip=132.207.4.11 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1783547519; cv=none; b=GSA2mu/qHFwiAJ1AMODROxU8ChjW+jklED0QwPeVceYshV2g06qv00XEHoIKtjBIAynxfq0T2NWmaLZABEmCwLaL1uxcsfgBf3UeO83K82XUsPEQJPnY/CdtRPLatOJRYI5LaibADtPd9LjME8HHE40zkKAD9YLfI9RQJ4SPrXc= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1783547519; c=relaxed/simple; bh=YEFjNjlyp1HLJPZnQESDoLoYN7RzI3WlzEuF7HAn31s=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=eix1BU+LiVrtl5Wz/4QMU25wDKD2QenRugLRgCuhoKipcr3y03RQdGVDZiRu+PJUt0Y0xFDcKNPeVAQw1Z2gFRyqB5fd/8c/MSwtDOvTTc9j5OacONeZ7/zftmx1ArxfEfR7TVMFN9PRQutQlbnEBSc3iFbUWYvEUOSRJuHsCpg= ARC-Authentication-Results: i=1; sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=polymtl.ca header.i=@polymtl.ca header.a=rsa-sha256 header.s=oct2025 header.b=mcN89O5z DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 85D5D4BA2E14 Received: from simark.ca (simark.ca [158.69.221.121]) (authenticated bits=0) by smtp.polymtl.ca (8.14.7/8.14.7) with ESMTP id 668LppXL117341 (version=TLSv1/SSLv3 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Wed, 8 Jul 2026 17:51:56 -0400 DKIM-Filter: OpenDKIM Filter v2.11.0 smtp.polymtl.ca 668LppXL117341 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=polymtl.ca; s=oct2025; t=1783547516; bh=vQ2v8LO+xUWX8MS3eg4qxfkYMiw6aBP4XRjhDE50+3k=; h=From:To:Cc:Subject:Date:In-Reply-To:From; b=mcN89O5z6u5VIq5iXdYruNJvK0oi9DUxzYavyMiGpCgTW9kHNmuPUR/AaPmtmt0j0 SpFijyhjJ7sMyaIfduNW95qZRAxovESqdbGF1U+OxbrvtliheeVxjdrHCDspKGvCPL hewJB5cFhN+vyFg2bi9EuYeADK9ZtoMNE9WIT6gvUo+q4yicM3htfNz+jO5v7KmIO9 aL8tLmq2OphJ/as1qm2QWWughQYWQIjxQRdRAxfrjcGDH0KahsFIHojad6/tF1fIek QEvHpsaoN0nhcutaHxLswK7qbVNEfG6y8sLM25zmiOkTcEUFNu2mLngXHTnKYAK7o0 EzhyglViIGUqA== Received: by simark.ca (Postfix) id 6222A1E0A6; Wed, 08 Jul 2026 17:51:51 -0400 (EDT) From: simon.marchi@polymtl.ca To: gdb-patches@sourceware.org Cc: Tom Tromey , Lancelot SIX , Simon Marchi , Lancelot Six Subject: [PATCH v3 03/10] gdb/solib-rocm: add cached_target_fd to manage cached fd lifetime Date: Wed, 8 Jul 2026 17:51:35 -0400 Message-ID: <20260708215145.93134-4-simon.marchi@polymtl.ca> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20260708215145.93134-1-simon.marchi@polymtl.ca> References: <20260708215145.93134-1-simon.marchi@polymtl.ca> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Poly-FromMTA: (simark.ca [158.69.221.121]) at Wed, 8 Jul 2026 21:51:51 +0000 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 From: Simon Marchi 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 Approved-by: Lancelot Six (amdgpu) --- 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 97eb93865610..a54e3a5a400b 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.55.0