From patchwork Sun Mar 24 13:02:37 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Philippe Waroquiers X-Patchwork-Id: 31965 Received: (qmail 50043 invoked by alias); 24 Mar 2019 13:02:47 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 50026 invoked by uid 89); 24 Mar 2019 13:02:46 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-23.7 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_LOW, SPF_PASS autolearn=ham version=3.3.1 spammy= X-HELO: mailsec106.isp.belgacom.be Received: from mailsec106.isp.belgacom.be (HELO mailsec106.isp.belgacom.be) (195.238.20.102) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Sun, 24 Mar 2019 13:02:44 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=skynet.be; i=@skynet.be; q=dns/txt; s=securemail; t=1553432564; x=1584968564; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=DtdeN//twxJvKvHmgqPLKzmUak/1GbUhy2+BQcvzRDk=; b=UWYDZFje9v9Zcg67bN7kEe464B34uR9vCd5h4FQxp+PkpHu8J+3NLVCV z85JfZyHwbDdNqlln2kWyXSJTZD8Aw==; Received: from 147.122-130-109.adsl-dyn.isp.belgacom.be (HELO md.home) ([109.130.122.147]) by relay.skynet.be with ESMTP/TLS/DHE-RSA-AES128-GCM-SHA256; 24 Mar 2019 14:02:42 +0100 From: Philippe Waroquiers To: gdb-patches@sourceware.org Cc: Philippe Waroquiers Subject: [OBVIOUS/PUSHED] (re-)fix the regcache leaks when detaching from an executable. Date: Sun, 24 Mar 2019 14:02:37 +0100 Message-Id: <20190324130237.19835-1-philippe.waroquiers@skynet.be> MIME-Version: 1.0 X-IsSubscribed: yes Commit 799efbe8e01ab8292c01f46ac59a6fb2349d4535 was supposed to fix the below leak. However, for this fix to work, it is critical to save the ptid before detach. This commit (pushed as OBVIOUS, as the change was already reviewed/approved) saves the ptid before the detach, as in the original reviewed patch (see https://sourceware.org/ml/gdb-patches/2019-02/msg00263.html). Re-tested on debian/amd64, natively and under valgrind. ==7426== 1,123 (72 direct, 1,051 indirect) bytes in 1 blocks are definitely lost in loss record 2,872 of 3,020 ==7426== at 0x4C2C4CC: operator new(unsigned long) (vg_replace_malloc.c:344) ==7426== by 0x5BD1E1: get_thread_arch_aspace_regcache(ptid_t, gdbarch*, address_space*) (regcache.c:330) ==7426== by 0x5BD39A: get_thread_regcache (regcache.c:366) ==7426== by 0x5BD39A: get_current_regcache() (regcache.c:372) ==7426== by 0x4B1EB4: get_current_frame() (frame.c:1588) ... --- gdb/target.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/gdb/target.c b/gdb/target.c index 5f596b6a3c..6c05b6b83e 100644 --- a/gdb/target.c +++ b/gdb/target.c @@ -2013,6 +2013,11 @@ target_preopen (int from_tty) void target_detach (inferior *inf, int from_tty) { + /* After we have detached, we will clear the register cache for this inferior + by calling registers_changed_ptid. We must save the pid_ptid before + detaching, as the target detach method will clear inf->pid. */ + ptid_t save_pid_ptid = ptid_t (inf->pid); + /* As long as some to_detach implementations rely on the current_inferior (either directly, or indirectly, like through target_gdbarch or by reading memory), INF needs to be the current inferior. When that @@ -2033,14 +2038,11 @@ target_detach (inferior *inf, int from_tty) current_top_target ()->detach (inf, from_tty); - /* After we have detached, clear the register cache for this inferior. */ - ptid_t pid_ptid = ptid_t (inf->pid); - - registers_changed_ptid (pid_ptid); + registers_changed_ptid (save_pid_ptid); /* We have to ensure we have no frame cache left. Normally, - registers_changed_ptid (pid_ptid) calls reinit_frame_cache when - inferior_ptid matches pid_ptid, but in our case, it does not + registers_changed_ptid (save_pid_ptid) calls reinit_frame_cache when + inferior_ptid matches save_pid_ptid, but in our case, it does not call it, as inferior_ptid has been reset. */ reinit_frame_cache (); }