]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
gdb_test_multiple: Anchor prompt match if -lbl
authorPedro Alves <pedro@palves.net>
Thu, 5 Jun 2025 17:09:44 +0000 (18:09 +0100)
committerPedro Alves <pedro@palves.net>
Fri, 6 Jun 2025 13:50:51 +0000 (14:50 +0100)
The testcase added by this patch has a gdb_test_multiple call that
wants to match different lines of output that all have a common
prefix, and do different actions on each.  Instead of a single regular
expression with alternatives, it's clearer code if the different
expressions are handled with different "-re", like so:

  gdb_test_multiple "command" "" -lbl {
     -re "^command(?=\r\n)" {
 exp_continue
     }
     -re "^\r\nprefix foo(?=\r\n)" {
 # Some action for "foo".
 exp_continue
     }
     -re "^\r\nprefix bar(?=\r\n)" {
 # Some action for "bar".
 exp_continue
     }
     -re "^\r\nprefix \[^\r\n\]*(?=\r\n)" {
 # Some action for all others.
 exp_continue
     }
     -re "^\r\n$::gdb_prompt $" {
 gdb_assert {$all_prefixes_were_seen} $gdb_test_name
     }
  }

Above, the leading anchors in the "^\r\nprefix..." matches are needed
to avoid too-eager matching due to the common prefix.  Without the
anchors, if the expect output buffer happens to contain at least:

  "\r\nprefix xxx\r\nprefix foo\r\n"

... then the "prefix foo" pattern match inadvertently consumes the
first "prefix xxx" line.

Without the anchor in the prompt match, like:

  -re "\r\n$::gdb_prompt $" {
      gdb_assert {$all_prefixes_were_seen} $gdb_test_name
  }

Or the equivalent:

  -re -wrap "" {
      gdb_assert {$all_prefixes_were_seen} $gdb_test_name
  }

... then if the expect buffer contains:

  "\r\nmeant-to-be-matched-by-lbl\r\nprefix foo\r\n$gdb_prompt "

... then the prompt regexp matches this, consuming the "prefix" line
inadvertently, and we get a FAIL.  The built-in regexp matcher for
-lbl doesn't get a chance to match the
"\r\nmeant-to-be-matched-by-lbl\r\n" part, because the built-in prompt
match appears first within gdb_test_multiple.

By adding the anchor to the prompt regexp, we avoid that problem.

However, the same expect output buffer contents will still match the
built-in prompt regexp.  That is what is fixed by this patch.  It
makes it so that if -lbl is specified, the built-in prompt regexp has
a leading anchor.

Original idea for turning this into a gdb.testsuite/ testcase by Tom
de Vries <tdevries@suse.de>.

Approved-By: Tom de Vries <tdevries@suse.de>
Change-Id: Ic2571ec793d856a89ee0d533ec363e2ac6036ea2

gdb/testsuite/gdb.testsuite/gdb_test_multiple-lbl.exp [new file with mode: 0644]
gdb/testsuite/gdb.testsuite/gdb_test_multiple-lbl.gdb [new file with mode: 0755]
gdb/testsuite/lib/gdb.exp

diff --git a/gdb/testsuite/gdb.testsuite/gdb_test_multiple-lbl.exp b/gdb/testsuite/gdb.testsuite/gdb_test_multiple-lbl.exp
new file mode 100644 (file)
index 0000000..a05ce61
--- /dev/null
@@ -0,0 +1,84 @@
+# Copyright 2025 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+# Test gdb_test_multiple -lbl, particularly with patterns that share a
+# common prefix.
+
+standard_testfile
+
+clean_restart
+
+gdb_test_no_output "source ${srcdir}/${subdir}/$testfile.gdb" \
+    "source gdb test script"
+
+set saw_prompt 0
+set saw_prefix 0
+set saw_command 0
+set saw_prefix_foo 0
+set saw_prefix_bar 0
+
+# #1 - We need anchors so that the "prefix foo" pattern below does not
+# match when the expect output buffer contains:
+#
+#  "\r\nprefix xxx\r\n\prefix foo\r\n"
+#
+# #2 - We need an anchor on the prompt match as otherwise the prompt
+# regexp would match:
+#
+#  "\r\nmeant-to-be-matched-by-lbl-2\r\nprefix xxx\r\n(gdb) "
+#
+# This test would fail if -lbl did not force the built-in prompt match
+# regexp to have an anchor as well, as without it, the built-in prompt
+# regexp would have the exact same issue as #2 above.
+
+gdb_test_multiple "command" "" -lbl {
+    -re "^command(?=\r\n)" {
+       verbose -log <COMMAND>
+       incr saw_command
+       exp_continue
+    }
+    -re "^\r\nprefix foo(?=\r\n)" {
+       verbose -log <PREFIX-FOO>
+       incr saw_prefix_foo
+       exp_continue
+    }
+    -re "^\r\nprefix bar(?=\r\n)" {
+       verbose -log <PREFIX-BAR>
+       incr saw_prefix_bar
+       exp_continue
+    }
+    -re "^\r\nprefix \[^\r\n\]*(?=\r\n)" {
+       verbose -log <PREFIX>
+       incr saw_prefix
+       exp_continue
+    }
+    -re "^\r\n$gdb_prompt $" {
+       verbose -log <PROMPT>
+       incr saw_prompt
+       pass $gdb_test_name
+    }
+}
+
+verbose -log "saw_command: $saw_command"
+verbose -log "saw_prefix_foo: $saw_prefix_foo"
+verbose -log "saw_prefix_bar: $saw_prefix_bar"
+verbose -log "saw_prefix: $saw_prefix"
+verbose -log "saw_prompt: $saw_prompt"
+
+gdb_assert {$saw_command == 1}
+gdb_assert {$saw_prefix_foo == 1}
+gdb_assert {$saw_prefix_bar == 1}
+gdb_assert {$saw_prefix == 3}
+gdb_assert {$saw_prompt == 1}
diff --git a/gdb/testsuite/gdb.testsuite/gdb_test_multiple-lbl.gdb b/gdb/testsuite/gdb.testsuite/gdb_test_multiple-lbl.gdb
new file mode 100755 (executable)
index 0000000..8c94dfa
--- /dev/null
@@ -0,0 +1,25 @@
+# Copyright 2025 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+define command
+  echo prefix xxx\n
+  echo meant-to-be-matched-by-lbl-1\n
+  echo prefix foo\n
+  echo prefix bar\n
+  echo meant-to-be-matched-by-lbl-2\n
+  echo prefix xxx\n
+  echo prefix xxx\n
+  echo meant-to-be-matched-by-lbl-3\n
+end
index 0caa9cccd803139cd7d29272a06a8e7426e2b0aa..eefb6382a27d4a6edf7959efe62c134be4cbc885 100644 (file)
@@ -1127,6 +1127,7 @@ proc gdb_test_multiple { command message args } {
     global any_spawn_id
 
     set line_by_line 0
+    set lbl_anchor_re ""
     set prompt_regexp ""
     set prompt_anchor 1
     for {set i 0} {$i < [llength $args]} {incr i} {
@@ -1136,6 +1137,7 @@ proc gdb_test_multiple { command message args } {
            set prompt_regexp [lindex $args $i]
        } elseif { $arg == "-lbl" } {
            set line_by_line 1
+           set lbl_anchor_re "^"
        } elseif { $arg == "-no-prompt-anchor" } {
            set prompt_anchor 0
        } else {
@@ -1394,7 +1396,7 @@ proc gdb_test_multiple { command message args } {
            fail "$errmsg"
            set result -1
        }
-       -re "\r\n$prompt_regexp" {
+       -re "${lbl_anchor_re}\r\n$prompt_regexp" {
            if {![string match "" $message]} {
                fail "$message"
            }