From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from barracuda.ebox.ca (barracuda.ebox.ca [96.127.255.19]) by sourceware.org (Postfix) with ESMTPS id 3047D3858D35 for ; Sat, 1 Aug 2020 22:24:49 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 3047D3858D35 X-ASG-Debug-ID: 1596320673-0c856e1810345080001-fS2M51 Received: from smtp.ebox.ca (smtp.ebox.ca [96.127.255.82]) by barracuda.ebox.ca with ESMTP id 0YHXXEAkZOsUjvVX (version=TLSv1 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Sat, 01 Aug 2020 18:24:33 -0400 (EDT) X-Barracuda-Envelope-From: simon.marchi@efficios.com X-Barracuda-RBL-Trusted-Forwarder: 96.127.255.82 Received: from epycamd.internal.efficios.com (192-222-181-218.qc.cable.ebox.net [192.222.181.218]) by smtp.ebox.ca (Postfix) with ESMTP id 9CE50441B21; Sat, 1 Aug 2020 18:24:33 -0400 (EDT) From: Simon Marchi X-Barracuda-RBL-IP: 192.222.181.218 X-Barracuda-Effective-Source-IP: 192-222-181-218.qc.cable.ebox.net[192.222.181.218] X-Barracuda-Apparent-Source-IP: 192.222.181.218 To: gdb-patches@sourceware.org Subject: [PATCH] gdb: don't use inferior_ptid in linux_nat_wait_1 Date: Sat, 1 Aug 2020 18:24:32 -0400 X-ASG-Orig-Subj: [PATCH] gdb: don't use inferior_ptid in linux_nat_wait_1 Message-Id: <20200801222432.7404-1-simon.marchi@efficios.com> X-Mailer: git-send-email 2.28.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Barracuda-Connect: smtp.ebox.ca[96.127.255.82] X-Barracuda-Start-Time: 1596320673 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: 2688 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.83626 Rule breakdown below pts rule name description ---- ---------------------- -------------------------------------------------- X-Spam-Status: No, score=-23.5 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_NONE, KAM_DMARC_STATUS, KAM_NUMSUBJECT, RCVD_IN_DNSWL_LOW, SPF_HELO_NONE, SPF_SOFTFAIL, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org 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: , X-List-Received-Date: Sat, 01 Aug 2020 22:24:50 -0000 From: Simon Marchi target_ops::wait implementations should not rely on the value of inferior_ptid on entry. While looking at another wait-related patch, I noticed that the code in linux_nat_wait_1, checking for a newly created process, did just that. This patch fixes it. Note that I didn't see any bug, this "fix" is simply to make the function respect the target_ops::wait contract. Instead of checking inferior_ptid, check for the passed in `ptid` value. During startup, linux_nat_wait_1 gets called a few times with the pid-only ptid, while startup_inferior waits for the expected number of exec events. For this reason, I needed to add a `find_lwp_pid` call to ensure that the actions of changing the main thread's ptid, and adding the initial lwp, were done only once for a given process. This was not needed before, since thread_change_ptid, through the thread_ptid_changed observer, ends up changing inferior_ptid. So the second time around, inferior_ptid was not a pid-only ptid. That find_lwp_pid won't add much overhead, as it will only be called when the ptid is a pid-only ptid. And AFAIK, that only happens during inferior startup. An alternative to that `find_lwp_pid` call might be to make startup_inferior realize that the main thread has changed ptid, and make it wait for the new ptid. But that doesn't look easy to do. Regtested on amd64/Linux. gdb/ChangeLog: * linux-nat.c (linux_nat_wait_1): Don't use inferior_ptid when checking for initial lwp. Change-Id: I8f1d5c766f5cb2a29c948bc75fa4582d7130c23f --- gdb/linux-nat.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/gdb/linux-nat.c b/gdb/linux-nat.c index 20b03bc2ba9..75c6d219d6a 100644 --- a/gdb/linux-nat.c +++ b/gdb/linux-nat.c @@ -3277,14 +3277,13 @@ linux_nat_wait_1 (ptid_t ptid, struct target_waitstatus *ourstatus, /* The first time we get here after starting a new inferior, we may not have added it to the LWP list yet - this is the earliest moment at which we know its PID. */ - if (inferior_ptid.is_pid ()) + if (ptid.is_pid () && find_lwp_pid (ptid) == nullptr) { - /* Upgrade the main thread's ptid. */ - thread_change_ptid (linux_target, inferior_ptid, - ptid_t (inferior_ptid.pid (), - inferior_ptid.pid (), 0)); + ptid_t lwp_ptid (ptid.pid (), ptid.pid ()); - lp = add_initial_lwp (inferior_ptid); + /* Upgrade the main thread's ptid. */ + thread_change_ptid (linux_target, ptid, lwp_ptid); + lp = add_initial_lwp (lwp_ptid); lp->resumed = 1; } -- 2.28.0