From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wm1-f49.google.com (mail-wm1-f49.google.com [209.85.128.49]) by sourceware.org (Postfix) with ESMTPS id 99434385803F for ; Thu, 3 Mar 2022 14:40:48 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 99434385803F 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-f49.google.com with SMTP id bg31-20020a05600c3c9f00b00381590dbb33so3361396wmb.3 for ; Thu, 03 Mar 2022 06:40:48 -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:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=/jaeFoc3CiYPyHhFcrOB9uA+A9HVLGwnHO8JyJl4jtU=; b=e0fabDusMZ3KiJxgOx5/XSar4NZ6V3QWomvRGbj7xQnIifPdrjVoJBTu+vS2MxV4GK 9/OzImjEF/9Vl89CMFPl10NECjmPzNi8E7fY3RzFquhOw9M9B6+7gJW5DVDdpW138kIt GG7/eXKVqT9QFirliqJ0k3T1f+FU6NtqZt+wNpWihB84s5opGpWzxmJ9l+hASsgvQxEn LhIACrHmxXYSnGbKD1YpHcmwMV4YEFFX6wejKoer1l4w4NSR2ON4UJyNwbgjNav2XFIN BkvfNQF15EL/p76o0LspZWukyVM87cWGkG0nVsNWRIe0aL3vYmBpBbKooamuq2okqjzc uHXQ== X-Gm-Message-State: AOAM5307ElSZmIyRn/AIkDsK46Hi9rG/j9l3/vsRYYw1xA3dLdixj4W5 fR9qe+5we8QkT7mse4iNaNrNc1F+h+Q= X-Google-Smtp-Source: ABdhPJxPTssVXdlrEd1XYbmlHZYmKHVPEZsk1yV+S5A/0TZtXzxZ8Vxqmk5A4E6Wtt24H9r5N0Majg== X-Received: by 2002:a05:600c:3596:b0:381:751a:f0a6 with SMTP id p22-20020a05600c359600b00381751af0a6mr4054858wmq.168.1646318446908; Thu, 03 Mar 2022 06:40:46 -0800 (PST) Received: from localhost ([2001:8a0:f924:2600:209d:85e2:409e:8726]) by smtp.gmail.com with ESMTPSA id l7-20020adfc787000000b001f049375350sm2344884wrg.8.2022.03.03.06.40.45 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 03 Mar 2022 06:40:45 -0800 (PST) From: Pedro Alves To: gdb-patches@sourceware.org Subject: [PATCH 11/11] Process exit status is leader exit status testcase Date: Thu, 3 Mar 2022 14:40:20 +0000 Message-Id: <20220303144020.3601082-12-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=-10.1 required=5.0 tests=BAYES_00, FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM, GIT_PATCH_0, HEADER_FROM_DIFFERENT_DOMAINS, KAM_DMARC_STATUS, KAM_SHORT, 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:50 -0000 From: Lancelot SIX This adds a multi-threaded testcase that has all threads in the process exit with a different exit code, and ensures that GDB reports the thread group leader's exit status as the whole-process exit status. Before this set of patches, this would randomly report the exit code of some other thread, and thus fail. Tested on Linux-x86_64, native and gdbserver. Co-Authored-By: Pedro Alves Change-Id: I30cba2ff4576fb01b5169cc72667f3268d919557 --- ...rocess-exit-status-is-leader-exit-status.c | 64 +++++++++++++++++++ ...cess-exit-status-is-leader-exit-status.exp | 45 +++++++++++++ 2 files changed, 109 insertions(+) create mode 100644 gdb/testsuite/gdb.threads/process-exit-status-is-leader-exit-status.c create mode 100644 gdb/testsuite/gdb.threads/process-exit-status-is-leader-exit-status.exp diff --git a/gdb/testsuite/gdb.threads/process-exit-status-is-leader-exit-status.c b/gdb/testsuite/gdb.threads/process-exit-status-is-leader-exit-status.c new file mode 100644 index 00000000000..c27c266ef28 --- /dev/null +++ b/gdb/testsuite/gdb.threads/process-exit-status-is-leader-exit-status.c @@ -0,0 +1,64 @@ +/* This testcase is part of GDB, the GNU debugger. + + Copyright 2022 Free Software Foundation, Inc. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +#include +#include +#include + +#define NUM_THREADS 32 + +pthread_barrier_t barrier; + +static void +do_exit (int exitcode) +{ + /* Synchronize all threads up to here so that they all exit at + roughly the same time. */ + pthread_barrier_wait (&barrier); + + /* All threads exit with SYS_exit, even the main thread, to avoid + exiting with a group-exit syscall, as that syscall changes the + exit status of all still-alive threads, thus potentially masking + a bug. */ + syscall (SYS_exit, exitcode); +} + +static void * +start (void *arg) +{ + int thread_return_value = *(int *) arg; + + do_exit (thread_return_value); +} + +int +main(void) +{ + pthread_t threads[NUM_THREADS]; + int thread_return_val[NUM_THREADS]; + int i; + + pthread_barrier_init (&barrier, NULL, NUM_THREADS + 1); + + for (i = 0; i < NUM_THREADS; ++i) + { + thread_return_val[i] = i + 2; + pthread_create (&threads[i], NULL, start, &thread_return_val[i]); + } + + do_exit (1); +} diff --git a/gdb/testsuite/gdb.threads/process-exit-status-is-leader-exit-status.exp b/gdb/testsuite/gdb.threads/process-exit-status-is-leader-exit-status.exp new file mode 100644 index 00000000000..655e4bc209b --- /dev/null +++ b/gdb/testsuite/gdb.threads/process-exit-status-is-leader-exit-status.exp @@ -0,0 +1,45 @@ +# Copyright (C) 2022 Free Software Foundation, Inc. + +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +# GDB must always report the process's exit status based on the exit +# status of the thread group leader thread. Test that when multiple +# threads exit simultaneously, GDB doesn't confuse the non-leader +# threads' exit status for the process's exit status. GDB used to +# have a race condition to led to randomly handling this incorrectly. +# +# Since the improper behavior is racy in nature, this test is not +# expected to be able to reproduce the error reliably. Multiple +# executions (or increasing the number of iterations) might be +# required to reproduce the error with a misbehaving GDB. + +if { ![istarget "*-*-linux*"] } { + return 0 +} + +standard_testfile + +if {[prepare_for_testing "failed to prepare" $testfile $srcfile {debug pthreads}] == -1} { + return -1 +} + +for {set iteration 0} {$iteration < 10} {incr iteration} { + with_test_prefix "iteration=$iteration" { + if {![runto_main]} { + return + } + + gdb_test "continue" "\\\[Inferior 1 \\(.*\\) exited with code 01\\\]" + } +} -- 2.26.2