From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id OZtKKEnhS2r6BioAWB0awg (envelope-from ) for ; Mon, 06 Jul 2026 13:09:29 -0400 Authentication-Results: simark.ca; dkim=pass (1024-bit key; unprotected) header.d=amd.com header.i=@amd.com header.a=rsa-sha256 header.s=selector1 header.b=dBsHhIBs; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id 8F4781E024; Mon, 06 Jul 2026 13:09:29 -0400 (EDT) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-6.4 required=5.0 tests=ARC_SIGNED,ARC_VALID,BAYES_00, DKIMWL_WL_HIGH,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 5FEB41E024 for ; Mon, 06 Jul 2026 13:09:28 -0400 (EDT) Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id BD4484BA2E2E for ; Mon, 6 Jul 2026 17:09:26 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org BD4484BA2E2E Authentication-Results: sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=amd.com header.i=@amd.com header.a=rsa-sha256 header.s=selector1 header.b=dBsHhIBs Received: from SJ2PR03CU001.outbound.protection.outlook.com (mail-westusazon11012020.outbound.protection.outlook.com [52.101.43.20]) by sourceware.org (Postfix) with ESMTPS id AC7FB4BA2E21 for ; Mon, 6 Jul 2026 17:09:00 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org AC7FB4BA2E21 Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com Authentication-Results: sourceware.org; spf=fail smtp.mailfrom=amd.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org AC7FB4BA2E21 Authentication-Results: sourceware.org; arc=pass smtp.remote-ip=52.101.43.20 ARC-Seal: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1783357740; cv=pass; b=VNO4xRKkYkc4UN2yjH4mG/hTGnPjvQBYIraRs2bJyNGqrz3wOu/f7V8IpVOIuVLPh4Ed+8EQ1TwmKKtqgxak8STSPz+3vlujmm6Lcf6M+aPDPIuK6UYc45AYZpv38KxpkQBzl942gJbqQVGf9D0lU8U5nrwTk/MVjDeMs740yM8= ARC-Message-Signature: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1783357740; c=relaxed/simple; bh=UdOrPs474NPcQqiMsJoM08pCsYAj6tqp+xY1KeEFwKk=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=w3KAc5NolMsioFfTFQLtENCaaoK4IGXgZcRbCwFA1sGIftA96iAuQ1Jl8lHDzwH+I9AYq9xuhWn5WqfapBSMj0kFmAtzF/lh916z7JJOHhQhDZ/aXxDgjD2yu4zdSKCwPPlTnQ9sdPLQnknDJ+UK8C+0Nnznsghd7fRSs4HHlgQ= ARC-Authentication-Results: i=2; sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=amd.com header.i=@amd.com header.a=rsa-sha256 header.s=selector1 header.b=dBsHhIBs DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org AC7FB4BA2E21 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=lVzazXJ5Po4eoEqIYvrilbqEGQaBUcvLFHwQxUxiOniHG/BLYgc7WECLt4wFV+87X5Etw/t0p0dRxCF1SJOCTPn7owPpR3vVDr8eetWmD1IJX7gTTtLT5z/GJA4ToXZz8SY9GPg8fGYtn3UuI/5jsmNPvUWxcqaGYK2ytnJyoplZYnS0jBxTfo1rG/r8VpmVXidCZCoxxcOxh5evrXNNw54XBNmixbGSxb2/Cy48n4GQ0MXk5UB4h9mHPQcp048eyL45UtWbWqziL8l3Vlve9D8ZSDLrvF1mPFI8G0njebbYQaQ/CsFKUZUrp8GNbhY7svRSIHQlN0d7hGEGNTWudQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=IyrmcB8zlPSBtoz1Ku3rr+rsr+FKLdbrjexA+RJ6Up4=; b=yftmTCtoikQJB+WkgR2flQEorGA0qKuyUTXk0lolgpoI6Dpw4AH4onB0Ho3wVOItyeBOXN03c/V6OhTSdAwtipPXYs6fm18TaANmRLeYKsSjjH4KKRmV+pZ7OhVRALHXxhZJWKQwzneXS8MdDt4GtcHFGA//f3Xblz9grSZihl0d7Xk10jsWLWOJdQRoaFq1KftQD9B+K5z6RHZdmwaiWCWF+BAIVHUMv0dTjYIfGf4Nd/w+GMSDfnGke2xGoSYPChFHIMbobP7vUJdVrdq2EsQo/1A/zbK/SkBDisAMdExSjv8/1JLQQfKOcsNw30fNKsOOEknojV8crLB4ab/89g== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=efficios.com smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=IyrmcB8zlPSBtoz1Ku3rr+rsr+FKLdbrjexA+RJ6Up4=; b=dBsHhIBsLAiH0Qk9trMKy7C8bRcHS9Xg7bzlUw5/m2RnfYRZfrRdmXYStLwvDcWKJX3MMxT3yk4JRJSwFqz1CJRa6lLebv6srTYHGwO6uFf/17vHpkDFycpllqas3AnViHC6MUYc1I+MzynsxoID42t5JaH27jzZYY/CmYJlEX8= Received: from BN9PR03CA0475.namprd03.prod.outlook.com (2603:10b6:408:139::30) by SJ2PR12MB8831.namprd12.prod.outlook.com (2603:10b6:a03:4d0::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.181.8; Mon, 6 Jul 2026 17:08:50 +0000 Received: from BN2PEPF000044AA.namprd04.prod.outlook.com (2603:10b6:408:139:cafe::61) by BN9PR03CA0475.outlook.office365.com (2603:10b6:408:139::30) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.21.181.8 via Frontend Transport; Mon, 6 Jul 2026 17:08:49 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=satlexmb08.amd.com; pr=C Received: from satlexmb08.amd.com (165.204.84.17) by BN2PEPF000044AA.mail.protection.outlook.com (10.167.243.105) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.181.6 via Frontend Transport; Mon, 6 Jul 2026 17:08:49 +0000 Received: from khazad-dum (10.180.168.240) by satlexmb08.amd.com (10.181.42.217) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.41; Mon, 6 Jul 2026 12:08:47 -0500 Date: Mon, 6 Jul 2026 18:08:38 +0100 From: Lancelot SIX To: Simon Marchi , Subject: Re: [PATCH v2 3/10] gdb/solib-rocm: add cached_target_fd to manage cached fd lifetime Message-ID: References: <20260608200100.666134-4-simon.marchi@efficios.com> MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Disposition: inline In-Reply-To: <20260608200100.666134-4-simon.marchi@efficios.com> X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: satlexmb08.amd.com (10.181.42.217) To satlexmb08.amd.com (10.181.42.217) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BN2PEPF000044AA:EE_|SJ2PR12MB8831:EE_ X-MS-Office365-Filtering-Correlation-Id: 84808416-beb8-48d9-3dfe-08dedb813f57 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; ARA:13230040|376014|23010399003|82310400026|36860700016|1800799024|56012099006|11063799006|18002099003|22082099003|3023799007; X-Microsoft-Antispam-Message-Info: 9em185odPN5eQHBT4KruDaBc05SBqhoXyRlEk2YQt+mSk5SaTpFbXN8JnLnidq+q7xV1P74pn6GiioWRt69sU05gJeg0yGgWrPcc11jNv5R+yeQ9oXatR1ftqoCKnrMvF0UyjDUWnMjIOwdm+jXrBwKQy207cUdzzcP7UppaeJWrzbxRDYEm+JRcIjZ5giX9cXKYBnQEzlgYrLQlPUd+7vDsFcWHgsVhzx07B2s2BcWwl715WGYxKlMScM10Z0UbPqbTqSIDBu50YiO5YOYfUpdXi27EES68wIIKGt6y2Wfcbo3hhAGq4D4TkUf1tGt5aJyiwMOFG6h25bXRw2Y1rYHw5BsxM+0acVsfom878DtvXYe8YKjI8ziwrq1x+sB92VW5j5V+ioyPJeTuaGUBReJO11Q6n5BKTnE7Md7u8+ST21HJqCvZqTiHbk4qp+0rxSua8tP3kL3u0AGuhtjJeZbxKvdxyOuD5DFLQytA5NrCJuTVKSSJceJOf1JiX2cewC4YnHXfqXYLbrt/57suU4YRGNwuNF73BGEW4jGWofaVkv3rauF7cJZ2LAOA3IUrYlFkVrOqPlH5LTTnwcVah5BWK1s15mPFj9xlMMgOWBC0EaDsczpHCs7a2tv8i14wkgDpf9SJfSlBAbrIM/taUQHWc4E2a806tEWnEAQCfJFKLXkUq8vH2SHvaJl4BF0UWxtradPU33yD4UgVflKHxQ== X-Forefront-Antispam-Report: CIP:165.204.84.17; CTRY:US; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:satlexmb08.amd.com; PTR:InfoDomainNonexistent; CAT:NONE; SFS:(13230040)(376014)(23010399003)(82310400026)(36860700016)(1800799024)(56012099006)(11063799006)(18002099003)(22082099003)(3023799007); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: gslFI144TIHjgGg01cZzTlP9CPba2INQ6caO3+SsFExhz9oRnChQgwuAQGMEyAnfZzcXVd4kjjUKl/HHJ9mCNiunZ5I7MaIoztd9Xd/uS5m+lfNipRMDSFtJ8JqDtcUteeY3dWTU4v+IeFREb8GP4GPOF53r0H8UidQcP8gPH11WHGHuyN/kdw5K2Hb7YJg0pRYT0IyE4gwUtSj92ShYJICHs7GYuY+ULz/avrPU6DkAj9Rx9ze5sJnhFUm07HRk0dNhZYnyFynsPrjUrENytr3vNA1KLOsvcx27vnAtwPne9WHnE3I+2vle8fOHtwO5SnZf7HMcu+4QtIt41lb245ZZqB/Rw46GE4B/QfCSBIj1C+4veGNYVF7RP7PlfLhH6fF+UlK8D+pp2qu7ubM670SKthHd9FmMfM3qSLIXNpW/jdZCCS9oD+xNmDu1d/Di X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 06 Jul 2026 17:08:49.2713 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 84808416-beb8-48d9-3dfe-08dedb813f57 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d; Ip=[165.204.84.17]; Helo=[satlexmb08.amd.com] X-MS-Exchange-CrossTenant-AuthSource: BN2PEPF000044AA.namprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ2PR12MB8831 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 On Mon, Jun 08, 2026 at 04:00:27PM -0400, Simon Marchi wrote: > 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. Hi Simon, This overall looks good. Just have one minor nits below. Other than that, Approved-by: Lancelot Six (amdgpu) > > 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: Indentation seems wrong here. > + 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 };; You have 2 ";" here. One is too much. > } > 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