From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id Wq6+LJnz/GlL2R8AWB0awg (envelope-from ) for ; Thu, 07 May 2026 16:18:33 -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=Q1uNui+p; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id A6ECA1E067; Thu, 07 May 2026 16:18:33 -0400 (EDT) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-3.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,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 66F8F1E067 for ; Thu, 07 May 2026 16:18:32 -0400 (EDT) Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id B2D6D4BA2E31 for ; Thu, 7 May 2026 20:18:31 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B2D6D4BA2E31 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=Q1uNui+p Received: from BN8PR05CU002.outbound.protection.outlook.com (mail-eastus2azlp170110003.outbound.protection.outlook.com [IPv6:2a01:111:f403:c110::3]) by sourceware.org (Postfix) with ESMTPS id BA03E4BA2E2F for ; Thu, 7 May 2026 20:17:52 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org BA03E4BA2E2F 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 BA03E4BA2E2F Authentication-Results: sourceware.org; arc=pass smtp.remote-ip=2a01:111:f403:c110::3 ARC-Seal: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1778185072; cv=pass; b=lbsjK7+iT2V56D5FaUkdYI0XPj4fQ76PdfaPt+HODunOvARuwW6PQlO4Zm/Id5o8xwmtiHakQrIoGRhcXsL2wYUptYbzz32YYOfI03Z7GaPhnMTPtS/kCPsBvPm2kRtRqMbs3r1mzK63EocqU7Q4UZ6HqTNMtEGddPI7KvhYcww= ARC-Message-Signature: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1778185072; c=relaxed/simple; bh=Kg81BU159MGRDp4gy6NlYSg+mCLx/4nvcdXtzPNAW2Q=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=TJ1+bSl/Nn5VWyffn7hEnWEfcwltdpg56UWRXakRY4/0q/5MfD2UGiiYHx02ys3CwmHMFu7FHGm4fq03j2bbg+GSNs1KYi/edTIxECbiPiUlktznggZxZm6fEzY1O1v3mg/7/loNBufkInN3tWsh12zastwxGPhCfW+jN6EoGNY= 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=Q1uNui+p DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org BA03E4BA2E2F ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=GUJXJ5Igdy12wc5OZktXIy90oyjt8Yp9/UDYhuIgrXteBONgf/KnHJNUAPrDBMKf7LSu8zLi5Gn2GvnAkZRNNbQKkY4MHh2npZi7D39Ox51FgkaDhmEPkW4gIEKw/cNRWesaiNwoZ8eEg8nBGtUOpczrBjJpkexjeuYjKgY7ZYexGYoxkHBHKmRUP7kn3K2LCUc2jZ/JhUlqXRdQiX8eR9gFqsLULjXaIBeouKk+rraLzKYqeFIRM/TAA5QWXtHyR0SjJZ3u8+DexXIbJ0gn2DJJx4TWhFFcYFFhcXt4EZ9w+M+VssexOzd4RjnFO5/PgjV9+Uqyav7NswWt/mVEOg== 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=vi5VOa9wZIwDLLcdofXugHAxnCSyRWckPKVyEPBoqwo=; b=TTFAd3yvbMJwh+2WcLZVrxSCP0lkI8pG77aDcqbhQ1D3E03Q0E5n/2S/owikFDu3LGd9gpvQ1l11PC1ewFs0wnQ1z/mIb5hHvJ3P0Sd355SmSoIkUxDsn2gabXKMXBX5l8z6MzVXIhGPq1QgN9L+LJzwYv8Q1jWmY6BQSB2dyurIgrYp1JQeXCu/JCBHu7q/2Aal11hv1V0lu0Ln5AnKJ2QKpWBIE5nMv4IL2hbZb4NiWvXvC0BSC6yenZHgxxXYoLutUMHi8ZZu6hYdoytvDZPVRgfMInbNOHEymxhGnmvVNJFoDM5XewZRIL0g4X5B8+fKxOWmlUgB5BmOEs0zHA== 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=vi5VOa9wZIwDLLcdofXugHAxnCSyRWckPKVyEPBoqwo=; b=Q1uNui+p8vDdV2JzLK5gEf7x+PzmGXQxSYivxs7hk5bLspxI2sSRZrd4BTH0UnMoPntijVJMLpenK2m6Q4cD1bDaG87Ip7yEAMLA2IwmaMR/Lb48o0TsszWRR8XI4RANtdT5UyokV+kEdWI74/tVHtjXrcEMw/sae92LjzuLkEs= Received: from BY1P220CA0012.NAMP220.PROD.OUTLOOK.COM (2603:10b6:a03:59d::8) by MN6PR12MB8565.namprd12.prod.outlook.com (2603:10b6:208:47d::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.9891.18; Thu, 7 May 2026 20:17:45 +0000 Received: from MWH0EPF000A6735.namprd04.prod.outlook.com (2603:10b6:a03:59d:cafe::d7) by BY1P220CA0012.outlook.office365.com (2603:10b6:a03:59d::8) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.20.9891.18 via Frontend Transport; Thu, 7 May 2026 20:17:44 +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=satlexmb07.amd.com; pr=C Received: from satlexmb07.amd.com (165.204.84.17) by MWH0EPF000A6735.mail.protection.outlook.com (10.167.249.27) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.9891.9 via Frontend Transport; Thu, 7 May 2026 20:17:44 +0000 Received: from khazad-dum (10.180.168.240) by satlexmb07.amd.com (10.181.42.216) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.17; Thu, 7 May 2026 15:17:42 -0500 Date: Thu, 7 May 2026 21:17:28 +0100 From: Lancelot SIX To: Simon Marchi CC: Subject: Re: [PATCH 4/11] gdb/solib-rocm: add cached_fd to manage cached fd lifetime Message-ID: References: <20251209193610.296085-5-simon.marchi@efficios.com> MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Disposition: inline In-Reply-To: <20251209193610.296085-5-simon.marchi@efficios.com> X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: satlexmb07.amd.com (10.181.42.216) To satlexmb07.amd.com (10.181.42.216) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: MWH0EPF000A6735:EE_|MN6PR12MB8565:EE_ X-MS-Office365-Filtering-Correlation-Id: 43e5f7e3-0228-416c-3bfc-08deac75b2d5 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; ARA:13230040|36860700016|1800799024|82310400026|376014|18002099003|22082099003|56012099003|3023799003; X-Microsoft-Antispam-Message-Info: Ub4X8pkMaalTnfWB5Vp2v8e3I3dTjIXPfKui8jMdjgVQJhvn6u4ZPcJSIjEZXsHDcvQAsvGVvvKk2BQcquSZR9Jk36LkeWVn1RjgE7vgHafZTJopJ4m1XvYBeCD7sbFGgngwNSeUBUvcDP2IDcNsBAh5MWmOtLSx1XOh1r1/TCuTen4aosra7cM9AW1NVmJvZol5iyboqTPSVRnA1AwPqDnT5kHsjUz4+vjaaHlJx6yR2VXUv6LFMSIhLQRqjAfeWwPdusGetmG1njNpIvG4Ty2vz63C7J9V27AoI9Lit2UHf6v7Mr4Nrd+NbCZE4lCHfKZkpCTu6lH4RA4uVVHrgs7ClPoPFTO3ftG15qO5QHbioqwm5AibyFe3nxV1jtuu/GoPvoo3wies/kOPueY278or1BvngcN+65aZ5meAqGCaqOC6aJowdWSidP6XOIcgalp4BgZDvEUq+9u4f8rArnJeXGSq/Ssgulj0dJdc2o+XyiFITOt0Elz4CNbCw9LmUMiR04As1BknQqk3F2nn8tgcSDEOxfL5vHZ9iX2lxSUOri6nqqSNvMVPfMMLFKgEizq6SZB6Dl1Pl88B1mYJqyYB9i9srkn5MO7vdgbKSEYfrIbi81vPjKd/SXQa7QT3gjWBrguvGdNE3kk0SpB0H+rrlTrwFeUKDrpt2u6ag5keWCNoJPy8h09NDI7yZF8J6KIBcpW4J3M7F7VOUqZhKArs6k5WuCkURE65AfkxLrs= X-Forefront-Antispam-Report: CIP:165.204.84.17; CTRY:US; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:satlexmb07.amd.com; PTR:InfoDomainNonexistent; CAT:NONE; SFS:(13230040)(36860700016)(1800799024)(82310400026)(376014)(18002099003)(22082099003)(56012099003)(3023799003); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: heLZO2503cHVZncjtBJZUcay1g2sTzLsMTcT9EOLv6uFbDx086hmGWLvBnpx9CCsD+HFIQPK+n///iBwyh4Kxl28SdWULUn8JoYx/dDtSd4PVuF9eVXMDwiiCPddS2AkdnVu7WbNeOlRjKHgvfcMXTPmQAIeLYKUgZeaKDc1vq45iAPdAfELC8QzAg4SrGWFXdsooYzB3rUqCoitZvaRcpJGe0gzJheNEAt+sysEx5+6Ci/FUGfbb+7VOHl3xtAXAuUnqO7SMa0BmcU1iaPtub000A6eLgmqWEwYAG4Lp53nI6WYDGhf1prbv4+Fv8V5J2HhQ3hBKjNn6Q+UG9ecwA3koFPHR619q/ehpKC5eb5X1Qi4YqycxW5C1oPUd9+OT+JyRfJ8gOW/Rz7323alD8d11dIfyC5MpvG7MJn6G9JxNvOgFCQBS0x2Dra8AT6V X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 07 May 2026 20:17:44.3268 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 43e5f7e3-0228-416c-3bfc-08deac75b2d5 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=[satlexmb07.amd.com] X-MS-Exchange-CrossTenant-AuthSource: MWH0EPF000A6735.namprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN6PR12MB8565 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 Tue, Dec 09, 2025 at 02:32:08PM -0500, Simon Marchi wrote: > 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; Given some changes since this patch was sent, this is now a target_fd. > + rocm_solib_fd_cache &cache; the cache field is never accessed outside of the class, there is no need to have it public. I expect quite a few conflicts when rebasing this patch, nevertheless the direction so far works for me. Best, Lancelot. > + }; > + > /* 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