[gdb/testsuite] Warn about leaked global array --- gdb/testsuite/lib/gdb.exp | 54 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/gdb/testsuite/lib/gdb.exp b/gdb/testsuite/lib/gdb.exp index f7d20bd94f..91491a1f0f 100644 --- a/gdb/testsuite/lib/gdb.exp +++ b/gdb/testsuite/lib/gdb.exp @@ -5048,6 +5048,58 @@ proc standard_testfile {args} { } } +# Returns 1 if __VAR is a global array. + +proc global_array_exists { __var } { + global $__var + return [array exists $__var] +} + +# Unset global variable __VAR. + +proc global_unset { __var } { + global $__var + unset $__var +} + +# Save global vars to variable gdb_global_vars. + +proc save_global_vars { test_file_name } { + global gdb_test_file_name + set gdb_test_file_name $test_file_name + + global gdb_global_vars + set gdb_global_vars [list] + + set gdb_global_vars [info globals] +} + +# Check global variables not in gdb_global_vars. + +proc check_global_vars { } { + global gdb_global_vars + set vars [info globals] + set skip [list "expect_out" "spawn_out"] + foreach var $vars { + set found [lsearch -exact $gdb_global_vars $var] + if { $found != -1 } { + # Already present + continue + } + set found [lsearch -exact $skip $var] + if { $found != -1 } { + continue + } + if { ![global_array_exists $var] } { + continue + } + + global gdb_test_file_name + warning "$gdb_test_file_name.exp defined global array $var" + global_unset $var + } +} + # The default timeout used when testing GDB commands. We want to use # the same timeout as the default dejagnu timeout, unless the user has # already provided a specific value (probably through a site.exp file). @@ -5177,10 +5229,12 @@ proc gdb_init { test_file_name } { global gdb_instances set gdb_instances 0 + save_global_vars $test_file_name return [default_gdb_init $test_file_name] } proc gdb_finish { } { + check_global_vars global gdbserver_reconnect_p global gdb_prompt global cleanfiles