[PATCH] gdb/testsuite: Add gdb_test_multiple_name variable

Andrew Burgess
2019-10-01T17:29:01Z

This commit adds a new feature to gdb_test_multiple, an automatically
created variable gdb_test_multiple_name. The idea is to make it
easier to write tests using gdb_test_multiple, and avoid places where
the string passed to pass/fail within an action element is different
to the message passed to the top level gdb_test_multiple.

As an example, previously you might write this:

gdb_test_multiple "print foo" "test foo" {
-re "expected output 1" {
pass "test foo"
}
-re "expected output 2" {
fail "test foo"
}
}

This is OK, but it's easy for the pass/fail strings to come out of
sync, or contain a typo. A better version would look like this:

set testname "test foo"
gdb_test_multiple "print foo" $testname {
-re "expected output 1" {
pass $testname
}
-re "expected output 2" {
fail $testname
}
}

This is better, but its a bit of a drag having to create a new
variable each time.

After this patch you can now write this:

gdb_test_multiple "print foo" "test foo" {
-re "expected output 1" {
pass $gdb_test_multiple_name
}
-re "expected output 2" {
fail $gdb_test_multiple_name
}
}

The $gdb_test_multiple_name is setup by gdb_test_multiple, and cleaned
up once the test has completed. Nested calls to gdb_test_multiple are
supported, though $gdb_test_multiple_name will only ever contain the
inner most test message (which is probably what you want).

My only regret is that '$gdb_test_multiple_name' is so long, but I
wanted something that was unlikely to clash with any existing variable
name, or anything that a user is likely to want to use.

I've tested this on x86-64/GNU Linux and see no test regressions, and
I've converted one test script over to make use of this new technique
both as an example, and to ensure that the new facility doesn't get
broken. I have no plans to convert all tests over to this technique,
but I hope others will find this useful for writing tests in the
future.

gdb/testsuite/ChangeLog:

* lib/gdb.exp (gdb_test_multiple): Add gdb_test_multiple_name
mechanism.
* gdb.base/annota1.exp: Update to use gdb_test_multiple_name.
---
gdb/testsuite/ChangeLog | 6 ++++
gdb/testsuite/gdb.base/annota1.exp | 58 ++++++++++++++++++--------------------
gdb/testsuite/lib/gdb.exp | 38 +++++++++++++++++++++++--
3 files changed, 70 insertions(+), 32 deletions(-)

diff --git a/gdb/testsuite/gdb.base/annota1.exp b/gdb/testsuite/gdb.base/annota1.exp
index 3d379f0fd49..96b0fce5cf7 100644
--- a/gdb/testsuite/gdb.base/annota1.exp
+++ b/gdb/testsuite/gdb.base/annota1.exp
@@ -97,11 +97,11 @@ gdb_expect {
#
gdb_test_multiple "info break" "breakpoint info" {
-re "\r\n\032\032post-prompt\r\n\r\n\032\032breakpoints-headers\r\n\r\n\032\032field 0\r\nNum \r\n\032\032field 1\r\nType \r\n\032\032field 2\r\nDisp \r\n\032\032field 3\r\nEnb \r\n\032\032field 4\r\nAddress +\r\n\032\032field 5\r\nWhat\r\n\r\n\032\032breakpoints-table\r\n\r\n\032\032record\r\n\r\n\032\032field 0\r\n1 \r\n\032\032field 1\r\nbreakpoint \r\n\032\032field 2\r\nkeep \r\n\032\032field 3\r\ny \r\n\032\032field 4\r\n$hex +\r\n\032\032field 5\r\nin main at ${escapedsrcfile}:$main_line\r\n\r\n\032\032breakpoints-table-end\r\n$gdb_prompt$" {
- pass "breakpoint info"
+ pass $gdb_test_multiple_name
}
-re "\r\n\032\032post-prompt\r\n\r\n\032\032breakpoints-headers\r\n\r\n\032\032field 0\r\nNum \r\n\032\032field 1\r\nType \r\n\032\032field 2\r\nDisp \r\n\032\032field 3\r\nEnb \r\n\032\032field 4\r\nAddress +\r\n\032\032field 5\r\nWhat\r\n\r\n\032\032breakpoints-table\r\n\r\n\032\032record\r\n\r\n\032\032field 0\r\n1 \r\n\032\032field 1\r\nbreakpoint \r\n\032\032field 2\r\nkeep \r\n\032\032field 3\r\ny \r\n\032\032field 4\r\n$hex +\r\n\032\032field 5\r\nin main at .*${srcfile}:$main_line\r\n\r\n\032\032breakpoints-table-end\r\n$gdb_prompt$" {
setup_xfail "*-*-*" 1270
- fail "breakpoint info"
+ fail $gdb_test_multiple_name
}
}

@@ -147,7 +147,7 @@ gdb_test_multiple "run" "run until main breakpoint" {
"\032\032source.*$srcfile:$main_line:.*:beg:$hex\r\n\r\n" \
"\032\032frame-end\r\n\r\n" \
"\032\032stopped.*$gdb_prompt$" } ] {
- pass "run until main breakpoint"
+ pass $gdb_test_multiple_name
}
}
#exp_internal 0
@@ -160,7 +160,7 @@ gdb_test_multiple "run" "run until main breakpoint" {
#
gdb_test_multiple "next" "go after array init line" {
-re "source .*annota1.c.*$gdb_prompt$" {
- pass "go after array init line"
+ pass $gdb_test_multiple_name
}
}

@@ -179,7 +179,7 @@ gdb_test_multiple "next" "go after array init line" {
#
gdb_test_multiple "print my_array" "print array" {
-re "\r\n\032\032post-prompt\r\n\r\n\032\032value-history-begin 1 -\r\n.*= \r\n\032\032value-history-value\r\n.\r\n\032\032array-section-begin 0 -\r\n1\r\n\032\032elt\r\n, 2\r\n\032\032elt\r\n, 3\r\n\032\032elt\r\n\r\n\032\032array-section-end\r\n.\r\n\r\n\032\032value-history-end\r\n$gdb_prompt$" {
- pass "print array"
+ pass $gdb_test_multiple_name
}
}

@@ -193,7 +193,7 @@ gdb_test_multiple "print my_array" "print array" {
#exp_internal 1
gdb_test_multiple "print non_existent_value" "print non_existent_value" {
-re "\r\n\032\032post-prompt\r\n\r\n\032\032error-begin\r\nNo symbol \"non_existent_value\" in current context.\r\n\r\n\032\032error\r\n$gdb_prompt$" {
- pass "print non_existent_value"
+ pass $gdb_test_multiple_name
}
}

@@ -204,7 +204,7 @@ gdb_test_multiple "print non_existent_value" "print non_existent_value" {
#
gdb_test_multiple "break handle_USR1" "break handle_USR1" {
-re "\r\n\032\032post-prompt\r\nBreakpoint.*at $hex: file.*$srcfile, line.*\r\n\032\032breakpoints-invalid\r\n.*$gdb_prompt$" {
- pass "break handle_USR1"
+ pass $gdb_test_multiple_name
}
}

@@ -213,10 +213,10 @@ gdb_test_multiple "break handle_USR1" "break handle_USR1" {
#
gdb_test_multiple "break printf" "break printf" {
-re "\r\n\032\032post-prompt\r\nBreakpoint.*at $hex.*\032\032breakpoints-invalid\r\n.*$gdb_prompt$" {
- pass "break printf"
+ pass $gdb_test_multiple_name
}
-re "\r\n\032\032post-prompt\r\nwarning: Breakpoint address adjusted from $hex to $hex.\r\n\r\n\032\032breakpoints-invalid\r\nBreakpoint.*at $hex.*$gdb_prompt$" {
- pass "break printf"
+ pass $gdb_test_multiple_name
}
}

@@ -229,9 +229,9 @@ set pat_end "\r\n\032\032breakpoint 3\r\n\r\nBreakpoint 3, \r\n\032\032frame-beg

gdb_test_multiple "continue" "continue to printf" {
-re "${pat_begin}($pat_adjust)?$pat_end" {
- pass "continue to printf"
+ pass $gdb_test_multiple_name
}
- -re ".*$gdb_prompt$" { fail "continue to printf" }
+ -re ".*$gdb_prompt$" { fail $gdb_test_multiple_name }
}

#
@@ -246,11 +246,11 @@ set pat_end "\r\n\032\032frame-source-file-end\r\n:\r\n\032\032frame-source-line

gdb_test_multiple "backtrace" "backtrace from shlibrary" {
-re "$pat_begin$escapedsrcfile$pat_end" {
- pass "backtrace from shlibrary"
+ pass $gdb_test_multiple_name
}
-re "$pat_begin.*$srcfile$pat_end" {
setup_xfail "*-*-*" 1270
- fail "backtrace from shlibrary"
+ fail $gdb_test_multiple_name
}
}

@@ -269,11 +269,11 @@ if [target_info exists gdb,nosignals] {
} else {
gdb_test_multiple "signal SIGUSR1" "send SIGUSR1" {
-re "\r\n\032\032post-prompt\r\nContinuing with signal SIGUSR1.\r\n\r\n\032\032starting\(\(\r\n\r\n\032\032frames-invalid\)|\(\r\n\r\n\032\032breakpoints-invalid\)\)+\r\n\r\n\032\032breakpoint 2\r\n\r\nBreakpoint 2, \r\n\032\032frame-begin 0 $hex\r\n\r\n\032\032frame-function-name\r\nhandle_USR1\r\n\032\032frame-args\r\n \\(\r\n\032\032arg-begin\r\nsig\r\n\032\032arg-name-end\r\n=\r\n\032\032arg-value -\r\n$decimal\r\n\032\032arg-end\r\n\\)\r\n\032\032frame-source-begin\r\n at \r\n\032\032frame-source-file\r\n${escapedsrcfile}\r\n\032\032frame-source-file-end\r\n:\r\n\032\032frame-source-line\r\n.*\r\n\032\032frame-source-end\r\n\r\n\r\n\032\032source.*annota1.c:.*:.*:beg:$hex\r\n$decimal\[^\r\n\]+\r\n\r\n\032\032frame-end\r\n\r\n\032\032stopped\r\n$gdb_prompt$" {
- pass "send SIGUSR1"
+ pass $gdb_test_multiple_name
}
-re "\r\n\032\032post-prompt\r\nContinuing with signal SIGUSR1.\r\n\r\n\032\032starting\(\(\r\n\r\n\032\032frames-invalid\)|\(\r\n\r\n\032\032breakpoints-invalid\)\)+\r\n\r\n\032\032breakpoint 2\r\n\r\nBreakpoint 2, \r\n\032\032frame-begin 0 $hex\r\n\r\n\032\032frame-function-name\r\nhandle_USR1\r\n\032\032frame-args\r\n \\(\r\n\032\032arg-begin\r\nsig\r\n\032\032arg-name-end\r\n=\r\n\032\032arg-value -\r\n$decimal\r\n\032\032arg-end\r\n\\)\r\n\032\032frame-source-begin\r\n at \r\n\032\032frame-source-file\r\n.*${srcfile}\r\n\032\032frame-source-file-end\r\n:\r\n\032\032frame-source-line\r\n.*\r\n\032\032frame-source-end\r\n\r\n\r\n\032\032source.*annota1.c:.*:.*:beg:$hex\r\n$decimal\[^\r\n\]+\r\n\r\n\032\032frame-end\r\n\r\n\032\032stopped\r\n$gdb_prompt$" {
setup_xfail "*-*-*" 1270
- fail "send SIGUSR1"
+ fail $gdb_test_multiple_name
}
}

@@ -283,7 +283,7 @@ if [target_info exists gdb,nosignals] {
#
gdb_test_multiple "backtrace" "backtrace @ signal handler" {
-re "frame-begin 0 $hex\r\n#0.*frame-end.*frame-begin 1 $hex\r\n#1.*(\032\032signal-handler-caller\r\n.signal handler called.\r\n\r\n)+\032\032frame-end\r\n\r\n\032\032frame-begin 2 $hex\r\n#2.*(frame-begin 3 $hex\r\n#3.*)*frame-end.*$gdb_prompt$" {
- pass "backtrace @ signal handler"
+ pass $gdb_test_multiple_name
}
}
}
@@ -293,19 +293,19 @@ if [target_info exists gdb,nosignals] {
#
gdb_test_multiple "delete 1" "delete bp 1" {
-re "\r\n\032\032post-prompt\r\n${breakpoints_invalid}$gdb_prompt$" {
- pass "delete bp 1"
+ pass $gdb_test_multiple_name
}
}

gdb_test_multiple "delete 2" "delete bp 2" {
-re "\r\n\032\032post-prompt\r\n${breakpoints_invalid}$gdb_prompt$" {
- pass "delete bp 2"
+ pass $gdb_test_multiple_name
}
}

gdb_test_multiple "delete 3" "delete bp 3" {
-re "\r\n\032\032post-prompt\r\n${breakpoints_invalid}$gdb_prompt$" {
- pass "delete bp 3"
+ pass $gdb_test_multiple_name
}
}

@@ -313,14 +313,13 @@ gdb_test_multiple "delete 3" "delete bp 3" {
# break in main, after value is initialized. This is in preparation
# to test the annotate output for the display command.
#
-set test "break in main"
-gdb_test_multiple "break ${srcfile}:${main_line}" $test {
+gdb_test_multiple "break ${srcfile}:${main_line}" "break in main" {
-re "post-prompt.*Breakpoint 4 at $hex: file ${escapedsrcfile}, line $main_line.*\032\032breakpoints-invalid.*$gdb_prompt$" {
- pass $test
+ pass $gdb_test_multiple_name
}
-re "post-prompt.*Breakpoint 4 at $hex: file .*${srcfile}, line $main_line.*\032\032breakpoints-invalid.*$gdb_prompt$" {
setup_xfail "*-*-*" 1270
- fail $test
+ fail $gdb_test_multiple_name
}
}

@@ -405,12 +404,11 @@ gdb_test_multiple "next" "breakpoint ignore count" {

# Get the inferior's PID for later.

-set test "get inferior pid"
set pid -1
-gdb_test_multiple "info inferior 1" "$test" {
+gdb_test_multiple "info inferior 1" "get inferior pid" {
-re "process (\[0-9\]*).*$gdb_prompt$" {
set pid $expect_out(1,string)
- pass "$test"
+ pass $gdb_test_multiple_name
}
}

@@ -429,7 +427,7 @@ if [target_info exists gdb,nosignals] {
} else {
gdb_test_multiple "signal SIGTRAP" "signal sent" {
-re ".*\032\032post-prompt\r\nContinuing with signal SIGTRAP.\r\n\r\n\032\032starting\(\r\n\r\n\032\032frames-invalid\)+\r\n\r\n\032\032signalled\r\n\r\nProgram terminated with signal \r\n\032\032signal-name\r\nSIGTRAP\r\n\032\032signal-name-end\r\n, \r\n\032\032signal-string\r\nTrace.breakpoint trap\r\n\032\032signal-string-end\r\n.\r\nThe program no longer exists.\r\n\r\n\032\032thread-exited,id=\"${decimal}\",group-id=\"i${decimal}\"\r\n\r\n\032\032stopped\r\n$gdb_prompt$" {
- pass "signal sent"
+ pass $gdb_test_multiple_name
}
}
}
@@ -482,13 +480,13 @@ proc thread_test {} {

gdb_test_multiple "continue" "new thread" {
-re "\032\032new-thread.*\r\n$gdb_prompt$" {
- pass "new thread"
+ pass $gdb_test_multiple_name
}
}

gdb_test_multiple "continue" "thread exit" {
-re "\032\032thread-exited,id=\"${decimal}\",group-id=\"i${decimal}\".*\r\n$gdb_prompt$" {
- pass "thread exit"
+ pass $gdb_test_multiple_name
}
}
}
@@ -497,7 +495,7 @@ proc thread_test {} {
proc thread_switch {} {
gdb_test_multiple "thread 1" "thread switch" {
-re ".*\032\032thread-changed" {
- pass "thread switch"
+ pass $gdb_test_multiple_name
}
}
}
diff --git a/gdb/testsuite/lib/gdb.exp b/gdb/testsuite/lib/gdb.exp
index 3a1f053cf8a..a472650cc75 100644
--- a/gdb/testsuite/lib/gdb.exp
+++ b/gdb/testsuite/lib/gdb.exp
@@ -719,10 +719,24 @@ proc gdb_internal_error_resync {} {
#
# gdb_test_multiple "print foo" "test foo" {
# -re "expected output 1" {
-# pass "print foo"
+# pass "test foo"
# }
# -re "expected output 2" {
-# fail "print foo"
+# fail "test foo"
+# }
+# }
+#
+# Within action elements you can also make use of the variable
+# gdb_test_multiple_name, this variable is setup automatically by
+# gdb_test_multiple, and contains the value of MESSAGE. You can then
+# write this, which is equivalent to the above:
+#
+# gdb_test_multiple "print foo" "test foo" {
+# -re "expected output 1" {
+# pass $gdb_test_multiple_name
+# }
+# -re "expected output 2" {
+# fail $gdb_test_multiple_name
# }
# }
#
@@ -1038,8 +1052,28 @@ proc gdb_test_multiple { command message user_code { prompt_regexp "" } } {
}
}

+ # Create gdb_test_multiple_name in the parent scope. If this
+ # variable already exists, which is might if we have nested calls
+ # to gdb_test_multiple, then preserve the old value, otherwise,
+ # create a new variable in the parent scope.
+ upvar gdb_test_multiple_name gdb_test_multiple_name
+ if { [info exists gdb_test_multiple_name] } {
+ set gdb_test_multiple_name_old "$gdb_test_multiple_name"
+ }
+ set gdb_test_multiple_name "$message"
+
set result 0
set code [catch {gdb_expect $code} string]
+
+ # Clean up the gdb_test_multiple_name variable. If we had a
+ # previous value then restore it, otherwise, delete the variable
+ # from the parent scope.
+ if { [info exists gdb_test_multiple_name_old] } {
+ set gdb_test_multiple_name "$gdb_test_multiple_name_old"
+ } else {
+ unset gdb_test_multiple_name
+ }
+
if {$code == 1} {
global errorInfo errorCode
return -code error -errorinfo $errorInfo -errorcode $errorCode $string
--
2.14.5