From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wm1-f42.google.com (mail-wm1-f42.google.com [209.85.128.42]) by sourceware.org (Postfix) with ESMTPS id E317B385801B for ; Thu, 3 Mar 2022 14:40:28 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org E317B385801B 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-wm1-f42.google.com with SMTP id l1-20020a05600c4f0100b00389645443d2so518181wmq.2 for ; Thu, 03 Mar 2022 06:40:28 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=wvMHM3X/PZ9zvtK2jtz1vfkWlpNw2L/tqmKzgShFMnI=; b=C0fkdrDB6NVFbYDOpcM6gmBZxfyronP4N9yeN8HqrRCHjduWa6OqR9KBoYsgn/HzLO qaxrf2IDuJ51pkz618MYhmLjPVG8xIfzBhaDwCGLeZ94L9+sDna9BDxFezlY+wwLxm74 0J+8vZVI9q63qYgJNQl55XyhjG+LEgbrO4/azFDXkDLR9hXrS0K2BRXqGGyE9p+FuU8C 2hUTxA+e8Mg6qfVTHjImdSXcIGlhF6Er+XL94lR+54Sn3LMCCreXzoIdpZzTk8xFxf1Z tvG1fuayNp6iPDIhhCoJeRxPLnYcnMGY0LM2VW7Ac5rFVggQBZF9ApSzgzZDZPXmXj68 6o1A== X-Gm-Message-State: AOAM5326bnVAWvH/eHfL1HwjZcI+H6yLpznS6pKBra9aaZYczJohNPgI h/Ssp6CvH3kg6cSBIxjTT6fmS2gASfw= X-Google-Smtp-Source: ABdhPJxoLljRkfwh4uwYvOB5VraSpz3bMN5bNhaIftcC/FCVk46yZxXL5dy9VZY7LFaOxdYMbuUDkQ== X-Received: by 2002:a05:600c:4151:b0:381:4bed:a355 with SMTP id h17-20020a05600c415100b003814beda355mr4091611wmm.135.1646318427088; Thu, 03 Mar 2022 06:40:27 -0800 (PST) Received: from localhost ([2001:8a0:f924:2600:209d:85e2:409e:8726]) by smtp.gmail.com with ESMTPSA id f10-20020a05600c154a00b0037bbbc15ca7sm18461101wmg.36.2022.03.03.06.40.25 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 03 Mar 2022 06:40:26 -0800 (PST) From: Pedro Alves To: gdb-patches@sourceware.org Subject: [PATCH 02/11] Fix gdb.threads/clone-new-thread-event.exp race Date: Thu, 3 Mar 2022 14:40:11 +0000 Message-Id: <20220303144020.3601082-3-pedro@palves.net> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20220303144020.3601082-1-pedro@palves.net> References: <20220303144020.3601082-1-pedro@palves.net> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-9.8 required=5.0 tests=BAYES_00, FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM, GIT_PATCH_0, HEADER_FROM_DIFFERENT_DOMAINS, KAM_DMARC_STATUS, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) 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: Thu, 03 Mar 2022 14:40:30 -0000 If we make GDB report the process EXIT event for the leader thread, instead of whatever is the last thread in the LWP list, as will be done in a latter patch of this series, then gdb.threads/current-lwp-dead.exp starts failing: (gdb) FAIL: gdb.threads/clone-new-thread-event.exp: catch SIGUSR1 (the program exited) This is a testcase race -- the main thread does not wait for the spawned clone "thread" to finish before exiting, so the main program may exit before the second thread is scheduled and reports its SIGUSR1. With the change to make GDB report the EXIT for the leader, the race is 100% reproducible by adding a sleep(), like so: --- c/gdb/testsuite/gdb.threads/clone-new-thread-event.c +++ w/gdb/testsuite/gdb.threads/clone-new-thread-event.c @@ -51,6 +51,7 @@ local_gettid (void) static int fn (void *unused) { + sleep (1); tkill (local_gettid (), SIGUSR1); return 0; } Resulting in: Breakpoint 1, main (argc=1, argv=0x7fffffffd418) at gdb.threads/clone-new-thread-event.c:65 65 stack = malloc (STACK_SIZE); (gdb) continue Continuing. [New LWP 3715562] [Inferior 1 (process 3715555) exited normally] (gdb) FAIL: gdb.threads/clone-new-thread-event.exp: catch SIGUSR1 (the program exited) That inferior exit reported is actually correct. The main thread has indeed exited, and that's the thread that has the right exit code to report to the user, as that's the exit code that is reported to the program's parent. In this case, GDB managed to collect the exit code for the leader thread before reaping the other thread, because in reality, the testcase isn't creating standard threads, it is using raw clone, and the new clones are put in their own thread group. Fix it by making the main thread wait for the child to exit. Also, run the program to completion for completeness. Change-Id: I315cd3dc2b9e860395dcab9658341ea868d7a6bf --- .../gdb.threads/clone-new-thread-event.c | 16 +++++++++++++++- .../gdb.threads/clone-new-thread-event.exp | 2 ++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/gdb/testsuite/gdb.threads/clone-new-thread-event.c b/gdb/testsuite/gdb.threads/clone-new-thread-event.c index 800514d0b6d..9e7ceb97a65 100644 --- a/gdb/testsuite/gdb.threads/clone-new-thread-event.c +++ b/gdb/testsuite/gdb.threads/clone-new-thread-event.c @@ -26,6 +26,7 @@ #include #include #include +#include #include #ifdef __UCLIBC__ @@ -59,7 +60,7 @@ int main (int argc, char **argv) { unsigned char *stack; - int new_pid; + int new_pid, status, ret; stack = malloc (STACK_SIZE); assert (stack != NULL); @@ -71,5 +72,18 @@ main (int argc, char **argv) , NULL, NULL, NULL, NULL); assert (new_pid > 0); + /* Note the clone call above didn't use CLONE_THREAD, so it actually + put the new thread in a new thread group. However, the new clone + is still reported with PTRACE_EVENT_CLONE to GDB, since we didn't + use CLONE_VFORK (results in PTRACE_EVENT_VFORK) nor set the + termination signal to SIGCHLD (results in PTRACE_EVENT_FORK), so + GDB thinks of it as a new thread of the same inferior. It's a + bit of an odd setup, but it's not important for what we're + testing, and, it let's us conveniently use waitpid to wait for + the clone, which you can't with CLONE_THREAD. */ + ret = waitpid (new_pid, &status, __WALL); + assert (ret == new_pid); + assert (WIFSIGNALED (status) && WTERMSIG (status) == SIGUSR1); + return 0; } diff --git a/gdb/testsuite/gdb.threads/clone-new-thread-event.exp b/gdb/testsuite/gdb.threads/clone-new-thread-event.exp index fdebd488d8a..db5ae39aded 100644 --- a/gdb/testsuite/gdb.threads/clone-new-thread-event.exp +++ b/gdb/testsuite/gdb.threads/clone-new-thread-event.exp @@ -31,3 +31,5 @@ if { ![runto_main] } { gdb_test "continue" \ "Thread 2 received signal SIGUSR1, User defined signal 1.*" \ "catch SIGUSR1" + +gdb_continue_to_end "" continue 1 -- 2.26.2