gdb/testsuite: fix race in gdb.base/async-shell.exp
authorSimon Marchi <simon.marchi@polymtl.ca>
Mon, 10 Oct 2022 01:21:53 +0000 (21:21 -0400)
committerSimon Marchi <simon.marchi@polymtl.ca>
Tue, 11 Oct 2022 00:45:56 +0000 (20:45 -0400)
commitf6c874187a5a8b6f7108fff3b668395de45f9904
treeeba0324cacebbab2f9f0c3f676a514f0bebba556
parent4a5d6345a0bb18f8688540025036647509f56f81
gdb/testsuite: fix race in gdb.base/async-shell.exp

I see some random failures in this test:

    FAIL: gdb.base/async-shell.exp: run & (timeout)

It can be reliably reproduced on a recent enough GNU/Linux with this
change:

    diff --git a/gdb/testsuite/lib/gdb.exp b/gdb/testsuite/lib/gdb.exp
    index 44cc28b30051..2a3c8253ba5a 100644
    --- a/gdb/testsuite/lib/gdb.exp
    +++ b/gdb/testsuite/lib/gdb.exp
    @@ -1301,6 +1301,7 @@ proc gdb_test_multiple { command message args } {
         }
         set gdb_test_name "$message"

    +    sleep 2
         set result 0
         set code [catch {gdb_expect $code} string]

"recent enough" means a system where libpthread.so was merged with
libc.so, so at least glibc 2.34.

The problem is that the `run &` command prints some things after the
prompt:

    (gdb) [Thread debugging using libthread_db enabled]
    Using host libthread_db library "/usr/lib/../lib/libthread_db.so.1".

If expect is quick enough, it will consume only up to the prompt.  But
if it is slow enough, it will consume those messages at the same time as
the prompt, in which case the gdb_test used for "run &" won't match.  By
default, the prompt used by gdb_test uses a `$` to anchor the match at
the end of the buffer.  If there's anything following the prompt, it
won't match.

The diff above adds a delay between sending the command and consuming
the output, giving GDB more time to output the messages, giving a good
chance that expect consumes them at the same time as the prompt.

This is normally handled by using gdb_test_multiple and specifying a
pattern that ends with "$gdb_prompt", but not a trailing $.  I think
this is common enough that it deserves its own gdb_test option.
Therefore, add the -no-anchor-prompt option to gdb_test, and
gdb_test_no_output for completeness.  Use it in
gdb.base/async-shell.exp.

Change-Id: I9051d8800d1c10a2e95db1a575991f7723492f1b
Approved-By: Tom de Vries <tdevries@suse.de>
gdb/testsuite/gdb.base/async-shell.exp
gdb/testsuite/lib/gdb.exp