Re: [PATCH v2 42/47] gdb_test_multiple: Anchor prompt match if -lbl

Tom de Vries
2025-05-21T15:20:11Z

[-- Attachment #1: Type: text/plain, Size: 1944 bytes --]

On 5/19/25 15:23, Pedro Alves wrote:
> I wrote a test like this:
>
> gdb_test_multiple "command" "" -lbl {
> -re "^\r\nprefix foo(?=\r\n)" {
> exp_continue
> }
> -re "^\r\nprefix bar(?=\r\n)" {
> exp_continue
> }
> -re "^\r\prefix (?=\r\n)" {

Hi Pedro,

Missing n in "\r\n".

> exp_continue
> }
> -re "^\r\n$::gdb_prompt $" {
> pass $gdb_test_name
> }
> }
>
> Anchors are needed in my case to avoid too-eager matching due to the
> common prefix.
I'm not saying you don't need anchors, maybe you do or maybe you don't,
but I don't understand how a common prefix is a reason to need one.

Also, if you need anchors, you need to match the command as well, so you
need a -re "^command(?=\r\n)" clause.

> The intent is for the prompt match above to override the built-in
> prompt match. However, it doesn't and the test fails with output like
> this:
>
> (gdb) command
> prefix foo
> prefix bar
> meant-to-be-matched-by-lbl
> (gdb)
>
> That's because the built-in match for the prompt matches before the
> -lbl pattern for this expect buffer:
>
> \r\nmeant-to-be-matched-by-lbl\r\n(gdb)
>

You can fix that by dropping the anchor and using "\r\n$::gdb_prompt $"
instead. Even more simple, use -wrap "".

> This this by anchoring the built-in prompt match if -lbl was
> requested.
>

This might be a good idea indeed.

FWIW, the intent of -lbl was to handle except buffers overflows, so the
-lbl pattern is meant to match lines in absence of a prompt.

But AFAIU, your patch will make matching more predictable, so that's a
good thing.

I created a mockup test-case to emulate the case you describe above.
I've left the anchoring in place for the prefix lines, but for the gdb
output printed by gdb.sh, it doesn't look necessary. [ The test-case has
hardcoded paths in it. ]

Thanks,
- Tom

[-- Attachment #2: 0001-try.patch --]
[-- Type: text/x-patch, Size: 2376 bytes --]

From 91363128b5918a16a8426ca31b43927004d1535d Mon Sep 17 00:00:00 2001
From: Tom de Vries <tdevries@suse.de>
Date: Wed, 21 May 2025 17:13:55 +0200
Subject: [PATCH] try

---
.../gdb.testsuite/gdb-test-multiple.exp | 25 ++++++++++
gdb/testsuite/gdb.testsuite/gdb.sh | 47 +++++++++++++++++++
2 files changed, 72 insertions(+)
create mode 100644 gdb/testsuite/gdb.testsuite/gdb-test-multiple.exp
create mode 100755 gdb/testsuite/gdb.testsuite/gdb.sh

diff --git a/gdb/testsuite/gdb.testsuite/gdb-test-multiple.exp b/gdb/testsuite/gdb.testsuite/gdb-test-multiple.exp
new file mode 100644
index 00000000000..ac0674ef20f
--- /dev/null
+++ b/gdb/testsuite/gdb.testsuite/gdb-test-multiple.exp
@@ -0,0 +1,25 @@
+set GDB $srcdir/$subdir/gdb.sh
+clean_restart
+
+gdb_test_multiple "command" "" -lbl {
+ -re "^command(?=\r\n)" {
+ verbose -log <COMMAND>
+ exp_continue
+ }
+ -re "^\r\nprefix foo(?=\r\n)" {
+ verbose -log <PREFIX-FOO>
+ exp_continue
+ }
+ -re "^\r\nprefix bar(?=\r\n)" {
+ verbose -log <PREFIX-BAR>
+ exp_continue
+ }
+ -re "^\r\nprefix (?=\r\n)" {
+ verbose -log <PREFIX>
+ exp_continue
+ }
+ -re -wrap "" {
+ verbose -log "<PROMPT>"
+ pass $gdb_test_name
+ }
+}
diff --git a/gdb/testsuite/gdb.testsuite/gdb.sh b/gdb/testsuite/gdb.testsuite/gdb.sh
new file mode 100755
index 00000000000..9c358f1a204
--- /dev/null
+++ b/gdb/testsuite/gdb.testsuite/gdb.sh
@@ -0,0 +1,47 @@
+#!/bin/sh
+
+while [ $# -gt 0 ]; do
+ case $1 in
+ --version)
+ echo "gdb.sh"
+ exit 0
+ ;;
+ *)
+ ;;
+ esac
+
+ shift
+done
+
+while true; do
+ echo -n "(gdb) "
+ read line
+
+ if [ "$line" = "set height 0" ]; then
+ continue
+ fi
+
+ if [ "$line" = "set width 0" ]; then
+ continue
+ fi
+
+ if [ "$line" = "dir" ]; then
+ echo -n "Reinitialize source path to empty? (y or n) "
+ read response
+ echo 'Source directories searched: $cdir:$cwd'
+ continue
+ fi
+
+ if [ "$line" = "dir /data/vries/gdb/src/gdb/testsuite/gdb.testsuite" ]; then
+ echo "Source directories searched: /data/vries/gdb/src/gdb/testsuite/gdb.testsuite:$cdir:$cwd"
+ continue
+ fi
+
+ if [ "$line" = "command" ]; then
+ echo "prefix foo"
+ echo "prefix bar"
+ for n in $(seq 1 1); do
+ echo "meant-to-be-matched-by-lbl-$n"
+ done
+ fi
+done

base-commit: 14dd98b0f7f04d25bc7bc63de3ab958bdd51641d
--
2.43.0