Fix gdb.base/testenv.exp against --target_board=native-extended-gdbserver

Currently we get:

  Running ..../src/gdb/testsuite/gdb.base/testenv.exp ...
  FAIL: gdb.base/testenv.exp: test no TEST_GDB var
  FAIL: gdb.base/testenv.exp: test with one TEST_GDB var
  FAIL: gdb.base/testenv.exp: test with two TEST_GDB var
  FAIL: gdb.base/testenv.exp: test with one TEST_GDB var, after unset
  FAIL: gdb.base/testenv.exp: test with TEST_GDB_GLOBAL
  FAIL: gdb.base/testenv.exp: test with TEST_GDB_GLOBAL unset

The problem is that the testcase relies on stdio.  While we could fix
this for gdbserver by read output from inferior_spawn_id, a better fix
it to not rely on stdio at all.  That's what this commit does.
Instead, it reads variables off of the inferior to extract the
necessary information.

Along the way, most of the .exp file is reimplemented/cleaned up using
more modern mechanisms.  E.g., with_test_prefix, proc_with_prefix,
save_vars, etc.  Also, a missing check for "is_remote host" is added.

gdb/testsuite/ChangeLog:
2017-10-13  Pedro Alves  <palves@redhat.com>

	* gdb.base/testenv.exp: Check use_gdb_stub instead of is_remote.
	(test_num_test_vars, run_and_count_vars, find_env)
	(test_set_unset_env, test_inherit_env_var): New procedures.
	(top level): Use them.
This commit is contained in:
Pedro Alves
2017-10-12 18:27:20 +01:00
parent dad0c6d2f7
commit 50500caf81
2 changed files with 134 additions and 64 deletions

View File

@ -1,3 +1,10 @@
2017-10-13 Pedro Alves <palves@redhat.com>
* gdb.base/testenv.exp: Check use_gdb_stub instead of is_remote.
(test_num_test_vars, run_and_count_vars, find_env)
(test_set_unset_env, test_inherit_env_var): New procedures.
(top level): Use them.
2017-10-13 Pedro Alves <palves@redhat.com> 2017-10-13 Pedro Alves <palves@redhat.com>
* lib/selftest-support.exp (selftest_setup): Extend comments, and * lib/selftest-support.exp (selftest_setup): Extend comments, and

View File

@ -18,9 +18,10 @@
# Check if environment variables are correctly passed to inferiors # Check if environment variables are correctly passed to inferiors
# #
# There's no support for passing environment variables in the remote protocol. # Can't pass environment variables to the inferior if when we connect,
if { [is_remote target] } { # the inferior is already running.
return 0 if [target_info exists use_gdb_stub] {
return
} }
standard_testfile .c standard_testfile .c
@ -32,92 +33,154 @@ if { [prepare_for_testing "failed to prepare" ${binfile} ${srcfile}] } {
return -1 return -1
} }
# make sure $pc is sane, in case we're talking to a board. # Test that the the inferior sees EXPECTED env vars starting with
if { ![runto_main] } { # "TEST_GDB".
gdb_suppress_tests proc test_num_test_vars {expected message} {
set num [get_integer_valueof "j" -1 "$message, get num vars"]
gdb_assert {$num == $expected} "$message, confirmed"
} }
set bp_line [gdb_get_line_number "set breakpoint here"] set bp_line [gdb_get_line_number "set breakpoint here"]
gdb_breakpoint $bp_line gdb_breakpoint $bp_line
# Restart test program, and prepare for another test sequence.
# Returns true on success.
proc run_and_count_vars {} {
global srcfile bp_line
return [runto "$srcfile:$bp_line"]
}
# Find environment variable named VARNAME (peeking inferior variables
# directly), and return its value. Returns "<not found>" if not
# found.
proc find_env {varname} {
global gdb_prompt
for {set i 0} {1} {incr i} {
set test "printf \"var: %s\\n\", envp\[$i\] ? envp\[$i\] : \"\""
set var ""
gdb_test_multiple $test $test {
-re "var: \r\n$gdb_prompt $" {
return "<not found>"
}
-re "var: \(\[^\r\n\]*\)\r\n$gdb_prompt $" {
set var $expect_out(1,string)
}
}
if {[string match "$varname=*" $var]} {
set from [expr [string first "=" $var] + 1]
set to [string length $var]
return [string range $var $from $to]
}
}
}
# #
# Test gdb set/unset environment commands. # Test gdb set/unset environment commands.
# Executable lists and counts all environment variables # The executable lists and counts all environment variables
# starting with TEST_GDB. # starting with TEST_GDB.
proc_with_prefix test_set_unset_env {} {
global binfile
# First test with no TEST_GDB_VAR clean_restart $binfile
gdb_test "continue" \
".*Program found 0 variables starting with TEST_GDB.*" \
"test no TEST_GDB var"
# First test with no TEST_GDB_VAR.
with_test_prefix "test1" {
if ![run_and_count_vars] {
return
}
test_num_test_vars 0 "no TEST_GDB vars"
}
# Second test with one TEST_GDB_VAR.
with_test_prefix "test2" {
gdb_test_no_output "set env TEST_GDB_VAR1 test1" \ gdb_test_no_output "set env TEST_GDB_VAR1 test1" \
"set TEST_GDB_VAR1" "set TEST_GDB_VAR1"
runto_main if ![run_and_count_vars] {
gdb_breakpoint $bp_line return
}
# Second test with one TEST_GDB_VAR test_num_test_vars 1 "one TEST_GDB var"
gdb_test "continue" \ }
".*Program found 1 variables starting with TEST_GDB.*" \
"test with one TEST_GDB var"
# Third test with two TEST_GDB_VAR.
with_test_prefix "test3" {
gdb_test_no_output "set env TEST_GDB_VAR2 test2" \ gdb_test_no_output "set env TEST_GDB_VAR2 test2" \
"set TEST_GDB_VAR2" "set TEST_GDB_VAR2"
runto_main if ![run_and_count_vars] {
gdb_breakpoint $bp_line return
}
# Third test with two TEST_GDB_VAR test_num_test_vars 2 "two TEST_GDB var"
gdb_test "continue" \ }
".*Program found 2 variables starting with TEST_GDB.*" \
"test with two TEST_GDB var"
gdb_test_no_output "unset env TEST_GDB_VAR1" \
"unset TEST_GDB_VAR1"
runto_main
gdb_breakpoint $bp_line
# Fourth test with one TEST_GDB_VAR left, after one was removed # Fourth test with one TEST_GDB_VAR left, after one was removed
# with unset command. # with unset command.
gdb_test "continue" \ with_test_prefix "test4" {
".*Program found 1 variables starting with TEST_GDB.*" \ gdb_test_no_output "unset env TEST_GDB_VAR1" \
"test with one TEST_GDB var, after unset" "unset TEST_GDB_VAR1"
gdb_exit if ![run_and_count_vars] {
return
}
test_num_test_vars 1 "one TEST_GDB var, after unset"
}
}
proc_with_prefix test_inherit_env_var {} {
global binfile
global bp_line
global env
# This test assumes that the build's environ (where dejagnu runs)
# is the same as the host's (where gdb runs) environ.
if [is_remote host] {
return
}
save_vars {env(TEST_GDB_GLOBAL)} {
set env(TEST_GDB_GLOBAL) "Global environment value" set env(TEST_GDB_GLOBAL) "Global environment value"
clean_restart $binfile clean_restart $binfile
# make sure $pc is sane, in case we're talking to a board.
if { ![runto_main] } {
gdb_suppress_tests
}
set bp_line [gdb_get_line_number "set breakpoint here"]
gdb_breakpoint $bp_line gdb_breakpoint $bp_line
gdb_test "show env" ".*TEST_GDB_GLOBAL=.*" "test passing TEST_GDB_GLOBAL to GDB" # First test with only inherited TEST_GDB_GLOBAL.
# First test with only inherited TEST_GDB_GLOBAL with_test_prefix "test1" {
gdb_test "continue" \ if ![run_and_count_vars] {
".*TEST_GDB_GLOBAL=Global environment value.*Program found 1 variables starting with TEST_GDB.*" \ return
"test with TEST_GDB_GLOBAL" }
gdb_test "show env" ".*TEST_GDB_GLOBAL=.*" \
"test passing TEST_GDB_GLOBAL to GDB"
test_num_test_vars 1 "TEST_GDB_GLOBAL"
set var [find_env "TEST_GDB_GLOBAL"]
gdb_assert {[string equal $var "Global environment value"]} \
"TEST_GDB_GLOBAL found with right value"
}
# Second test with one TEST_GDB_VAR.
with_test_prefix "test2" {
gdb_test_no_output "unset env TEST_GDB_GLOBAL" \ gdb_test_no_output "unset env TEST_GDB_GLOBAL" \
"unset TEST_GDB_GLOBAL" "unset TEST_GDB_GLOBAL"
runto_main if ![run_and_count_vars] {
gdb_breakpoint $bp_line return
}
# Second test with one TEST_GDB_VAR test_num_test_vars 0 "TEST_GDB_GLOBAL is unset"
gdb_test "continue" \ }
".*Program found 0 variables starting with TEST_GDB.*" \ }
"test with TEST_GDB_GLOBAL unset" }
gdb_exit
# Clear environment in case we're doing multiple runs
unset env(TEST_GDB_GLOBAL)
test_set_unset_env
test_inherit_env_var