From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id GnoFB7eCMmACRAAAWB0awg (envelope-from ) for ; Sun, 21 Feb 2021 10:56:39 -0500 Received: by simark.ca (Postfix, from userid 112) id 04FF21EF78; Sun, 21 Feb 2021 10:56:39 -0500 (EST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-1.1 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,MAILING_LIST_MULTI,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.2 Received: from 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 RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id DD4ED1E590 for ; Sun, 21 Feb 2021 10:56:37 -0500 (EST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 061253857C50; Sun, 21 Feb 2021 15:56:37 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 061253857C50 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1613922997; bh=3yFtATTGXr6rOAe7rqr0zhmbmZyifA4M/F0jImZ4yOs=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=Vm5vuUSqqVwCvfRuAOd16R1V5E9rg4MoZx1nrstYtndCEGuU+YwhPPf5p+h4Oeis7 rNScC43G8mYSBmxl69duNjY5HxSpJx2znV7WFiUfbfjNKOLvAo2DXPAgXpC1ydwglt xRgMWAMSyGEjd609IBZSn77pS9hsx4hSDvPq0h8k= Received: from barracuda.ebox.ca (barracuda.ebox.ca [96.127.255.19]) by sourceware.org (Postfix) with ESMTPS id 805F1385803F for ; Sun, 21 Feb 2021 15:56:32 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 805F1385803F X-ASG-Debug-ID: 1613922991-0c856e67e27b8ae0001-fS2M51 Received: from smtp.ebox.ca (smtp.ebox.ca [96.127.255.82]) by barracuda.ebox.ca with ESMTP id 2EWIQTyDBfTjnlBl (version=TLSv1 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Sun, 21 Feb 2021 10:56:31 -0500 (EST) X-Barracuda-Envelope-From: simon.marchi@polymtl.ca X-Barracuda-RBL-Trusted-Forwarder: 96.127.255.82 Received: from simark.localdomain (192-222-157-6.qc.cable.ebox.net [192.222.157.6]) by smtp.ebox.ca (Postfix) with ESMTP id 4F856441B21; Sun, 21 Feb 2021 10:56:31 -0500 (EST) X-Barracuda-RBL-IP: 192.222.157.6 X-Barracuda-Effective-Source-IP: 192-222-157-6.qc.cable.ebox.net[192.222.157.6] X-Barracuda-Apparent-Source-IP: 192.222.157.6 To: gdb-patches@sourceware.org Subject: [PATCH 1/2] gdb: push target earlier in procfs_target::attach (PR 27435) Date: Sun, 21 Feb 2021 10:56:29 -0500 X-ASG-Orig-Subj: [PATCH 1/2] gdb: push target earlier in procfs_target::attach (PR 27435) Message-Id: <20210221155630.3026942-1-simon.marchi@polymtl.ca> X-Mailer: git-send-email 2.30.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Barracuda-Connect: smtp.ebox.ca[96.127.255.82] X-Barracuda-Start-Time: 1613922991 X-Barracuda-Encrypted: DHE-RSA-AES256-SHA X-Barracuda-URL: https://96.127.255.19:443/cgi-mod/mark.cgi X-Virus-Scanned: by bsmtpd at ebox.ca X-Barracuda-Scan-Msg-Size: 4374 X-Barracuda-BRTS-Status: 1 X-Barracuda-Spam-Score: 0.00 X-Barracuda-Spam-Status: No, SCORE=0.00 using global scores of TAG_LEVEL=1000.0 QUARANTINE_LEVEL=1000.0 KILL_LEVEL=8.0 tests= X-Barracuda-Spam-Report: Code version 3.2, rules version 3.2.3.88051 Rule breakdown below pts rule name description ---- ---------------------- -------------------------------------------------- X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Simon Marchi via Gdb-patches Reply-To: Simon Marchi Cc: Libor Bukata Errors-To: gdb-patches-bounces@sourceware.org Sender: "Gdb-patches" Since this is a GDB 9 -> 10 regression, I would like to push it to gdb-10-branch. This is a follow-up to: https://sourceware.org/pipermail/gdb-patches/2021-February/176202.html This patch fixes a segfault seen when attaching to a process on Solaris. The steps leading to the segfault are: - procfs_target::attach calls do_attach, at this point the inferior's process slot in the target stack is empty. - do_attach adds a thread with `add_thread (&the_procfs_target, ptid)` - in add_thread_silent, the passed target (&the_procfs_target) is passed to find_inferior_ptid - find_inferior_ptid returns nullptr, as there is no inferior with this ptid that has &the_procfs_target as its process target - the nullptr `inf` is passed to find_thread_ptid, which dereferences it, causing a segfault - back in procfs_target::attach, after do_attach, we push the the_procfs_target on the inferior's target stack, although we never reach this because the segfault happens before. To fix this, I think we need to do the same as is done in inf_ptrace_target::attach: push the target early and unpush it in case the attach fails (and keep it if the attach succeeds). Implement it by moving target_unpush_up to target.h, so it can be re-used here. Make procfs_target::attach use it. Note that just like is mentioned in inf_ptrace_target::attach, we should push the target before calling target_pid_to_str, so that calling target_pid_to_str ends up in procfs_target::pid_to_str. Tested by trying to attach on a process on gcc211 on the gcc compile farm. gdb/ChangeLog: PR gdb/27435 * inf-ptrace.c (struct target_unpusher): Move to target.h. (target_unpush_up): Likewise. * procfs.c (procfs_target::attach): Push target early. Use target_unpush_up to unpush target in case of error. * target.h (struct target_unpusher): Move here. (target_unpush_up): Likewise. Change-Id: I88aff8b20204e1ca1d792e27ac6bc34fc1aa0d52 --- gdb/inf-ptrace.c | 16 ---------------- gdb/procfs.c | 14 ++++++++++++-- gdb/target.h | 14 ++++++++++++++ 3 files changed, 26 insertions(+), 18 deletions(-) diff --git a/gdb/inf-ptrace.c b/gdb/inf-ptrace.c index 0f2f56cc3660..7ca02dfd8764 100644 --- a/gdb/inf-ptrace.c +++ b/gdb/inf-ptrace.c @@ -49,22 +49,6 @@ gdb_ptrace (PTRACE_TYPE_ARG1 request, ptid_t ptid, PTRACE_TYPE_ARG3 addr, #endif } -/* A unique_ptr helper to unpush a target. */ - -struct target_unpusher -{ - void operator() (struct target_ops *ops) const - { - unpush_target (ops); - } -}; - -/* A unique_ptr that unpushes a target on destruction. */ - -typedef std::unique_ptr target_unpush_up; - - - inf_ptrace_target::~inf_ptrace_target () {} diff --git a/gdb/procfs.c b/gdb/procfs.c index e73faa8d41dc..cab29c3cbbcb 100644 --- a/gdb/procfs.c +++ b/gdb/procfs.c @@ -1767,6 +1767,14 @@ procfs_target::attach (const char *args, int from_tty) if (pid == getpid ()) error (_("Attaching GDB to itself is not a good idea...")); + /* Push the target if needed, ensure it gets un-pushed it if attach fails. */ + target_unpush_up unpusher; + if (!target_is_pushed (this)) + { + push_target (this); + unpusher.reset (this); + } + if (from_tty) { const char *exec_file = get_exec_file (0); @@ -1780,9 +1788,11 @@ procfs_target::attach (const char *args, int from_tty) fflush (stdout); } + do_attach (ptid_t (pid)); - if (!target_is_pushed (this)) - push_target (this); + + /* Everything went fine, keep the target pushed. */ + unpusher.release (); } void diff --git a/gdb/target.h b/gdb/target.h index 0de78075e9b5..52e23b05ffd8 100644 --- a/gdb/target.h +++ b/gdb/target.h @@ -2392,6 +2392,20 @@ extern void push_target (target_ops_up &&); extern int unpush_target (struct target_ops *); +/* A unique_ptr helper to unpush a target. */ + +struct target_unpusher +{ + void operator() (struct target_ops *ops) const + { + unpush_target (ops); + } +}; + +/* A unique_ptr that unpushes a target on destruction. */ + +typedef std::unique_ptr target_unpush_up; + extern void target_pre_inferior (int); extern void target_preopen (int); -- 2.30.1