From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id oeBzKdOgFGqyHxoAWB0awg (envelope-from ) for ; Mon, 25 May 2026 15:19:47 -0400 Received: by simark.ca (Postfix, from userid 112) id A122E1E0A6; Mon, 25 May 2026 15:19:47 -0400 (EDT) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-2.3 required=5.0 tests=ARC_SIGNED,ARC_VALID,BAYES_00, MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED,RCVD_IN_VALIDITY_RPBL_BLOCKED, RCVD_IN_VALIDITY_SAFE_BLOCKED autolearn=ham autolearn_force=no version=4.0.1 Received: from vm01.sourceware.org (vm01.sourceware.org [38.145.34.32]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id 2EEDF1E091 for ; Mon, 25 May 2026 15:19:46 -0400 (EDT) Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id B04EE4BA799F for ; Mon, 25 May 2026 19:19:45 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B04EE4BA799F Received: from mail-wm1-f50.google.com (mail-wm1-f50.google.com [209.85.128.50]) by sourceware.org (Postfix) with ESMTPS id C85DD4BA23C4 for ; Mon, 25 May 2026 19:18:46 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C85DD4BA23C4 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=palves.net Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org C85DD4BA23C4 Authentication-Results: sourceware.org; arc=none smtp.remote-ip=209.85.128.50 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1779736727; cv=none; b=OWzlQwfiqNkRJ41xo85tU0wEPX9q6l5R53poAXKfsEcfSQDydgfWtPKJqfNreRurUdHNEvFpGIZ25V62jwk4ichZBHF+2YS7B2XikGTed+eM9/Wx2IxFSuCAHudVbXVw4atBOtHvFgL2F4JXN4hK8YPPpoGUlsaOWhMu0IXg12s= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1779736727; c=relaxed/simple; bh=mBBFVEfZsx+2DSqZaWIUwexvJOxisvJiZ55dclsWsQ8=; h=From:To:Subject:Date:Message-ID:MIME-Version; b=Lwf9e7RM+BOLWTJ2AAsYEmtkpatp73FtuYGSkc7N4iqTv/IIAgoCOiB+XnRpo7uBSF4pq+kCob2PWJYGbdgE7yPCm7YLMgjAExb6vqqOhsFvpchoskGf8D3bG/Y7aH0A7orI4DhRddHH2X32Xlbu45W/T/QueiUmOFk/NW4oFCs= ARC-Authentication-Results: i=1; sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C85DD4BA23C4 Received: by mail-wm1-f50.google.com with SMTP id 5b1f17b1804b1-4906238c62eso12735805e9.3 for ; Mon, 25 May 2026 12:18:46 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1779736726; x=1780341526; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-gg:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=zAQWdDTV6yRCgW1uo7OZ3fcimriK4tn6NK+hY8ia6CA=; b=h14Ugt6ONemnD6P2/fC0V/enCs/pe2OLojY9Gd7R17+AhB71Go+RElbhyzoIvz4fms R3uIXQi0pq2TlPJxcdoObnnNSo9wygewvqOn8pwJLLZiBABlyylaPlgTB8sHjwHzJ8BO CJlFbuEvaZte9uWgVwvMNWfLJAl4FRLHMVSSHQGj64+pwL4pS/Fd5FGEvWYLe9dYTKOh XA9SHY8y1iYXDa3aqda/oHiNLTLT0huexyyRYl7wCE+Tjqil5sy2FF2hfu6VBa2gc8Qu w+4/BH4WsvQUX5UTrWltOwJBVeXuCgKJZbsIV4c7MXrQ/jzeWjwAYs8RY+EFmunn09i7 madw== X-Gm-Message-State: AOJu0Yy8KHOOECfpiJ/+mfvqnRP1EV8xk59h9tuZWYJhqhNE7f2bhPpN n0AKj0UMyyzMVTlhdsVpWu6M3+0i6qxIIkh4Fe5jB69ybbfyqjY2B1w59Jg5ig== X-Gm-Gg: Acq92OFBKHOlYdJzynBe092KeCNaQZH4igfKrNr68IZfir8HCudVW8KyI+sTtns2AKx gbySyDZfboFR/Fglfm5KJMWs2lAmsknvHWrlXT0Ew7bRjsXfD/m6VYfbqqAAmqzk9PFtz2fxqRJ F3C4T3+FV+S9jCdIRPrI5HuVEobawWyKXuW1BlGvYZhEANVLQax31zbSX7yAP933+e7hQeURpBy BFYZ5xaP3E71I57xwYWYsTGJOR1NxW3LAVMSDP7XnWXEkyZrKRMkaqrH5+FiCbZjXTDxXcTMbwj ez5oxceGrtW13VFuhWRAvEgOOL1fRX84XeshmyLdpnpqc8IQ32VdPDqPLCmKgZg0/JuzFgSXqwK tTc0zxIIwsNFx1pG26iD8dO2//zsTNLWhCgk0FkUnsXGnyMG6Ohrm+H5L0ml2Z/R7lL5aMoSYx2 YXOq05oe6sB+/dHi2uIp+nVTJVil9mc9s+ X-Received: by 2002:a05:600c:c0d2:10b0:490:33b3:4be0 with SMTP id 5b1f17b1804b1-490426c1759mr194073285e9.20.1779736725403; Mon, 25 May 2026 12:18:45 -0700 (PDT) Received: from localhost ([2001:8a0:fae3:2600:dc16:ec0f:632c:5ba7]) by smtp.gmail.com with UTF8SMTPSA id 5b1f17b1804b1-49059cc9a91sm51830215e9.1.2026.05.25.12.18.44 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 25 May 2026 12:18:45 -0700 (PDT) From: Pedro Alves To: gdb-patches@sourceware.org Subject: [PATCH v2 05/11] gdb.base/exitsignal.exp: Test attaching too Date: Mon, 25 May 2026 20:18:23 +0100 Message-ID: <20260525191829.984105-6-pedro@palves.net> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260525191829.984105-1-pedro@palves.net> References: <20260525191829.984105-1-pedro@palves.net> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces~public-inbox=simark.ca@sourceware.org On some targets, like Cygwin (see the following commit), extracting the exit code works differently depending on whether the inferior was run by GDB, or GDB attached to an existing process. Extend gdb.base/exitsignal.exp to test both scenarios. Note: We add a wait_for_gdb sleep loop to segv.c and normal.c so the attach path can synchronize with GDB before the inferior runs. For simplicity, the run path pokes the same variable from setup, so behavior there is unchanged. The testcase passes cleanly on Linux, native and gdbserver, and on MinGW too. Cygwin still requires more fixes. Change-Id: I9908aef0cea3eaec8134f05e347702baf753d10c commit-id: b74921d8 --- gdb/testsuite/gdb.base/exitsignal.exp | 86 +++++++++++++++++++++------ gdb/testsuite/gdb.base/normal.c | 7 +++ gdb/testsuite/gdb.base/segv.c | 7 +++ 3 files changed, 82 insertions(+), 18 deletions(-) diff --git a/gdb/testsuite/gdb.base/exitsignal.exp b/gdb/testsuite/gdb.base/exitsignal.exp index 7ae6e19b093..7684646b546 100644 --- a/gdb/testsuite/gdb.base/exitsignal.exp +++ b/gdb/testsuite/gdb.base/exitsignal.exp @@ -21,6 +21,10 @@ # killed by a signal. However, if it was killed by an uncaught # signal, then there is no way for it to have exited. +# On some targets, like Cygwin, extracting the exit code works +# differently depending on whether the inferior was run by GDB, or GDB +# attached to an existing process. We exercise both cases. + require {!target_info exists gdb,nosignals} set testfile "exitsignal" @@ -43,16 +47,48 @@ if { [build_executable "failed to build $exec2" ${exec2} "${srcfile2}" \ return -1 } -proc test_signal {} { +# Get the inferior under GDB's control in mode HOW ("run" or +# "attach"), using BINFILE. In "attach" mode, spawn the binary and +# attach to it; in "run" mode, run to main. In both modes, clear the +# inferior's wait_for_gdb flag so the spin loop in main exits when +# execution continues. + +proc setup {how binfile} { + if {$how == "run"} { + if { ![runto_main] } { + return + } + } else { + # Change directory so that the core file (for test_signal) + # doesn't end up outside the output directory. + with_cwd [standard_output_file {}] { + set ::test_spawn_id [spawn_wait_for_attach $binfile] + } + set test_pid [spawn_id_get_pid $::test_spawn_id] + gdb_test "attach $test_pid" "Attaching to program.*" \ + "attach to process" + } + + gdb_test "p wait_for_gdb = 0" " = 0" +} + +# Counterpart to setup. In "attach" mode, kill the spawned process +# started by setup. In "run" mode, nothing to do. + +proc teardown {how} { + if {$how == "attach"} { + kill_wait_spawned_process $::test_spawn_id + } +} + +proc test_signal {how} { clean_restart $::exec1 - # Run to main. But, before, change cwd to get the core into the - # output directory. + # Get the inferior under GDB's control. But, before, change cwd + # so the core file ends up in the output directory. set_inferior_cwd_to_output_dir - if { ![runto_main] } { - return - } + setup $how $::binfile1 # Get the inferior's PID for later. set pid [get_inferior_pid] @@ -98,8 +134,15 @@ proc test_signal {} { gdb_test "print \$_exitcode" " = void" \ "\$_exitcode is still void after SIGSEGV" - # Re-run to main, i.e., restart the executable. - rerun_to_main + # Start over. + if {$how == "run"} { + rerun_to_main + } else { + with_test_prefix "reattach" { + kill_wait_spawned_process $::test_spawn_id + setup $how $::binfile1 + } + } # Print the $_exitsignal again. Even in this normal scenario, it # should still contain the signal triggered in the other run. @@ -109,11 +152,13 @@ proc test_signal {} { # And, again, print $_exitcode. gdb_test "print \$_exitcode" " = void" \ "\$_exitcode is still void after restarting the inferior" + + teardown $how } # Test the behavior of $_exit{code,signal} during a normal inferior # execution. -proc test_normal {} { +proc test_normal {how} { clean_restart $::exec2 # Check $_exitsignal and $_exitcode, both should be void before @@ -124,10 +169,7 @@ proc test_normal {} { "\$_exitcode is void before normal inferior is executed" # Run the inferior until the end. - if { ![runto_main] } { - return - } - + setup $how $::binfile2 gdb_test "continue" " exited with code 01\\\].*" "continue to exit" # Check $_exitcode. It should be 1. @@ -138,11 +180,19 @@ proc test_normal {} { # has not received any signal. gdb_test "print \$_exitsignal" " = void" \ "\$_exitsignal is still void after normal inferior is executed" -} -with_test_prefix "signal" { - test_signal + teardown $how } -with_test_prefix "normal" { - test_normal + +foreach_with_prefix how {"run" "attach"} { + if {$how == "attach" && ![can_spawn_for_attach]} { + continue + } + + with_test_prefix "signal" { + test_signal $how + } + with_test_prefix "normal" { + test_normal $how + } } diff --git a/gdb/testsuite/gdb.base/normal.c b/gdb/testsuite/gdb.base/normal.c index ba04c861dad..fbe59653c97 100644 --- a/gdb/testsuite/gdb.base/normal.c +++ b/gdb/testsuite/gdb.base/normal.c @@ -17,9 +17,16 @@ /* This test is just a normal return 1. */ +#include + +volatile int wait_for_gdb = 1; + int main (int argc, char *argv[]) { + while (wait_for_gdb) + sleep (1); + /* Non-zero specifically, as it would be otherwise easier for a buggy GDB to report exit code 0 when it shouldn't and that wouldn't be noticed. */ diff --git a/gdb/testsuite/gdb.base/segv.c b/gdb/testsuite/gdb.base/segv.c index fd43d95e9b2..e1c295f50a5 100644 --- a/gdb/testsuite/gdb.base/segv.c +++ b/gdb/testsuite/gdb.base/segv.c @@ -17,9 +17,16 @@ /* This test can be used just to generate a SIGSEGV. */ +#include + +volatile int wait_for_gdb = 1; + int main (int argc, char *argv[]) { + while (wait_for_gdb) + sleep (1); + /* Generating a SIGSEGV. */ *(volatile int *) 0; -- 2.53.0