From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wr1-f51.google.com (mail-wr1-f51.google.com [209.85.221.51]) by sourceware.org (Postfix) with ESMTPS id 7FD6E3858402 for ; Tue, 21 Nov 2023 15:05:55 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 7FD6E3858402 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 7FD6E3858402 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=209.85.221.51 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1700579157; cv=none; b=FrAaphRSsoYCbII3UH9fag0Ge528CY7SwEqJcNOHeNJSUlWvqiTI4GOkwY4QVsYtvjDp6ZNKfYxJZfS1mv93qKMCudNSv+kXF9REFh2o5comdCpv8VMWhm5HNKXbzyPiRvWIjJpOKDoroXN3YJ2MRBVUga/GQFqEhwlDQ/aZjSU= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1700579157; c=relaxed/simple; bh=oil9oW7yKArsKjI1h1ArENWbQNN2OVKotO3VZjupkqw=; h=From:To:Subject:Date:Message-ID:MIME-Version; b=Hi+BO6QbZrXGan2GAkSA5CIXb8256kyp+lJP4BmPwUOn7S300CzAwkLAl3EUQbO1zU24szJ4jeYeu9Ol1/o21noLN6M6ZyPkb/fhSvAOAPJnoTacGe1pyFRjCASg6QRF9+I7PLgRLeSd1LQoAacLwdHqaUv+ZpPTD0axjE2bKiE= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-wr1-f51.google.com with SMTP id ffacd0b85a97d-32d895584f1so4004514f8f.1 for ; Tue, 21 Nov 2023 07:05:55 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700579154; x=1701183954; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=CoQdeIRsD8bn1jJvTWiukg0EwpL0PuXBCZjelenZLXM=; b=WAZtS5X/hxRc3rSmR0NlHfbfQKZEcEzYRux24+VOOBR62pPlsgTGz4J+DrfzknuGbm NLmp9Au73RbAEr6tKz68qcn7sWFq/uOx7bCSbRjy+JXdKkpRWKgi3LU6egzhkyPuLGTw tfb6APwsmoXS9yoPR/y/Xfmh4QswPXz/ZX/G6H3KLJGTC9poChmNWw+UV/fHXcU1kq0+ A6rExCMjxjud3Q6M3q6v4MVGe8o3wXHOpgm7jXei6J0PDO7YsA5FkDOgdtymOWLUuia0 M/FCyBwyvLNhqMCeG1+aHlJetRIYOIZbQo4FTNV9gBdhrfTi5kpVLjHz+xmQ4i+tHdCv uCWg== X-Gm-Message-State: AOJu0YwfgUjjkUviNFV60gNzpHOJg1gxrjzxX6hjJ6m+aEKyrGk9oBny 3hXmWgMvzljjr+hs+tS5ftcjPfRYvM7pVw== X-Google-Smtp-Source: AGHT+IFiANgQJcVcFbIsBuFPAbbn7qOTTZE0czdEYgTiR+qvzwxNPLlzLZCbsZNH/YhCaMgWXQu1vA== X-Received: by 2002:a5d:47af:0:b0:332:cae7:2ed4 with SMTP id 15-20020a5d47af000000b00332cae72ed4mr3574634wrb.29.1700579153587; Tue, 21 Nov 2023 07:05:53 -0800 (PST) Received: from localhost ([2001:8a0:f91e:1a00:9b98:76ec:b9b9:e6f5]) by smtp.gmail.com with UTF8SMTPSA id h1-20020a05600004c100b00318147fd2d3sm14570677wri.41.2023.11.21.07.05.52 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 21 Nov 2023 07:05:53 -0800 (PST) From: Pedro Alves To: gdb-patches@sourceware.org Subject: [PATCH 1/2] gdb.threads/step-over-thread-exit.exp improvements Date: Tue, 21 Nov 2023 15:05:44 +0000 Message-ID: <20231121150545.778892-2-pedro@palves.net> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20231121150545.778892-1-pedro@palves.net> References: <20231121150545.778892-1-pedro@palves.net> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-9.9 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.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: This commit makes the following improvements to gdb.threads/step-over-thread-exit.exp: - Add a third axis to stepping over the breakpoint with displaced vs inline stepping -- also test with no breakpoint at all. - Check that when GDB reports "Command aborted, thread exited.", the selected thread is the thread that exited. This is always true currently on GNU/Linux by coincidence, but a similar testcase on AMD GPU exposed a problem here. Better make the testcase catch any potential regression. - Fixes a race that Simon ran into with GDBserver testing. (gdb) next [New Thread 2143071.2143438] Thread 3 "step-over-threa" hit Breakpoint 2, 0x000055555555524e in my_exit_syscall () at .../testsuite/lib/my-syscalls.S:74 74 SYSCALL (my_exit, __NR_exit) (gdb) FAIL: gdb.threads/step-over-thread-exit.exp: displaced-stepping=auto: non-stop=on: target-non-stop=on: schedlock=off: cmd=next: ns_stop_all=0: command aborts when thread exits I was not able to reproduce it, but I believe that what happens is the following: Once we continue, the thread 2 exits, and the main thread thus unblocks from its pthread_join, and spawns a new thread. That new thread may hit the breakpoint at my_exit_syscall very quickly. GDB could then see/process that breakpoint event before the thread exit event for the thread we care about, which would result in the failure seen above. The fix here is to not loop and start a new thread at all in the scenario where the race can happen. We only need to loop and spawn new threads when testing with "cmd=continue" and schedlock off, in which case GDB doesn't abort the command when the thread exits. Change-Id: I90c95c32f00630a3f682b1541c23aff52451f9b6 --- .../gdb.threads/step-over-thread-exit.c | 16 ++- .../gdb.threads/step-over-thread-exit.exp | 107 +++++++++++++++--- 2 files changed, 102 insertions(+), 21 deletions(-) diff --git a/gdb/testsuite/gdb.threads/step-over-thread-exit.c b/gdb/testsuite/gdb.threads/step-over-thread-exit.c index 878e5924c5c..218f003b205 100644 --- a/gdb/testsuite/gdb.threads/step-over-thread-exit.c +++ b/gdb/testsuite/gdb.threads/step-over-thread-exit.c @@ -18,6 +18,7 @@ #include #include #include +#include #include "../lib/my-syscalls.h" static void * @@ -30,13 +31,19 @@ thread_func (void *arg) abort (); } +/* Number of threads we'll create. */ +int n_threads = 100; + int -main (void) +main (int argc, char **argv) { int i; - /* Spawn and join a thread, 100 times. */ - for (i = 0; i < 100; i++) + if (argc > 1) + n_threads = atoi (argv[1]); + + /* Spawn and join a thread, N_THREADS times. */ + for (i = 0; i < n_threads; i++) { pthread_t thread; int ret; @@ -48,5 +55,8 @@ main (void) assert (ret == 0); } + /* Some time to make sure that GDB processes the thread exit event + before the whole-process exit. */ + sleep (3); return 0; } diff --git a/gdb/testsuite/gdb.threads/step-over-thread-exit.exp b/gdb/testsuite/gdb.threads/step-over-thread-exit.exp index 615bd838763..366e9249357 100644 --- a/gdb/testsuite/gdb.threads/step-over-thread-exit.exp +++ b/gdb/testsuite/gdb.threads/step-over-thread-exit.exp @@ -25,11 +25,28 @@ if { [build_executable "failed to prepare" $testfile \ return } -# Each argument is a different testing axis, most of them obvious. +# Test stepping/continuing at an exit syscall instruction. +# +# Each argument is a different testing axis. +# +# STEP_OVER_MODE can be one of: +# +# - none: don't put a breakpoint on the s_endpgm instruction. +# +# - inline: put a breakpoint on the s_endpgm instruction, and use +# in-line stepping to step over it (disable displaced-stepping). +# +# - displaced: same, but use displaced stepping. +# +# SCHEDLOCK can be "on" or "off". +# +# CMD is the GDB command to run when at the exit syscall instruction. +# # NS_STOP_ALL is only used if testing "set non-stop on", and indicates # whether to have GDB explicitly stop all threads before continuing to # thread exit. -proc test {displaced-stepping non-stop target-non-stop schedlock cmd ns_stop_all} { +# +proc test {step_over_mode non-stop target-non-stop schedlock cmd ns_stop_all} { if {${non-stop} == "off" && $ns_stop_all} { error "invalid arguments" } @@ -40,19 +57,24 @@ proc test {displaced-stepping non-stop target-non-stop schedlock cmd ns_stop_all clean_restart $::binfile } - gdb_test_no_output "set displaced-stepping ${displaced-stepping}" - - if { ![runto_main] } { - return + if { $step_over_mode == "none" } { + # Nothing to do. + } elseif { $step_over_mode == "inline" } { + gdb_test_no_output "set displaced-stepping off" + } elseif { $step_over_mode == "displaced" } { + gdb_test_no_output "set displaced-stepping on" + } else { + error "Invalid step_over_mode value: $step_over_mode" } - gdb_breakpoint "my_exit_syscall" - if {$schedlock || (${non-stop} == "on" && $ns_stop_all)} { - gdb_test "continue" \ - "Thread 2 .*hit Breakpoint $::decimal.* my_exit_syscall .*" \ - "continue until syscall" + + gdb_test_no_output "set args 1" + + if { ![runto my_exit_syscall] } { + return + } if {${non-stop} == "on"} { # The test only spawns one thread at a time, so this just @@ -72,6 +94,13 @@ proc test {displaced-stepping non-stop target-non-stop schedlock cmd ns_stop_all gdb_test_no_output "set scheduler-locking ${schedlock}" + # If testing a step-over is requested, leave the breakpoint at + # the current instruction to force a step-over; otherwise, + # remove it. + if { $step_over_mode == "none" } { + delete_breakpoints + } + if {$cmd == "continue"} { gdb_test "continue" \ "No unwaited-for children left." \ @@ -84,9 +113,23 @@ proc test {displaced-stepping non-stop target-non-stop schedlock cmd ns_stop_all } } } else { + if {$cmd != "continue" || $step_over_mode == "none"} { + set n_threads 1 + } else { + set n_threads 100 + } + + gdb_test_no_output "set args $n_threads" + + if { ![runto_main] } { + return + } + + gdb_breakpoint "my_exit_syscall" + gdb_test_no_output "set scheduler-locking ${schedlock}" - if {$cmd != "continue"} { + if {$cmd != "continue" || $step_over_mode == "none"} { set thread "" gdb_test_multiple "continue" "" { -re -wrap "Thread ($::decimal) .*hit Breakpoint $::decimal.* my_exit_syscall .*" { @@ -98,12 +141,40 @@ proc test {displaced-stepping non-stop target-non-stop schedlock cmd ns_stop_all "switch to event thread" } - gdb_test_multiple $cmd "command aborts when thread exits" { - -re "Command aborted, thread exited\\.\r\n$::gdb_prompt " { - pass $gdb_test_name + # If testing a step-over is requested, leave the breakpoint at + # the current instruction to force a step-over; otherwise, + # remove it. + if { $step_over_mode == "none" } { + delete_breakpoints + } + + if {$cmd == "continue"} { + gdb_continue_to_end "continue to end" "continue" 1 + } else { + gdb_test_multiple $cmd "command aborts when thread exits" { + -re "Command aborted, thread exited\\.\r\n$::gdb_prompt " { + pass $gdb_test_name + } + } + + set cur_thr_after "" + gdb_test_multiple "print /d \$_thread" "get selected thread after" { + -re -wrap "$::decimal = ($::decimal)" { + set cur_thr_after $expect_out(1,string) + pass "$gdb_test_name" + } } + + gdb_assert {$thread == $cur_thr_after} \ + "selected thread didn't change" } } else { + if {$step_over_mode == "none"} { + # If we removed the breakpoint at my_exit_syscall, the + # loopping wouldn't work. This case is instead + # handled in the then/else branch above. + error "unexpected \$step_over_mode==none" + } for { set i 0 } { $i < 100 } { incr i } { with_test_prefix "iter $i" { set ok 0 @@ -130,7 +201,7 @@ proc test {displaced-stepping non-stop target-non-stop schedlock cmd ns_stop_all } } -foreach_with_prefix displaced-stepping {off auto} { +foreach_with_prefix step_over_mode {none inline displaced} { foreach_with_prefix non-stop {off on} { foreach_with_prefix target-non-stop {off on} { if {${non-stop} == "on" && ${target-non-stop} == "off"} { @@ -142,11 +213,11 @@ foreach_with_prefix displaced-stepping {off auto} { foreach_with_prefix cmd {"next" "continue"} { if {${non-stop} == "on"} { foreach_with_prefix ns_stop_all {0 1} { - test ${displaced-stepping} ${non-stop} ${target-non-stop} \ + test ${step_over_mode} ${non-stop} ${target-non-stop} \ ${schedlock} ${cmd} ${ns_stop_all} } } else { - test ${displaced-stepping} ${non-stop} ${target-non-stop} ${schedlock} ${cmd} 0 + test ${step_over_mode} ${non-stop} ${target-non-stop} ${schedlock} ${cmd} 0 } } } -- 2.42.0