Bug 32822 - [gdb/testsuite] FAIL: gdb.threads/access-mem-running-thread-exit.exp: all-stop/non-stop: access mem (print global_var after writing, inf=2, iter=1)
Summary: [gdb/testsuite] FAIL: gdb.threads/access-mem-running-thread-exit.exp: all-sto...
Status: RESOLVED FIXED
Alias: None
Product: gdb
Classification: Unclassified
Component: testsuite (show other bugs)
Version: 15.1
: P2 normal
Target Milestone: 17.1
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2025-03-27 11:48 UTC by Tom de Vries
Modified: 2025-03-27 12:20 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Tom de Vries 2025-03-27 11:48:43 UTC
In OBS, with a 15.2 based gdb package, occasionally I run into:
...
(gdb) inferior 2
[Switching to inferior 2 [process 31372] (access-mem-running-thread-exit)]
[Switching to thread 2.1 (Thread 0xf7db9700 (LWP 31372))](running)
(gdb) print global_var = 555
$1 = 555
(gdb) print global_var
$2 = 556
(gdb) FAIL: $exp: all-stop: access mem (print global_var after writing, inf=2, iter=1)
...

I managed to reproduce this on current trunk using:
...
diff --git a/gdb/testsuite/gdb.threads/access-mem-running-thread-exit.c b/gdb/testsuite/gdb.threads/ac
cess-mem-running-thread-exit.c
index af05b13c763..d67fe7fec02 100644
--- a/gdb/testsuite/gdb.threads/access-mem-running-thread-exit.c
+++ b/gdb/testsuite/gdb.threads/access-mem-running-thread-exit.c
@@ -102,6 +102,7 @@ main (void)
 {
   int i;
 
+  sleep (1);
   global_var++;
 
   for (i = 0; i < 4; i++)
diff --git a/gdb/testsuite/gdb.threads/access-mem-running-thread-exit.exp b/gdb/testsuite/gdb.threads/
access-mem-running-thread-exit.exp
index 784f17ff3b2..23440a944ee 100644
--- a/gdb/testsuite/gdb.threads/access-mem-running-thread-exit.exp
+++ b/gdb/testsuite/gdb.threads/access-mem-running-thread-exit.exp
@@ -168,6 +168,7 @@ proc test { non_stop } {
 
 	my_gdb_test "print global_var = 555" " = 555" \
 	    "write to global_var"
+	sleep 1
 	my_gdb_test "print global_var" " = 555" \
 	    "print global_var after writing"
 	my_gdb_test "print global_var = 333" " = 333" \
...
giving me:
...
FAIL: gdb.threads/access-mem-running-thread-exit.exp: all-stop: access mem (print global_var after writing, inf=2, iter=1)
FAIL: gdb.threads/access-mem-running-thread-exit.exp: non-stop: access mem (print global_var after writing, inf=2, iter=1)
...
Comment 1 Sourceware Commits 2025-03-27 12:19:05 UTC
The master branch has been updated by Tom de Vries <vries@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=4bf692ec70394f1157371f21092eeb1c3e9181bb

commit 4bf692ec70394f1157371f21092eeb1c3e9181bb
Author: Tom de Vries <tdevries@suse.de>
Date:   Thu Mar 27 13:18:57 2025 +0100

    [gdb/testsuite] Fix gdb.threads/access-mem-running-thread-exit.exp
    
    In OBS (Open Build Service), with a 15.2 based gdb package, occasionally I run
    into:
    ...
    (gdb) inferior 2
    [Switching to inferior 2 [process 31372] (access-mem-running-thread-exit)]
    [Switching to thread 2.1 (Thread 0xf7db9700 (LWP 31372))](running)
    (gdb) print global_var = 555
    $1 = 555
    (gdb) print global_var
    $2 = 556
    (gdb) FAIL: $exp: all-stop: access mem \
      (print global_var after writing, inf=2, iter=1)
    ...
    
    I managed to reproduce this on current trunk using a reproducer patch (posted
    in the PR).
    
    The problem is due to commit 31c21e2c13d ("[gdb/testsuite] Fix
    gdb.threads/access-mem-running-thread-exit.exp with clang"), which introduced
    an increment of global_var at the start of main.
    
    This created a race between:
    - gdb modifying global_var, and
    - the inferior modifying global_var.
    
    Fix this by:
    - adding a new empty function setup_done,
    - adding a call to setup_done after the increment of global_var, and
    - rather than running to main, running to setup_done.
    
    Tested on x86_64-linux.
    
    PR testsuite/32822
    Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=32822
Comment 2 Tom de Vries 2025-03-27 12:20:44 UTC
Fixed.