| 1 |
# Copyright (C) 2009-2025 Free Software Foundation, Inc. |
| 2 |
|
| 3 |
# This program is free software; you can redistribute it and/or modify |
| 4 |
# it under the terms of the GNU General Public License as published by |
| 5 |
# the Free Software Foundation; either version 3 of the License, or |
| 6 |
# (at your option) any later version. |
| 7 |
# |
| 8 |
# This program is distributed in the hope that it will be useful, |
| 9 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 |
# GNU General Public License for more details. |
| 12 |
# |
| 13 |
# You should have received a copy of the GNU General Public License |
| 14 |
# along with GCC; see the file COPYING3. If not see |
| 15 |
# <http://www.gnu.org/licenses/>. |
| 16 |
|
| 17 |
global gdb_tests |
| 18 |
set gdb_tests {} |
| 19 |
|
| 20 |
# Scan a file for markers and fill in the gdb_marker array for that |
| 21 |
# file. Any error in this script is simply thrown; errors here are |
| 22 |
# programming errors in the test suite itself and should not be |
| 23 |
# caught. |
| 24 |
proc scan_gdb_markers {filename} { |
| 25 |
global gdb_markers |
| 26 |
|
| 27 |
if {[info exists gdb_markers($filename,-)]} { |
| 28 |
return |
| 29 |
} |
| 30 |
|
| 31 |
set fd [open $filename] |
| 32 |
set lineno 1 |
| 33 |
while {! [eof $fd]} { |
| 34 |
set line [gets $fd] |
| 35 |
if {[regexp -- "Mark (\[a-zA-Z0-9\]+)" $line ignore marker]} { |
| 36 |
set gdb_markers($filename,$marker) $lineno |
| 37 |
} |
| 38 |
incr lineno |
| 39 |
} |
| 40 |
close $fd |
| 41 |
|
| 42 |
set gdb_markers($filename,-) {} |
| 43 |
} |
| 44 |
|
| 45 |
# Find a marker in a source file, and return the marker's line number. |
| 46 |
proc get_line_number {filename marker} { |
| 47 |
global gdb_markers |
| 48 |
|
| 49 |
scan_gdb_markers $filename |
| 50 |
return $gdb_markers($filename,$marker) |
| 51 |
} |
| 52 |
|
| 53 |
proc register_gdb_test {var result kind rexp selector} { |
| 54 |
global gdb_tests |
| 55 |
|
| 56 |
set xfail 0 |
| 57 |
if {[string length $selector] > 0} { |
| 58 |
switch [dg-process-target $selector] { |
| 59 |
"N" { return } |
| 60 |
"S" { } |
| 61 |
"P" { } |
| 62 |
"F" { set xfail 1 } |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
lappend gdb_tests $var $result $kind $rexp $xfail |
| 67 |
} |
| 68 |
|
| 69 |
# Make note of a gdb test. A test consists of a variable name and an |
| 70 |
# expected result, and an optional target selector. |
| 71 |
proc note-test {var result {selector {}}} { |
| 72 |
register_gdb_test $var $result print 0 $selector |
| 73 |
} |
| 74 |
|
| 75 |
# A test that uses a regular expression. This is like note-test, but |
| 76 |
# the result is a regular expression that is matched against the |
| 77 |
# output. |
| 78 |
proc regexp-test {var result {selector {}}} { |
| 79 |
register_gdb_test $var $result print 1 $selector |
| 80 |
} |
| 81 |
|
| 82 |
# A test of 'whatis'. This tests a type rather than a variable. |
| 83 |
proc whatis-test {var result {selector {}}} { |
| 84 |
register_gdb_test $var $result whatis 0 $selector |
| 85 |
} |
| 86 |
|
| 87 |
# A test of 'whatis' that uses a regular expression. This tests a type rather |
| 88 |
# than a variable. |
| 89 |
proc whatis-regexp-test {var result {selector {}}} { |
| 90 |
register_gdb_test $var $result whatis 1 $selector |
| 91 |
} |
| 92 |
|
| 93 |
# Utility for testing variable values using gdb, invoked via dg-final. |
| 94 |
# Tests all tests indicated by note-test, whatis-test, and the regexp versions. |
| 95 |
# |
| 96 |
# Argument 0 is the marker on which to put a breakpoint |
| 97 |
# Argument 2 handles expected failures and the like |
| 98 |
proc gdb-test { marker {selector {}} {load_xmethods 0} } { |
| 99 |
if { ![isnative] || [is_remote target] } { return } |
| 100 |
|
| 101 |
if {[string length $selector] > 0} { |
| 102 |
switch [dg-process-target $selector] { |
| 103 |
"S" { } |
| 104 |
"N" { return } |
| 105 |
"F" { setup_xfail "*-*-*" } |
| 106 |
"P" { } |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
# A very old version of gdb will not have the type_printers |
| 111 |
# global. Some organizations may ship a gdb that has some default |
| 112 |
# type printers, so accept any list output as indication that the |
| 113 |
# global exists. |
| 114 |
set do_whatis_tests [gdb_batch_check "python print(gdb.type_printers)" \ |
| 115 |
"\\\[.+"] |
| 116 |
if {!$do_whatis_tests} { |
| 117 |
send_log "skipping 'whatis' tests - gdb too old" |
| 118 |
} |
| 119 |
|
| 120 |
# This assumes that we are three frames down from dg-test, and that |
| 121 |
# it still stores the filename of the testcase in a local variable "name". |
| 122 |
# A cleaner solution would require a new DejaGnu release. |
| 123 |
upvar 2 name testcase |
| 124 |
upvar 2 prog prog |
| 125 |
|
| 126 |
set line [get_line_number $prog $marker] |
| 127 |
|
| 128 |
set gdb_name $::env(GUALITY_GDB_NAME) |
| 129 |
set testname "$testcase" |
| 130 |
set output_file "[file rootname [file tail $prog]].exe" |
| 131 |
set cmd_file "[file rootname [file tail $prog]].gdb" |
| 132 |
|
| 133 |
global srcdir |
| 134 |
set printer_code [file join $srcdir .. python libstdcxx v6 printers.py] |
| 135 |
set xmethod_code [file join $srcdir .. python libstdcxx v6 xmethods.py] |
| 136 |
|
| 137 |
global gdb_tests |
| 138 |
|
| 139 |
set fd [open $cmd_file "w"] |
| 140 |
# We don't want the system copy of the pretty-printers loaded |
| 141 |
puts $fd "set auto-load no" |
| 142 |
# Now that we've disabled auto-load, it's safe to set the target file |
| 143 |
puts $fd "file ./$output_file" |
| 144 |
# See the full backtrace of any failures. |
| 145 |
puts $fd "set python print-stack full" |
| 146 |
# Load & register *our* copy of the pretty-printers |
| 147 |
puts $fd "source $printer_code" |
| 148 |
puts $fd "python register_libstdcxx_printers(None)" |
| 149 |
if { $load_xmethods } { |
| 150 |
# Load a& register xmethods. |
| 151 |
puts $fd "source $xmethod_code" |
| 152 |
puts $fd "python register_libstdcxx_xmethods(None)" |
| 153 |
} |
| 154 |
# And start the program |
| 155 |
puts $fd "break $line" |
| 156 |
puts $fd "run" |
| 157 |
# So we can verify that we're using the right libs ... |
| 158 |
puts $fd "info share" |
| 159 |
|
| 160 |
set count 0 |
| 161 |
foreach {var result kind rexp xfail} $gdb_tests { |
| 162 |
incr count |
| 163 |
set gdb_var($count) $var |
| 164 |
set gdb_expected($count) $result |
| 165 |
if {$kind == "whatis"} { |
| 166 |
if {$do_whatis_tests} { |
| 167 |
set gdb_is_type($count) 1 |
| 168 |
set gdb_is_regexp($count) $rexp |
| 169 |
set gdb_is_xfail($count) $xfail |
| 170 |
set gdb_command($count) "whatis $var" |
| 171 |
} else { |
| 172 |
unsupported "$testname" |
| 173 |
close $fd |
| 174 |
return |
| 175 |
} |
| 176 |
} else { |
| 177 |
set gdb_is_type($count) 0 |
| 178 |
set gdb_is_regexp($count) $rexp |
| 179 |
set gdb_is_xfail($count) $xfail |
| 180 |
set gdb_command($count) "print $var" |
| 181 |
} |
| 182 |
puts $fd $gdb_command($count) |
| 183 |
} |
| 184 |
set gdb_tests {} |
| 185 |
|
| 186 |
puts $fd "quit" |
| 187 |
close $fd |
| 188 |
|
| 189 |
set res [remote_spawn target "$gdb_name -nx -nw -quiet -batch -x $cmd_file "] |
| 190 |
if { $res < 0 || $res == "" } { |
| 191 |
unsupported "$testname" |
| 192 |
return |
| 193 |
} |
| 194 |
|
| 195 |
set test_counter 0 |
| 196 |
remote_expect target [timeout_value] { |
| 197 |
-re {^[\n\r]*(type|\$([0-9]+)) = ([^\n\r]*)[\n\r]+} { |
| 198 |
# Escape newlines so that we can print them. |
| 199 |
set escaped [string map {"\n" "\\n"} $expect_out(buffer)] |
| 200 |
set escaped2 [string map {"\r" "\\r"} $escaped] |
| 201 |
send_log "got: $escaped2" |
| 202 |
|
| 203 |
incr test_counter |
| 204 |
set first $expect_out(3,string) |
| 205 |
|
| 206 |
if {$gdb_is_type($test_counter)} { |
| 207 |
if {$expect_out(1,string) != "type"} { |
| 208 |
error "gdb failure" |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
if {$gdb_is_regexp($test_counter)} { |
| 213 |
set match [regexp -- $gdb_expected($test_counter) $first] |
| 214 |
} else { |
| 215 |
set match [expr {![string compare $first \ |
| 216 |
$gdb_expected($test_counter)]}] |
| 217 |
} |
| 218 |
|
| 219 |
if {$match} { |
| 220 |
if {$gdb_is_xfail($test_counter)} { |
| 221 |
xpass "$testname $gdb_command($test_counter)" |
| 222 |
verbose " matched =>$first<=" |
| 223 |
} else { |
| 224 |
pass "$testname $gdb_command($test_counter)" |
| 225 |
} |
| 226 |
} else { |
| 227 |
if {$gdb_is_xfail($test_counter)} { |
| 228 |
xfail "$testname $gdb_command($test_counter)" |
| 229 |
} else { |
| 230 |
fail "$testname $gdb_command($test_counter)" |
| 231 |
verbose " got =>$first<=" |
| 232 |
verbose "expected =>$gdb_expected($test_counter)<=" |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
if {$test_counter == $count} { |
| 237 |
remote_close target |
| 238 |
return |
| 239 |
} else { |
| 240 |
exp_continue |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
-re {Python scripting is not supported in this copy of GDB.[\n\r]+} { |
| 245 |
unsupported "$testname" |
| 246 |
remote_close target |
| 247 |
return |
| 248 |
} |
| 249 |
|
| 250 |
-re {Error while executing Python code.[\n\r]} { |
| 251 |
fail "$testname" |
| 252 |
remote_close target |
| 253 |
return |
| 254 |
} |
| 255 |
|
| 256 |
-re {^[^$][^\n\r]*[\n\r]+} { |
| 257 |
# Escape newlines so that we can print them. |
| 258 |
set escaped [string map {"\n" "\\n"} $expect_out(buffer)] |
| 259 |
set escaped2 [string map {"\r" "\\r"} $escaped] |
| 260 |
send_log "skipping: $escaped2" |
| 261 |
exp_continue |
| 262 |
} |
| 263 |
|
| 264 |
timeout { |
| 265 |
unsupported "$testname" |
| 266 |
remote_close target |
| 267 |
return |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
remote_close target |
| 272 |
unsupported "$testname" |
| 273 |
return |
| 274 |
} |
| 275 |
|
| 276 |
# Invoke gdb with a command and pattern-match the output. |
| 277 |
proc gdb_batch_check {command pattern} { |
| 278 |
set gdb_name $::env(GUALITY_GDB_NAME) |
| 279 |
set cmd "$gdb_name -nw -nx -quiet -batch -ex \"$command\"" |
| 280 |
send_log "Spawning: $cmd\n" |
| 281 |
if [catch { set res [remote_spawn target "$cmd"] } ] { |
| 282 |
return 0 |
| 283 |
} |
| 284 |
if { $res < 0 || $res == "" } { |
| 285 |
return 0 |
| 286 |
} |
| 287 |
|
| 288 |
remote_expect target [timeout_value] { |
| 289 |
-re $pattern { |
| 290 |
return 1 |
| 291 |
} |
| 292 |
|
| 293 |
-re {^[^\n\r]*[\n\r]+} { |
| 294 |
verbose "skipping: $expect_out(buffer)" |
| 295 |
exp_continue |
| 296 |
} |
| 297 |
|
| 298 |
timeout { |
| 299 |
remote_close target |
| 300 |
return 0 |
| 301 |
} |
| 302 |
} |
| 303 |
|
| 304 |
remote_close target |
| 305 |
return 0 |
| 306 |
} |
| 307 |
|
| 308 |
# Check for a new-enough version of gdb. The pretty-printer tests |
| 309 |
# require gdb 7.3, but we don't want to test versions, so instead we |
| 310 |
# check for the python "lookup_global_symbol" method, which is in 7.3 |
| 311 |
# but not earlier versions. |
| 312 |
# Return 1 if the version is ok, 0 otherwise. |
| 313 |
proc gdb_version_check {} { |
| 314 |
if { ![isnative] || [is_remote target] } { return 0 } |
| 315 |
|
| 316 |
return [gdb_batch_check "python print(gdb.lookup_global_symbol)" \ |
| 317 |
"<built-in function lookup_global_symbol>"] |
| 318 |
} |
| 319 |
|
| 320 |
# Check for a version of gdb which supports xmethod tests. It is done |
| 321 |
# in a manner similar to the check for a version of gdb which supports the |
| 322 |
# pretty-printer tests below. |
| 323 |
proc gdb_version_check_xmethods {} { |
| 324 |
if { ![isnative] || [is_remote target] } { return 0 } |
| 325 |
|
| 326 |
return [gdb_batch_check \ |
| 327 |
"python import gdb.xmethod; print(gdb.xmethod.XMethod)" \ |
| 328 |
"<class 'gdb\\.xmethod\\.XMethod'>"] |
| 329 |
} |
| 330 |
|
| 331 |
# Like dg-runtest but keep the .exe around. dg-test has an option for |
| 332 |
# this but there is no way to pass it through dg-runtest. |
| 333 |
proc gdb-dg-runtest {args} { |
| 334 |
global dg-interpreter-batch-mode |
| 335 |
set saved-dg-interpreter-batch-mode ${dg-interpreter-batch-mode} |
| 336 |
set dg-interpreter-batch-mode 1 |
| 337 |
|
| 338 |
eval dg-runtest $args |
| 339 |
|
| 340 |
set dg-interpreter-batch-mode ${saved-dg-interpreter-batch-mode} |
| 341 |
} |