[pushed,gdb/testsuite] Fix race in gdb.base/add-symbol-file-attach.exp

Message ID 20230904115449.18679-1-tdevries@suse.de
State Committed
Headers
Series [pushed,gdb/testsuite] Fix race in gdb.base/add-symbol-file-attach.exp |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gdb_build--master-aarch64 warning Patch is already merged
linaro-tcwg-bot/tcwg_gdb_build--master-arm warning Patch is already merged
linaro-tcwg-bot/tcwg_gdb_check--master-aarch64 warning Patch is already merged
linaro-tcwg-bot/tcwg_gdb_check--master-arm warning Patch is already merged

Commit Message

Tom de Vries Sept. 4, 2023, 11:54 a.m. UTC
  When running test-case gdb.base/add-symbol-file-attach.exp with target board
unix/-m32, we run into:
...
(gdb) attach 3955^M
Attaching to process 3955^M
Load new symbol table from "add-symbol-file-attach"? (y or n) y^M
Reading symbols from add-symbol-file-attach/add-symbol-file-attach...^M
Reading symbols from /lib/libm.so.6...^M
Reading symbols from /usr/lib/debug/lib/libm-2.31.so-i386.debug...^M
Reading symbols from /lib/libc.so.6...^M
Reading symbols from /usr/lib/debug/lib/libc-2.31.so-i386.debug...^M
Reading symbols from /lib/ld-linux.so.2...^M
Reading symbols from /usr/lib/debug/lib/ld-2.31.so-i386.debug...^M
0xf7f53549 in __kernel_vsyscall ()^M
(gdb) FAIL: gdb.base/add-symbol-file-attach.exp: attach
...

The test fails because this regexp is used:
...
    -re ".*in \[_A-Za-z0-9\]*pause.*$gdb_prompt $" {
...

The regexp attempts to detect that the exec is somewhere in pause ():
...
int
main (int argc, char **argv)
{
  pause ();
  return 0;
}
...
but when the exec is blocked in pause, the backtrace is:
...
 (gdb) bt
 #0  0xf7fd2549 in __kernel_vsyscall ()
 #1  0xf7d84966 in __libc_pause () at ../sysdeps/unix/sysv/linux/pause.c:29
 #2  0x0804844c in main (argc=1, argv=0xffffce84)
     at /data/vries/gdb/src/gdb/testsuite/gdb.base/add-symbol-file-attach.c:26
...

We could simply extend the regexp to also match __kernel_vsyscall, but the
more fundamental problem is that the test is racy.

The attach can happen before the exec is blocked in pause (), somewhere in the
dynamic linker resolving the call to pause, in main or even earlier.

Note that for the test-case to be effective, the exec is not required to be in
pause ().  I added a "while (1);" loop at the start of main, reverted the
patch fixing the corresponding PR and reproduced the problem it's supposed to
detect.

Fix this by simply matching the "Reading symbols from" line, similar to what
an earlier test is doing.

While we're at it, rewrite the earlier test to also use the -wrap idiom.

Tested on x86_64-linux.
---
 gdb/testsuite/gdb.base/add-symbol-file-attach.exp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)


base-commit: 0f020d9cedc947c09717984e0182828eb5f81208
  

Patch

diff --git a/gdb/testsuite/gdb.base/add-symbol-file-attach.exp b/gdb/testsuite/gdb.base/add-symbol-file-attach.exp
index 373f3c400f7..bbdfd994d49 100644
--- a/gdb/testsuite/gdb.base/add-symbol-file-attach.exp
+++ b/gdb/testsuite/gdb.base/add-symbol-file-attach.exp
@@ -57,7 +57,7 @@  gdb_test_multiple "add-symbol-file $binfile" $test {
 	send_gdb "y\n"
 	exp_continue
     }
-    -re "Reading symbols from.*$gdb_prompt $" {
+    -re -wrap "Reading symbols from .*" {
 	pass $test
     }
 }
@@ -71,7 +71,7 @@  gdb_test_multiple "attach $testpid" $test {
 	send_gdb "y\n"
 	exp_continue
     }
-    -re ".*in \[_A-Za-z0-9\]*pause.*$gdb_prompt $" {
+    -re -wrap "Reading symbols from .*" {
 	pass $test
     }
 }