[PATCH v2 2/5] Further cleanup/modernization of gdb.base/commands.exp

Pedro Alves
2016-11-09T00:19:39Z

- Use multi_line for matching multi-line GDB output.

- Add a multi_line_input variant of multi_line to build GDB input and
use it throughout.

(The two changes above make the tests much more readable, IMO.)

- Remove gdb_stop_suppressing_tests uses.

- tighten a few regexps.

- Replace send_gdb/gdb_expect with gdb_test_multiple and simplify,
making pass/fail messages the same.

gdb/ChangeLog:
yyyy-mm-dd Pedro Alves <palves@redhat.com>

* gdb.base/commands.exp (runto_or_return): New
procedure.
(gdbvar_simple_if_test, gdbvar_simple_while_test)
(gdbvar_complex_if_while_test, progvar_simple_if_test)
(progvar_simple_while_test, progvar_complex_if_while_test)
(if_while_breakpoint_command_test)
(infrun_breakpoint_command_test, breakpoint_command_test)
(user_defined_command_test, watchpoint_command_test)
(test_command_prompt_position, redefine_hook_test)
(redefine_backtrace_test): Use multi_line_input and multi_line.
* lib/gdb.exp (multi_line_input): New procedure.
---
gdb/testsuite/gdb.base/commands.exp | 333 +++++++++++++++++++++++-------------
gdb/testsuite/lib/gdb.exp | 9 +
2 files changed, 226 insertions(+), 116 deletions(-)

diff --git a/gdb/testsuite/gdb.base/commands.exp b/gdb/testsuite/gdb.base/commands.exp
index fe2c23a..b6c8f9e 100644
--- a/gdb/testsuite/gdb.base/commands.exp
+++ b/gdb/testsuite/gdb.base/commands.exp
@@ -23,16 +23,41 @@ if { [prepare_for_testing commands.exp commands run.c {debug additional_flags=-D
return -1
}

+# Run to FUNCTION. If that fails, issue a FAIL and make the caller
+# return.
+
+proc runto_or_return {function} {
+ if { ![runto factorial] } {
+ fail "cannot run to $function"
+ return -code return
+ }
+}
+
proc_with_prefix gdbvar_simple_if_test {} {
global gdb_prompt

gdb_test_no_output "set \$foo = 0" "set foo"
# All this test should do is print 0xdeadbeef once.
- gdb_test "if \$foo == 1\np/x 0xfeedface\nelse\np/x 0xdeadbeef\nend" \
- "\\\$\[0-9\]* = 0xdeadbeef" "#1"
+ gdb_test \
+ [multi_line_input \
+ "if \$foo == 1" \
+ " p/x 0xfeedface" \
+ "else" \
+ " p/x 0xdeadbeef" \
+ "end"] \
+ "\\\$\[0-9\]* = 0xdeadbeef" \
+ "#1"
+
# All this test should do is print 0xfeedface once.
- gdb_test "if \$foo == 0\np/x 0xfeedface\nelse\np/x 0xdeadbeef\nend" \
- "\\\$\[0-9\]* = 0xfeedface" "#2"
+ gdb_test \
+ [multi_line_input \
+ "if \$foo == 0" \
+ " p/x 0xfeedface" \
+ "else" \
+ " p/x 0xdeadbeef" \
+ "end"] \
+ "\\\$\[0-9\]* = 0xfeedface" \
+ "#2"
}

proc_with_prefix gdbvar_simple_while_test {} {
@@ -40,74 +65,133 @@ proc_with_prefix gdbvar_simple_while_test {} {

gdb_test_no_output "set \$foo = 5" "set foo"
# This test should print 0xfeedface five times.
- gdb_test "while \$foo > 0\np/x 0xfeedface\nset \$foo -= 1\nend" \
- "\\\$\[0-9\]* = 0xfeedface\[^\n\]*\n\\\$\[0-9\]* = 0xfeedface\[^\n\]*\n\\\$\[0-9\]* = 0xfeedface\[^\n\]*\n\\\$\[0-9\]* = 0xfeedface\[^\n\]*\n\\\$\[0-9\]* = 0xfeedface" \
- "#1"
+ gdb_test \
+ [multi_line_input \
+ "while \$foo > 0" \
+ " p/x 0xfeedface" \
+ " set \$foo -= 1" \
+ "end"] \
+ [multi_line \
+ "\\\$\[0-9\]* = 0xfeedface\[^\n\]*" \
+ "\\\$\[0-9\]* = 0xfeedface\[^\n\]*" \
+ "\\\$\[0-9\]* = 0xfeedface\[^\n\]*" \
+ "\\\$\[0-9\]* = 0xfeedface\[^\n\]*" \
+ "\\\$\[0-9\]* = 0xfeedface"] \
+ "#1"
}

proc_with_prefix gdbvar_complex_if_while_test {} {
global gdb_prompt

- gdb_test_no_output "set \$foo = 4" \
- "set foo"
+ gdb_test_no_output "set \$foo = 4" "set foo"
# This test should alternate between 0xdeadbeef and 0xfeedface two times.
- gdb_test "while \$foo > 0\nset \$foo -= 1\nif \(\$foo % 2\) == 1\np/x 0xdeadbeef\nelse\np/x 0xfeedface\nend\nend" \
- "\\\$\[0-9\]* = 0xdeadbeef\[^\n\]*\n\\\$\[0-9\]* = 0xfeedface\[^\n\]*\n\\\$\[0-9\]* = 0xdeadbeef\[^\n\]*\n\\\$\[0-9\]* = 0xfeedface" \
- "#1"
+ gdb_test \
+ [multi_line_input \
+ "while \$foo > 0" \
+ " set \$foo -= 1" \
+ " if \(\$foo % 2\) == 1" \
+ " p/x 0xdeadbeef" \
+ " else" \
+ " p/x 0xfeedface" \
+ " end" \
+ "end"] \
+ [multi_line \
+ "\\\$\[0-9\]* = 0xdeadbeef\[^\n\]*" \
+ "\\\$\[0-9\]* = 0xfeedface\[^\n\]*" \
+ "\\\$\[0-9\]* = 0xdeadbeef\[^\n\]*" \
+ "\\\$\[0-9\]* = 0xfeedface"] \
+ "#1"
}

proc_with_prefix progvar_simple_if_test {} {
global gdb_prompt

- if { ![runto factorial] } then { gdb_suppress_tests; }
+ runto_or_return factorial
+
# Don't depend upon argument passing, since most simulators don't
# currently support it. Bash value variable to be what we want.
- gdb_test "p value=5" ".*" "set value to 5"
+ gdb_test "p value=5" " = 5" "set value to 5"
# All this test should do is print 0xdeadbeef once.
- gdb_test "if value == 1\np/x 0xfeedface\nelse\np/x 0xdeadbeef\nend" \
- "\\\$\[0-9\]* = 0xdeadbeef" \
- "#1"
+ gdb_test \
+ [multi_line_input \
+ "if value == 1" \
+ " p/x 0xfeedface" \
+ "else" \
+ " p/x 0xdeadbeef" \
+ "end"] \
+ "\\\$\[0-9\]* = 0xdeadbeef" \
+ "#1"
+
# All this test should do is print 0xfeedface once.
- gdb_test "if value == 5\np/x 0xfeedface\nelse\np/x 0xdeadbeef\nend" \
- "\\\$\[0-9\]* = 0xfeedface" \
- "#2"
- gdb_stop_suppressing_tests
+ gdb_test \
+ [multi_line_input \
+ "if value == 5" \
+ " p/x 0xfeedface" \
+ "else" \
+ " p/x 0xdeadbeef" \
+ "end"] \
+ "\\\$\[0-9\]* = 0xfeedface" \
+ "#2"
}

proc_with_prefix progvar_simple_while_test {} {
global gdb_prompt

- if { ![runto factorial] } then { gdb_suppress_tests }
+ runto_or_return factorial
+
# Don't depend upon argument passing, since most simulators don't
# currently support it. Bash value variable to be what we want.
- gdb_test "p value=5" ".*" "set value to 5"
+ gdb_test "p value=5" " = 5" "set value to 5"
# This test should print 0xfeedface five times.
- gdb_test "while value > 0\np/x 0xfeedface\nset value -= 1\nend" \
- "\\\$\[0-9\]* = 0xfeedface\[^\n\]*\n\\\$\[0-9\]* = 0xfeedface\[^\n\]*\n\\\$\[0-9\]* = 0xfeedface\[^\n\]*\n\\\$\[0-9\]* = 0xfeedface\[^\n\]*\n\\\$\[0-9\]* = 0xfeedface" \
- "#1"
- gdb_stop_suppressing_tests
+ gdb_test \
+ [multi_line_input \
+ "while value > 0" \
+ " p/x 0xfeedface" \
+ " set value -= 1" \
+ "end"] \
+ [multi_line \
+ "\\\$\[0-9\]* = 0xfeedface\[^\n\]*" \
+ "\\\$\[0-9\]* = 0xfeedface\[^\n\]*" \
+ "\\\$\[0-9\]* = 0xfeedface\[^\n\]*" \
+ "\\\$\[0-9\]* = 0xfeedface\[^\n\]*" \
+ "\\\$\[0-9\]* = 0xfeedface"] \
+ "#1"
}

proc_with_prefix progvar_complex_if_while_test {} {
global gdb_prompt

- if { ![runto factorial] } then { gdb_suppress_tests }
+ runto_or_return factorial
+
# Don't depend upon argument passing, since most simulators don't
# currently support it. Bash value variable to be what we want.
- gdb_test "p value=4" ".*" "set value to 4"
- # This test should alternate between 0xdeadbeef and 0xfeedface two times.
- gdb_test "while value > 0\nset value -= 1\nif \(value % 2\) == 1\np/x 0xdeadbeef\nelse\np/x 0xfeedface\nend\nend" \
- "\\\$\[0-9\]* = 0xdeadbeef\[^\n\]*\n\\\$\[0-9\]* = 0xfeedface\[^\n\]*\n\\\$\[0-9\]* = 0xdeadbeef\[^\n\]*\n\\\$\[0-9\]* = 0xfeedface" \
- "#1"
- gdb_stop_suppressing_tests
+ gdb_test "p value=4" " = 4" "set value to 4"
+ # This test should alternate between 0xdeadbeef and 0xfeedface two
+ # times.
+ gdb_test \
+ [multi_line_input \
+ "while value > 0" \
+ " set value -= 1" \
+ " if \(value % 2\) == 1" \
+ " p/x 0xdeadbeef" \
+ " else" \
+ " p/x 0xfeedface" \
+ " end" \
+ "end"] \
+ [multi_line \
+ "\\\$\[0-9\]* = 0xdeadbeef\[^\n\]*" \
+ "\\\$\[0-9\]* = 0xfeedface\[^\n\]*" \
+ "\\\$\[0-9\]* = 0xdeadbeef\[^\n\]*" \
+ "\\\$\[0-9\]* = 0xfeedface"] \
+ "#1"
}

proc_with_prefix if_while_breakpoint_command_test {} {
+ runto_or_return factorial

- if { ![runto factorial] } then { gdb_suppress_tests }
# Don't depend upon argument passing, since most simulators don't
# currently support it. Bash value variable to be what we want.
- gdb_test "p value=5" ".*" "set value to 5"
+ gdb_test "p value=5" " = 5" "set value to 5"
delete_breakpoints
gdb_test "break factorial" "Breakpoint.*at.*" "break factorial"

@@ -118,14 +202,28 @@ proc_with_prefix if_while_breakpoint_command_test {} {
}

# This test should alternate between 0xdeadbeef and 0xfeedface two times.
- gdb_test "while value > 0\nset value -= 1\nif \(value % 2\) == 1\np/x 0xdeadbeef\nelse\np/x 0xfeedface\nend\nend\nend" \
- "" \
- "commands part 2"
- gdb_test "continue" \
- "\\\$\[0-9\]* = 0xdeadbeef\[^\n\]*\n\\\$\[0-9\]* = 0xfeedface\[^\n\]*\n\\\$\[0-9\]* = 0xdeadbeef\[^\n\]*\n\\\$\[0-9\]* = 0xfeedface" \
- "#1"
+ gdb_test \
+ [multi_line_input \
+ "while value > 0" \
+ " set value -= 1" \
+ " if \(value % 2\) == 1" \
+ " p/x 0xdeadbeef" \
+ " else" \
+ " p/x 0xfeedface" \
+ " end" \
+ "end" \
+ "end"] \
+ "" \
+ "commands part 2"
+ gdb_test \
+ "continue" \
+ [multi_line \
+ "\\\$\[0-9\]* = 0xdeadbeef\[^\n\]*" \
+ "\\\$\[0-9\]* = 0xfeedface\[^\n\]*" \
+ "\\\$\[0-9\]* = 0xdeadbeef\[^\n\]*" \
+ "\\\$\[0-9\]* = 0xfeedface"] \
+ "#1"
gdb_test "info break" "while.*set.*if.*p/x.*else.*p/x.*end.*"
- gdb_stop_suppressing_tests
}

# Test that we can run the inferior from breakpoint commands.
@@ -135,11 +233,11 @@ proc_with_prefix if_while_breakpoint_command_test {} {
# subsection "Breakpoint command lists".

proc_with_prefix infrun_breakpoint_command_test {} {
+ runto_or_return factorial

- if { ![runto factorial] } then { gdb_suppress_tests }
# Don't depend upon argument passing, since most simulators don't
# currently support it. Bash value variable to be what we want.
- gdb_test "p value=6" ".*" "set value to 6"
+ gdb_test "p value=6" " = 6" "set value to 6"
delete_breakpoints
gdb_test "break factorial if value == 5" "Breakpoint.*at.*"

@@ -159,24 +257,26 @@ proc_with_prefix infrun_breakpoint_command_test {} {

gdb_test "continue" \
"Continuing.*.*.*Breakpoint \[0-9\]*, factorial \\(value=5\\).*at.*\[0-9\]*\[ \]*if \\(value > 1\\) \{.*\[0-9\]*\[ \]*value \\*= factorial \\(value - 1\\);.*"
-
- gdb_stop_suppressing_tests
}

proc_with_prefix breakpoint_command_test {} {
+ runto_or_return factorial

- if { ![runto factorial] } then { gdb_suppress_tests; }
# Don't depend upon argument passing, since most simulators don't
# currently support it. Bash value variable to be what we want.
- gdb_test "p value=6" ".*" "set value to 6"
+ gdb_test "p value=6" " = 6" "set value to 6"
delete_breakpoints
gdb_test "break factorial" "Breakpoint.*at.*"
- gdb_test "commands\nprintf \"Now the value is %d\\n\", value\nend" \
- "End with.*" "commands"
+ gdb_test \
+ [multi_line_input \
+ "commands" \
+ " printf \"Now the value is %d\\n\", value" \
+ "end"] \
+ "End with.*" \
+ "commands"
gdb_test "continue" \
"Breakpoint \[0-9\]*, factorial.*Now the value is 5"
gdb_test "print value" " = 5"
- gdb_stop_suppressing_tests
}

# Test a simple user defined command (with arguments)
@@ -192,13 +292,28 @@ proc_with_prefix user_defined_command_test {} {
}

# This test should alternate between 0xdeadbeef and 0xfeedface two times.
- gdb_test "while \$arg0 > 0\nset \$arg0 -= 1\nif \(\$arg0 % 2\) == 1\np/x 0xdeadbeef\nelse\np/x 0xfeedface\nend\nend\nend" \
- "" \
- "enter commands"
-
- gdb_test "mycommand \$foo" \
- "\\\$\[0-9\]* = 0xdeadbeef\[^\n\]*\n\\\$\[0-9\]* = 0xfeedface\[^\n\]*\n\\\$\[0-9\]* = 0xdeadbeef\[^\n\]*\n\\\$\[0-9\]* = 0xfeedface" \
- "execute user-defined command"
+ gdb_test \
+ [multi_line_input \
+ "while \$arg0 > 0" \
+ " set \$arg0 -= 1" \
+ " if \(\$arg0 % 2\) == 1" \
+ " p/x 0xdeadbeef" \
+ " else" \
+ " p/x 0xfeedface" \
+ " end" \
+ "end" \
+ "end"] \
+ "" \
+ "enter commands"
+
+ gdb_test \
+ "mycommand \$foo" \
+ [multi_line \
+ "\\\$\[0-9\]* = 0xdeadbeef\[^\n\]*" \
+ "\\\$\[0-9\]* = 0xfeedface\[^\n\]*" \
+ "\\\$\[0-9\]* = 0xdeadbeef\[^\n\]*" \
+ "\\\$\[0-9\]* = 0xfeedface"] \
+ "execute user-defined command"
gdb_test "show user mycommand" \
" while \\\$arg0.*set.* if \\\(\\\$arg0.*p/x.* else\[^\n\].*p/x.* end\[^\n\].* end\[^\n\].*" \
"display user command"
@@ -233,7 +348,8 @@ proc_with_prefix watchpoint_command_test {} {
gdb_test_no_output "set can-use-hw-watchpoints 0" ""
}

- if { ![runto factorial] } then { return }
+ runto_or_return factorial
+
delete_breakpoints

# Verify that we can create a watchpoint, and give it a commands
@@ -299,59 +415,42 @@ proc_with_prefix watchpoint_command_test {} {
proc_with_prefix test_command_prompt_position {} {
global gdb_prompt

- if { ![runto factorial] } then { gdb_suppress_tests; }
+ runto_or_return factorial
+
# Don't depend upon argument passing, since most simulators don't
# currently support it. Bash value variable to be what we want.
delete_breakpoints
gdb_test "break factorial" "Breakpoint.*at.*"
gdb_test "p value=5" ".*" "set value to 5"
# All this test should do is print 0xdeadbeef once.
- gdb_test "if value == 1\np/x 0xfeedface\nelse\np/x 0xdeadbeef\nend" \
- "\\\$\[0-9\]* = 0xdeadbeef" \
- "if test"
-
+ gdb_test \
+ [multi_line_input \
+ "if value == 1" \
+ " p/x 0xfeedface" \
+ "else" \
+ " p/x 0xdeadbeef" \
+ "end"] \
+ "\\\$\[0-9\]* = 0xdeadbeef" \
+ "if test"
+
# Now let's test for the correct position of the '>' in gdb's
# prompt for commands. It should be at the beginning of the line,
# and not after one space.

- send_gdb "commands\n"
- gdb_expect {
- -re "Type commands.*End with.*\[\r\n\]>$" {
- send_gdb "printf \"Now the value is %d\\n\", value\n"
- gdb_expect {
+ set test "> OK"
+ gdb_test_multiple "commands" $test {
+ -re "Type commands.*End with.*\[\r\n\]>$" {
+ gdb_test_multiple "printf \"Now the value is %d\\n\", value" $test {
-re "^printf.*value\r\n>$" {
- send_gdb "end\n"
- gdb_expect {
+ gdb_test_multiple "end" $test {
-re "^end\r\n$gdb_prompt $" {
- pass "> OK"
- }
- -re ".*$gdb_prompt $" {
- fail "some other message"
- }
- timeout {
- fail "(timeout) 1"
+ pass $test
}
}
}
- -re "^ >$" { fail "> not OK" }
- -re ".*$gdb_prompt $" {
- fail "wrong message"
- }
- timeout {
- fail "(timeout) 2"
- }
}
}
- -re "Type commands.*End with.*\[\r\n\] >$" {
- fail "prompt not OK"
- }
- -re ".*$gdb_prompt $" {
- fail "commands"
- }
- timeout { fail "(timeout) 3" }
}
-
- gdb_stop_suppressing_tests
}

@@ -693,13 +792,20 @@ proc_with_prefix error_clears_commands_left {} {
proc_with_prefix redefine_hook_test {} {
global gdb_prompt

- gdb_test "define one\nend" \
- "" \
- "define one"
+ gdb_test \
+ [multi_line_input \
+ "define one"\
+ "end"] \
+ "" \
+ "define one"

- gdb_test "define hook-one\necho hibob\\n\nend" \
- "" \
- "define hook-one"
+ gdb_test \
+ [multi_line_input \
+ "define hook-one" \
+ "echo hibob\\n" \
+ "end"] \
+ "" \
+ "define hook-one"

set test "redefine one"
gdb_test_multiple "define one" $test {
@@ -713,13 +819,9 @@ proc_with_prefix redefine_hook_test {} {
}
}

- gdb_test "end" \
- "" \
- "enter commands for one redefinition"
+ gdb_test "end" "" "enter commands for one redefinition"

- gdb_test "one" \
- "hibob" \
- "execute one command"
+ gdb_test "one" "hibob" "execute one command"
}

proc_with_prefix redefine_backtrace_test {} {
@@ -737,16 +839,15 @@ proc_with_prefix redefine_backtrace_test {} {
}
}

- gdb_test "echo hibob\\n\nend" \
- "" \
- "enter commands"
+ gdb_test \
+ [multi_line_input \
+ "echo hibob\\n" \
+ "end"] \
+ "" \
+ "enter commands"

- gdb_test "backtrace" \
- "hibob" \
- "execute backtrace command"
- gdb_test "bt" \
- "hibob" \
- "execute bt command"
+ gdb_test "backtrace" "hibob" "execute backtrace command"
+ gdb_test "bt" "hibob" "execute bt command"
}

gdbvar_simple_if_test
diff --git a/gdb/testsuite/lib/gdb.exp b/gdb/testsuite/lib/gdb.exp
index 735ed11..1ba2509 100644
--- a/gdb/testsuite/lib/gdb.exp
+++ b/gdb/testsuite/lib/gdb.exp
@@ -6005,5 +6005,14 @@ proc multi_line { args } {
return [join $args "\r\n"]
}

+# Similar to the above, but while multi_line is meant to be used to
+# match GDB output, this one is meant to be used to build strings to
+# send as GDB input.
+
+proc multi_line_input { args } {
+ return [join $args "\n"]
+}
+
+
# Always load compatibility stuff.
load_lib future.exp
--
2.5.5