From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id JZmNIl2M2GfcIhMAWB0awg (envelope-from ) for ; Mon, 17 Mar 2025 16:55:57 -0400 Authentication-Results: simark.ca; dkim=fail reason="signature verification failed" (768-bit key; unprotected) header.d=tromey.com header.i=@tromey.com header.a=rsa-sha256 header.s=default header.b=Isyb0Fyh; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id 7D6391E100; Mon, 17 Mar 2025 16:55:57 -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.1 required=5.0 tests=ARC_SIGNED,ARC_VALID,BAYES_00, DKIM_INVALID,DKIM_SIGNED,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED autolearn=ham autolearn_force=no version=4.0.1 Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (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 B5CFE1E0C0 for ; Mon, 17 Mar 2025 16:55:56 -0400 (EDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 9509C3858C48 for ; Mon, 17 Mar 2025 20:55:55 +0000 (GMT) Received: from omta036.useast.a.cloudfilter.net (omta036.useast.a.cloudfilter.net [44.202.169.35]) by sourceware.org (Postfix) with ESMTPS id 2E4313858D21 for ; Mon, 17 Mar 2025 20:55:41 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 2E4313858D21 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=tromey.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=tromey.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 2E4313858D21 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=44.202.169.35 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1742244941; cv=none; b=jeL9IPnXADrimKS+EgCytz7jr3hIt2PyO8BD4chrP5CLM+J7c6ewIXr1+u2zVPldLVtUuQ/CH6wSe3FOluF42SSa+TRN+c7H+dXcI2fEI8wOEcu9ELNpSGt51Sb2eTJvGBcmQcfmo3eCrTXugzUghDPqUXoYkmfUClR268czmhY= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1742244941; c=relaxed/simple; bh=jRJ+BxXNmNUFSep5QLl5vgNBRtmVcttfpCZXLapdkYY=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=HBpF9aMpMoINdVexE8O8/A0+UFukeFLzQoBgGvGlz8xknx16FRQehDzIxEb4o1y+qg5gZeyGsZVVpnqdBHmlwl2BKO8Wc3jRHkirAM+bGQZWF5XEGcbJGM+mnupiJ7tqjRoa2kzDl3eIUyFezo+kqz+0CXsVOa6UdJXhIciZ/ZE= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 2E4313858D21 Authentication-Results: sourceware.org; dkim=policy reason="signing key too small" (768-bit key, unprotected) header.d=tromey.com header.i=@tromey.com header.a=rsa-sha256 header.s=default header.b=Isyb0Fyh Received: from eig-obgw-6001a.ext.cloudfilter.net ([10.0.30.140]) by cmsmtp with ESMTPS id u2a6tNPllzZPauHUqt7Cup; Mon, 17 Mar 2025 20:55:40 +0000 Received: from box5379.bluehost.com ([162.241.216.53]) by cmsmtp with ESMTPS id uHUptSJHigFaXuHUqt9McA; Mon, 17 Mar 2025 20:55:40 +0000 X-Authority-Analysis: v=2.4 cv=DbzcqetW c=1 sm=1 tr=0 ts=67d88c4c a=ApxJNpeYhEAb1aAlGBBbmA==:117 a=ApxJNpeYhEAb1aAlGBBbmA==:17 a=Vs1iUdzkB0EA:10 a=ItBw4LHWJt0A:10 a=MJsaKEqOFJVj3QWIAgkA:9 a=6Ogn3jAGHLSNbaov7Orx:22 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=tromey.com; s=default; h=Content-Transfer-Encoding:MIME-Version:Message-ID:Date:Subject: Cc:To:From:Sender:Reply-To:Content-Type:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: In-Reply-To:References:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=29d7NVPmgvu7HM4RtO1p0W3h73yiP3Cz96RUd5nSTvw=; b=Isyb0FyhOJJVn6LEIY1rI4CGoz CKuAxCbj4bKXuRD8+FUmLWI2o/7n2uqaUioqnNuKIj59jxN/YNaY6VNOmKVvs5rx3dMECm5/yiSFa x/u3PUBResrAAMCtEkq/el1l8; Received: from 97-122-96-32.hlrn.qwest.net ([97.122.96.32]:45614 helo=localhost.localdomain) by box5379.bluehost.com with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.1) (envelope-from ) id 1tuHUp-00000002SeR-2Izf; Mon, 17 Mar 2025 14:55:39 -0600 From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [PATCH] Use scoped_fd in linux-nat.c:proc_mem_file Date: Mon, 17 Mar 2025 14:55:29 -0600 Message-ID: <20250317205529.3885971-1-tom@tromey.com> X-Mailer: git-send-email 2.46.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - box5379.bluehost.com X-AntiAbuse: Original Domain - sourceware.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - tromey.com X-BWhitelist: no X-Source-IP: 97.122.96.32 X-Source-L: No X-Exim-ID: 1tuHUp-00000002SeR-2Izf X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: 97-122-96-32.hlrn.qwest.net (localhost.localdomain) [97.122.96.32]:45614 X-Source-Auth: tom+tromey.com X-Email-Count: 1 X-Org: HG=bhshared;ORG=bluehost; X-Source-Cap: ZWx5bnJvYmk7ZWx5bnJvYmk7Ym94NTM3OS5ibHVlaG9zdC5jb20= X-Local-Domain: yes X-CMAE-Envelope: MS4xfJuE27uVgWdU4HGM+IhkZf93qVhKYSsrwr5ewy17/lg3X+iEzuo46akWaSoVhWeSMo9dJyjYvuLEH7qpGyczw1qkafRntCH3NrhIU4DrGk8YeXK+xqaM NOs8b0UA1pfH9EnaFQzeB7R35CfI8MlF2oeipkA23bmKnmt4UKAUMkK4EyljgIF5xcXapZ/mc5Gbqg290tt/Pw56S9vHnVzQq8A= 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 This changes linux-nat.c:proc_mem_file to use a scoped_fd and fixes up the users. Regression tested on x86-64 Fedora 40. --- gdb/linux-nat.c | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/gdb/linux-nat.c b/gdb/linux-nat.c index b16f9f96726..8861829f917 100644 --- a/gdb/linux-nat.c +++ b/gdb/linux-nat.c @@ -4029,24 +4029,26 @@ linux_nat_target::pid_to_exec_file (int pid) class proc_mem_file { public: - proc_mem_file (ptid_t ptid, int fd) - : m_ptid (ptid), m_fd (fd) + proc_mem_file (ptid_t ptid, scoped_fd fd) + : m_ptid (ptid), m_fd (std::move (fd)) { - gdb_assert (m_fd != -1); + gdb_assert (m_fd.get () != -1); } ~proc_mem_file () { linux_nat_debug_printf ("closing fd %d for /proc/%d/task/%ld/mem", - m_fd, m_ptid.pid (), m_ptid.lwp ()); - close (m_fd); + m_fd.get (), m_ptid.pid (), m_ptid.lwp ()); } DISABLE_COPY_AND_ASSIGN (proc_mem_file); - int fd () + proc_mem_file (proc_mem_file &&) = default; + proc_mem_file &operator= (proc_mem_file &&) = default; + + int fd () const noexcept { - return m_fd; + return m_fd.get (); } private: @@ -4055,7 +4057,7 @@ class proc_mem_file ptid_t m_ptid; /* The file descriptor. */ - int m_fd = -1; + scoped_fd m_fd; }; /* The map between an inferior process id, and the open /proc/PID/mem @@ -4093,9 +4095,9 @@ open_proc_mem_file (ptid_t ptid) xsnprintf (filename, sizeof filename, "/proc/%d/task/%ld/mem", ptid.pid (), ptid.lwp ()); - int fd = gdb_open_cloexec (filename, O_RDWR | O_LARGEFILE, 0).release (); + scoped_fd fd = gdb_open_cloexec (filename, O_RDWR | O_LARGEFILE, 0); - if (fd == -1) + if (fd.get () == -1) { warning (_("opening /proc/PID/mem file for lwp %d.%ld failed: %s (%d)"), ptid.pid (), ptid.lwp (), @@ -4103,12 +4105,11 @@ open_proc_mem_file (ptid_t ptid) return; } + linux_nat_debug_printf ("opened fd %d for lwp %d.%ld", + fd.get (), ptid.pid (), ptid.lwp ()); proc_mem_file_map.emplace (std::piecewise_construct, std::forward_as_tuple (ptid.pid ()), - std::forward_as_tuple (ptid, fd)); - - linux_nat_debug_printf ("opened fd %d for lwp %d.%ld", - fd, ptid.pid (), ptid.lwp ()); + std::forward_as_tuple (ptid, std::move (fd))); } /* Helper for linux_proc_xfer_memory_partial and -- 2.46.1