]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commit
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)
commitc809e3a03b7192ded8b3dbbea0585a754c46c669
tree54ce71154cfe625110058ae92c0dc4a21c1f4b75
parent0723ae1439c887887a860a12c562f78a9bd54b69
gdb_test_multiple: Anchor prompt match if -lbl

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