Re: [PATCH 3/3][gdb/testsuite] Warn about leaked global array

Tom de Vries
2020-06-03T10:09:49Z

On 03-06-2020 11:38, Tom de Vries wrote:
> On 03-06-2020 10:47, Tom de Vries wrote:
>> ERROR: can't read "mi_gdb_prompt": no such variable
>> while executing
>> "expect {
>> -i exp95 -timeout 10
>> -re "~\"GNU.*\r\n~\".*$mi_gdb_prompt$" {
>> # We have a new format mi startup prompt. If we are
>> # running mi1,..."
>> ("uplevel" body line 1)
>> invoked from within
>> "uplevel $body" TCL READ VARNAME can't read "mi_gdb_prompt": no such
>> variable
>
> So, the following happens:
> - a test-case imports mi-support.exp using load_lib
> - mi-support.exp sets mi_gdb_prompt
> - the test-case finishes and the new global mi_gdb_prompt is unset,
> because it was not set before the test-case
> - a next test-case imports mi-support.exp using load_lib
> - load_lib sees that the file already has been loaded, so it skips it
> - mi_gdb_prompt remains unset
> - the test-case uses mi_gdb_prompt, and we have an error.

This naive solution seems to work.

Perhaps we can override load_lib to do the same, automatically.

Thanks,
- Tom

diff --git a/gdb/testsuite/lib/gdb.exp b/gdb/testsuite/lib/gdb.exp
index 66de9d6be3..e75842c83e 100644
--- a/gdb/testsuite/lib/gdb.exp
+++ b/gdb/testsuite/lib/gdb.exp
@@ -5099,6 +5099,13 @@ set banned_traced 0
# global not in this list is deleted.
array set gdb_known_globals {}

+array set gdb_persistent_globals {}
+
+proc gdb_persistent_global { varname } {
+ global gdb_persistent_globals
+ set gdb_persistent_globals($varname) 1
+}
+
# Setup the GDB_KNOWN_GLOBALS array with the names of all current
# global variables.
proc gdb_setup_known_globals {} {
@@ -5114,10 +5121,14 @@ proc gdb_setup_known_globals {} {
# GDB_KNOWN_GLOBALS array is unset, this ensures we don't "leak"
# globals from one test script to another.
proc gdb_cleanup_globals {} {
- global gdb_known_globals
+ global gdb_known_globals gdb_persistent_globals

foreach varname [info globals] {
if {![info exists gdb_known_globals($varname)]} {
+ if { [info exists gdb_persistent_globals($varname)] } {
+ continue
+ }
+ verbose -log "gdb_cleanup_globals: deleting $varname"
uplevel #0 unset $varname
}
}--
diff --git a/gdb/testsuite/lib/mi-support.exp
b/gdb/testsuite/lib/mi-support.exp
index 1e59919ab4..2804cfc308 100644
--- a/gdb/testsuite/lib/mi-support.exp
+++ b/gdb/testsuite/lib/mi-support.exp
@@ -25,6 +25,7 @@ global mi_gdb_prompt
if ![info exists mi_gdb_prompt] then {
set mi_gdb_prompt "\[(\]gdb\[)\] \r\n"
}
+gdb_persistent_global mi_gdb_prompt

global mi_inferior_tty_name

@@ -39,11 +40,16 @@ global gdb_main_spawn_id
global mi_spawn_id

set MIFLAGS "-i=mi"
+gdb_persistent_global MIFLAGS

set thread_selected_re "=thread-selected,id=\"\[0-9\]+\"\r\n"
+gdb_persistent_global thread_selected_re
set gdbindex_warning_re "&\"warning: Skipping \[^\r\n\]+ \.gdb_index
section in \[^\r\n\]+\"\r\n(?:&\"\\\\n\"\r\n
)?"
+gdb_persistent_global gdbindex_warning_re
set library_loaded_re
"=library-loaded\[^\n\]+\"\r\n(?:$gdbindex_warning_re)?"
+gdb_persistent_global library_loaded_re
set breakpoint_re
"=(?:breakpoint-created|breakpoint-deleted)\[^\n\]+\"\r\n"
+gdb_persistent_global breakpoint_re

#
# mi_gdb_exit -- exit the GDB, killing the target program if necessary