From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id MCn6EL/xRWKeEwAAWB0awg (envelope-from ) for ; Thu, 31 Mar 2022 14:23:59 -0400 Received: by simark.ca (Postfix, from userid 112) id 42A7E1F163; Thu, 31 Mar 2022 14:23:59 -0400 (EDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-2.8 required=5.0 tests=BAYES_00,MAILING_LIST_MULTI, NICE_REPLY_A,RDNS_DYNAMIC,URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.2 Received: from sourceware.org (ip-8-43-85-97.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 BF3FD1ED17 for ; Thu, 31 Mar 2022 14:23:58 -0400 (EDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 3A84B3838006 for ; Thu, 31 Mar 2022 18:23:58 +0000 (GMT) Received: from mail-wr1-f43.google.com (mail-wr1-f43.google.com [209.85.221.43]) by sourceware.org (Postfix) with ESMTPS id AF2C6385741C for ; Thu, 31 Mar 2022 18:22:01 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org AF2C6385741C Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=palves.net Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-wr1-f43.google.com with SMTP id r13so917335wrr.9 for ; Thu, 31 Mar 2022 11:22:01 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:subject :content-language:to:cc:references:from:in-reply-to :content-transfer-encoding; bh=kfSxumYQ6T3zAJZoDu2p5LDAk9FV4UpM2eit4c9CQME=; b=NmQsUO6uh/yJON1msp3WNU2ogjxUpxhzeEo/lZdZHdOnkgB3K427cZERHRUgjPwYtW jz1Qw/mX+eLRx9NeBzfGLVbFyJKYgebE9eFR4k5RVghCpZzt+dx9PlvrAGGv+VHj6ljZ wkTeSO+azzrkO/huQHdNoFGND4zrAWES3LyVz7LwuizTp3L27bh7sxdgv93BVkHkamFj kyfhKUB2qJ3w3uUS0ok35tFHMexgSxep4iLPAimQLPlea4a4iR9llW2+DdsE1qKIdJqR 7zKSQiglQ86nDqJWcKSbob2YNybDQ741yFKoyKmfBuhPuCXpFir8fRo+aYMUM3Ait2af oJXw== X-Gm-Message-State: AOAM533Aikwgc1qEtHZP0pOWgi17OIExEdOZmRUjZDYkzhQfB5xnZpMe m7v/svEg3fKXazpC9Pq8lCzcJGL5yG/PUw== X-Google-Smtp-Source: ABdhPJwlGxjjbmxc++eZYwPKmVC242UuNC54PGirOrvml/JZBNHpqufVeX0k8QgRWmVdPGGc1RKxzw== X-Received: by 2002:a5d:6785:0:b0:204:f8f:8fab with SMTP id v5-20020a5d6785000000b002040f8f8fabmr5088777wru.353.1648750920408; Thu, 31 Mar 2022 11:22:00 -0700 (PDT) Received: from ?IPV6:2001:8a0:f924:2600:209d:85e2:409e:8726? ([2001:8a0:f924:2600:209d:85e2:409e:8726]) by smtp.gmail.com with ESMTPSA id f9-20020a05600c154900b0038cb98076d6sm32975wmg.10.2022.03.31.11.21.59 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 31 Mar 2022 11:21:59 -0700 (PDT) Message-ID: Date: Thu, 31 Mar 2022 19:21:58 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.7.0 Subject: Re: [PATCH 7/8] gdbserver: report correct status in thread stop race condition Content-Language: en-US To: Simon Marchi , gdb-patches@sourceware.org References: <20220117162742.524350-1-simon.marchi@polymtl.ca> <20220117162742.524350-8-simon.marchi@polymtl.ca> From: Pedro Alves In-Reply-To: <20220117162742.524350-8-simon.marchi@polymtl.ca> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit 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: , Cc: Simon Marchi Errors-To: gdb-patches-bounces+public-inbox=simark.ca@sourceware.org Sender: "Gdb-patches" On 2022-01-17 16:27, Simon Marchi via Gdb-patches wrote: > From: Simon Marchi > > The test introduced by the following patch would sometimes fail in this > configuration: > > FAIL: gdb.threads/next-fork-other-thread.exp: fork_func=vfork: target-non-stop=on: non-stop=off: displaced-stepping=auto: i=14: next to for loop > > The test has multiple threads constantly forking or vforking while the > main thread keep doing "next"s. > > (After writing the commit message, I realized this also fixes a similar > failure in gdb.threads/forking-threads-plus-breakpoint.exp with the > native-gdbserver and native-extended-gdbserver boards.) > > As stop_all_threads is called, because the main thread finished its > "next", it inevitably happens at some point that we ask the remote > target to stop a thread and wait() reports that this thread stopped with > a fork or vfork event, instead of the SIGSTOP we sent to try to stop it. > > While running this test, I attached to GDBserver and stopped at > linux-low.cc:3626. We can see that the status pulled from the kernel > for 2742805 is indeed a vfork event: > > (gdb) p/x w > $3 = 0x2057f > (gdb) p WIFSTOPPED(w) > $4 = true > (gdb) p WSTOPSIG(w) > $5 = 5 > (gdb) p/x (w >> 8) & (PTRACE_EVENT_VFORK << 8) > $6 = 0x200 > > However, the statement at line 3626 overrides that: > > ourstatus->set_stopped (gdb_signal_from_host (WSTOPSIG (w))); > > OURSTATUS becomes "stopped by a SIGTRAP". The information about the > fork or vfork is lost. > > It's then all downhill from there, stop_all_threads eventually asks for > a thread list update. That thread list includes the child of that > forgotten fork or vfork, the remote target goes "oh cool, a new process, > let's attach to it!", when in fact that vfork child's destiny was to be > detached. > > My reverse-engineered understanding of the code around there is that the > if/else between lines 3562 and 3583 (in the original code) makes sure > OURSTATUS is always initialized (not "ignore"). Either the details are > already in event_child->waitstatus (in the case of fork/vfork, for > example), in which case we just copy event_child->waitstatus to > ourstatus. Or, if the event is a plain "stopped by a signal" or a > syscall event, OURSTATUS is set to "stopped", but without a signal > number. Lines 3601 to 3629 (in the original code) serve to fill in that > last bit of information. > > The problem is that when `w` holds the vfork status, the code wrongfully > takes this branch, because WSTOPSIG(w) returns SIGTRAP: > > else if (current_thread->last_resume_kind == resume_stop > && WSTOPSIG (w) != SIGSTOP) > > The intent of this branch is, for example, when we sent SIGSTOP to try > to stop a thread, but wait() reports that it stopped with another signal > (that it must have received from somewhere else simultaneously), say > SIGWINCH. In that case, we want to report the SIGWINCH. But in our > fork/vfork case, we don't want to take this branch, as the thread didn't > really stop because it received a signal. For the non "stopped by a > signal" and non "syscall signal" cases, we would ideally skip over all > that snippet that fills in the signal or syscall number. > > The fix I propose is to move this snipppet of the else branch of the > if/else above. In addition to moving the code, the last two "else if" > branches: > > else if (current_thread->last_resume_kind == resume_stop > && WSTOPSIG (w) != SIGSTOP) > { > /* A thread that has been requested to stop by GDB with vCont;t, > but, it stopped for other reasons. */ > ourstatus->set_stopped (gdb_signal_from_host (WSTOPSIG (w))); > } > else if (ourstatus->kind () == TARGET_WAITKIND_STOPPED) > ourstatus->set_stopped (gdb_signal_from_host (WSTOPSIG (w))); > > are changed into a single else: > > else > ourstatus->set_stopped (gdb_signal_from_host (WSTOPSIG (w))); > > This is the default path we take if: > > - W is not a syscall status > - W does not represent a SIGSTOP that have sent to stop the thread and > therefore want to suppress it > > Change-Id: If2dc1f0537a549c293f7fa3c53efd00e3e194e79 This is OK. Thanks.