[PATCH][gdb/testsuite] Fix readnow testing

Tom de Vries via Gdb-patches
2022-07-20T16:57:17Z

Hi,

When running the testsuite with target board readnow, the readnow proc no
longer returns 1.

Readnow used to be indicated by this line in the maint print objfiles output:
...
.gdb_index: faked for "readnow"
...
but that's no longer the case.

Fix this by updating proc readnow to recognize the new "Cooked index in use"
line.

Factor out classify_symtab_mode, and use it in all procs in gdb.exp using
"maint print objfiles".

Likewise in gdb.dwarf2/gdb-index.exp and gdb.dwarf2/gdb-index-cxx.exp.

Similar in proc mi_readnow.

Tested on x86_64-linux, with native and target board readnow.

Any comments?

Thanks,
- Tom

[gdb/testsuite] Fix readnow testing

---
gdb/testsuite/gdb.dwarf2/gdb-index-cxx.exp | 18 +++---
gdb/testsuite/gdb.dwarf2/gdb-index.exp | 44 +++++++------
gdb/testsuite/lib/gdb.exp | 99 +++++++++++++-----------------
gdb/testsuite/lib/mi-support.exp | 22 ++++---
4 files changed, 89 insertions(+), 94 deletions(-)

diff --git a/gdb/testsuite/gdb.dwarf2/gdb-index-cxx.exp b/gdb/testsuite/gdb.dwarf2/gdb-index-cxx.exp
index 26ecdec962a..d8f7b217e50 100644
--- a/gdb/testsuite/gdb.dwarf2/gdb-index-cxx.exp
+++ b/gdb/testsuite/gdb.dwarf2/gdb-index-cxx.exp
@@ -22,14 +22,14 @@ if {[prepare_for_testing "failed to prepare" "${testfile}" \
return -1
}

-# The bug was that gdb would crash when saving.
-set output_dir [standard_output_file ""]
set cmd "save gdb-index"
-gdb_test_multiple "$cmd $output_dir" $cmd {
- -re -wrap "Cannot use an index to create the index.*" {
- unsupported $gdb_test_name
- }
- -re "^$cmd \[^\r\n\]*\r\n$gdb_prompt $" {
- pass $gdb_test_name
- }
+
+set mode [classify_symtab_mode $testfile]
+if { ![string equal $mode "cooked_index"] } {
+ unsupported $cmd
+ return -1
}
+
+# The bug was that gdb would crash when saving.
+set output_dir [standard_output_file ""]
+gdb_test_no_output "$cmd $output_dir" $cmd
diff --git a/gdb/testsuite/gdb.dwarf2/gdb-index.exp b/gdb/testsuite/gdb.dwarf2/gdb-index.exp
index 5badcaccb1b..1388bf772d5 100644
--- a/gdb/testsuite/gdb.dwarf2/gdb-index.exp
+++ b/gdb/testsuite/gdb.dwarf2/gdb-index.exp
@@ -82,31 +82,25 @@ proc local_add_gdb_index { program } {
# But only if the toolchain didn't already create one: gdb doesn't support
# building an index from a program already using one.

-set test "check if index present"
-gdb_test_multiple "mt print objfiles ${testfile}" $test {
- -re "gdb_index.*${gdb_prompt} $" {
- set binfile_with_index $binfile
- }
- -re "debug_names.*${gdb_prompt} $" {
- set binfile_with_index $binfile
- }
- -re "(Psymtabs|Cooked).*${gdb_prompt} $" {
- set binfile_with_index [local_add_gdb_index $binfile]
- if { ${binfile_with_index} == "" } {
- return -1
- }
- }
- -re ".*${gdb_prompt} $" {
- fail "unrecognized symbol reader"
+set mode [classify_symtab_mode $testfile]
+if { [string equal $mode "gdb_index"] || [string equal $mode "debug_names"] } {
+ set binfile_with_index $binfile
+} elseif { [string equal $mode "cooked_index"] } {
+ set binfile_with_index [local_add_gdb_index $binfile]
+ if { ${binfile_with_index} == "" } {
+ return -1
}
+} else {
+ return -1
}

# Ok, we have a copy of $binfile with an index.
# Restart gdb and verify the index was used.

clean_restart ${binfile_with_index}
-gdb_test "mt print objfiles ${testfile}" \
- "(gdb_index|debug_names).*" \
+set mode [classify_symtab_mode $binfile_with_index]
+gdb_assert \
+ {[string equal $mode "gdb_index"] || [string equal $mode "gdb_index"]} \
"index used"

# Make gdb re-read symbols and see if .gdb_index/.debug_names still
@@ -120,9 +114,13 @@ if {[run_on_host "touch binary" touch ${binfile_with_index}]} {
return -1
}

-if ![runto_main] {
- return -1
+with_test_prefix "after symbol reloading" {
+ if ![runto_main] {
+ return -1
+ }
+
+ set mode [classify_symtab_mode $binfile_with_index]
+ gdb_assert \
+ {[string equal $mode "gdb_index"] || [string equal $mode "gdb_index"]} \
+ "index used after"
}
-gdb_test "mt print objfiles ${testfile}" \
- "(gdb_index|debug_names).*" \
- "index used after symbol reloading"
diff --git a/gdb/testsuite/lib/gdb.exp b/gdb/testsuite/lib/gdb.exp
index a8f25b5f0dd..571a7b3282e 100644
--- a/gdb/testsuite/lib/gdb.exp
+++ b/gdb/testsuite/lib/gdb.exp
@@ -8167,24 +8167,38 @@ gdb_caching_proc supports_fcf_protection {
} executable "additional_flags=-fcf-protection=full"]
}

-# Return 1 if symbols were read in using -readnow. Otherwise, return 0.
+# Return symtab reading mode.
+
+proc classify_symtab_mode { args } {
+ global decimal
+ set have_symtab 0
+ set mode ""

-proc readnow { args } {
if { [llength $args] == 1 } {
set re [lindex $args 0]
} else {
set re ""
}

- set readnow_p 0
# Given the listing from the following command can be very verbose, match
# the patterns line-by-line. This prevents timeouts from waiting for
# too much data to come at once.
- set cmd "maint print objfiles $re"
- gdb_test_multiple $cmd "" -lbl {
- -re "\r\n.gdb_index: faked for \"readnow\"" {
- # Record the we've seen the above pattern.
- set readnow_p 1
+ set cmd "maint print objfiles"
+ gdb_test_multiple "$cmd $re" "$cmd [file tail $re]" -lbl {
+ -re "\r\n\\.gdb_index: version ${decimal}(?=\r\n)" {
+ set mode "gdb_index"
+ gdb_test_lines "" $gdb_test_name ".*"
+ }
+ -re "\r\n\\.debug_names: exists(?=\r\n)" {
+ set mode "debug_names"
+ gdb_test_lines "" $gdb_test_name ".*"
+ }
+ -re "\r\nCooked index in use(?=\r\n)" {
+ set mode "cooked_index"
+ gdb_test_lines "" $gdb_test_name ".*"
+ }
+ -re "\r\nSymtabs:" {
+ set have_symtab 1
exp_continue
}
-re -wrap "" {
@@ -8192,7 +8206,21 @@ proc readnow { args } {
}
}

- return $readnow_p
+ if { [string equal $mode ""] } {
+ if { ! $have_symtab } {
+ error "No symtabs for readnow"
+ }
+ set mode "readnow"
+ }
+
+ return $mode
+}
+
+# Return 1 if symbols were read in using -readnow. Otherwise, return 0.
+
+proc readnow { args } {
+ set mode [classify_symtab_mode {*}$args]
+ return [string equal $mode "readnow"]
}

# Return index name if symbols were read in using an index.
@@ -8200,27 +8228,12 @@ proc readnow { args } {

proc have_index { objfile } {

- set res ""
- set cmd "maint print objfiles $objfile"
- gdb_test_multiple $cmd "" -lbl {
- -re "\r\n.gdb_index: faked for \"readnow\"" {
- set res ""
- exp_continue
- }
- -re "\r\n.gdb_index:" {
- set res "gdb_index"
- exp_continue
- }
- -re "\r\n.debug_names:" {
- set res "debug_names"
- exp_continue
- }
- -re -wrap "" {
- # We don't care about any other input.
- }
+ set mode [classify_symtab_mode $objfile]
+ if { [string equal $mode "gdb_index"] || [string equal $mode "debug_names"] } {
+ return $mode
}

- return $res
+ return ""
}

# Return 1 if partial symbols are available. Otherwise, return 0.
@@ -8293,38 +8306,14 @@ proc add_gdb_index { program {style ""} } {
# string (the default) means .gdb_index; "-dwarf-5" means .debug_names.

proc ensure_gdb_index { binfile {style ""} } {
- global decimal
-
set testfile [file tail $binfile]
- set test "check if index present"
- set has_index 0
- set has_readnow 0
- gdb_test_multiple "mt print objfiles ${testfile}" $test -lbl {
- -re "\r\n\\.gdb_index: version ${decimal}(?=\r\n)" {
- set has_index 1
- gdb_test_lines "" $gdb_test_name ".*"
- }
- -re "\r\n\\.debug_names: exists(?=\r\n)" {
- set has_index 1
- gdb_test_lines "" $gdb_test_name ".*"
- }
- -re "\r\n(Cooked index in use|Psymtabs)(?=\r\n)" {
- gdb_test_lines "" $gdb_test_name ".*"
- }
- -re ".gdb_index: faked for \"readnow\"" {
- set has_readnow 1
- gdb_test_lines "" $gdb_test_name ".*"
- }
- -re -wrap "" {
- fail $gdb_test_name
- }
- }
+ set mode [classify_symtab_mode $testfile]

- if { $has_index } {
+ if { [string equal $mode "gdb_index"] || [string equal $mode "debug_names"] } {
return 0
}

- if { $has_readnow } {
+ if { [string equal $mode "readnow"] } {
return -1
}

diff --git a/gdb/testsuite/lib/mi-support.exp b/gdb/testsuite/lib/mi-support.exp
index e578a7e6f9b..a496a1c7d61 100644
--- a/gdb/testsuite/lib/mi-support.exp
+++ b/gdb/testsuite/lib/mi-support.exp
@@ -676,6 +676,7 @@ proc mi_gdb_load { arg } {

proc mi_readnow { args } {
global mi_gdb_prompt
+ global decimal

if { [llength $args] == 1 } {
set re [lindex $args 0]
@@ -683,20 +684,27 @@ proc mi_readnow { args } {
set re ""
}

- set readnow_p 0
- set cmd "maint print objfiles $re"
+ set mode ""
+ set cmd "interpreter-exec mi \"maint print objfiles $re\""
send_gdb "$cmd\n"
gdb_expect {
- -re ".gdb_index: faked for ..readnow.." {
- # Record that we've seen the above pattern.
- set readnow_p 1
- exp_continue
+ -re "\r\n\\.gdb_index: version ${decimal}(?=\r\n)" {
+ set mode "gdb_index"
+ }
+ -re "\r\n\\.debug_names: exists(?=\r\n)" {
+ set mode "debug_names"
+ }
+ -re "\r\nCooked index in use(?=\r\n)" {
+ set mode "cooked_index"
}
-re "\\^done\r\n$mi_gdb_prompt$" {
}
}
+ if { [string equal $mode ""] } {
+ set mode "readnow"
+ }

- return $readnow_p
+ return [string equal $mode "readnow"]
}

# mi_gdb_test COMMAND [PATTERN [MESSAGE [IPATTERN]]] -- send a command to gdb;