1 # Copyright (C) 2009-2020 Free Software Foundation, Inc.
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.
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.
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/>.
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
24 proc scan_gdb_markers {filename} {
27 if {[info exists gdb_markers($filename,-)]} {
31 set fd [open $filename]
35 if {[regexp -- "Mark (\[a-zA-Z0-9\]+)" $line ignore marker]} {
36 set gdb_markers($filename,$marker) $lineno
42 set gdb_markers($filename,-) {}
45 # Find a marker in a source file, and return the marker's line number.
46 proc get_line_number {filename marker} {
49 scan_gdb_markers $filename
50 return $gdb_markers($filename,$marker)
53 # Make note of a gdb test. A test consists of a variable name and an
55 proc note-test {var result} {
58 lappend gdb_tests $var $result print 0
61 # A test that uses a regular expression. This is like note-test, but
62 # the result is a regular expression that is matched against the
64 proc regexp-test {var result} {
67 lappend gdb_tests $var $result print 1
70 # A test of 'whatis'. This tests a type rather than a variable.
71 proc whatis-test {var result} {
74 lappend gdb_tests $var $result whatis 0
77 # A test of 'whatis' that uses a regular expression. This tests a type rather
79 proc whatis-regexp-test {var result} {
82 lappend gdb_tests $var $result whatis 1
85 # Utility for testing variable values using gdb, invoked via dg-final.
86 # Tests all tests indicated by note-test and regexp-test.
88 # Argument 0 is the marker on which to put a breakpoint
89 # Argument 2 handles expected failures and the like
90 proc gdb-test { marker {selector {}} {load_xmethods 0} } {
91 if { ![isnative] || [is_remote target] } { return }
93 if {[string length $selector] > 0} {
94 switch [dg-process-target $selector] {
97 "F" { setup_xfail "*-*-*" }
102 set do_whatis_tests [gdb_batch_check "python print(gdb.type_printers)" \
104 if {!$do_whatis_tests} {
105 send_log "skipping 'whatis' tests - gdb too old"
108 # This assumes that we are three frames down from dg-test, and that
109 # it still stores the filename of the testcase in a local variable "name".
110 # A cleaner solution would require a new DejaGnu release.
111 upvar 2 name testcase
114 set line [get_line_number $prog $marker]
116 set gdb_name $::env(GUALITY_GDB_NAME)
117 set testname "$testcase"
118 set output_file "[file rootname [file tail $prog]].exe"
119 set cmd_file "[file rootname [file tail $prog]].gdb"
122 set printer_code [file join $srcdir .. python libstdcxx v6 printers.py]
123 set xmethod_code [file join $srcdir .. python libstdcxx v6 xmethods.py]
127 set fd [open $cmd_file "w"]
128 # We don't want the system copy of the pretty-printers loaded
129 puts $fd "set auto-load no"
130 # Now that we've disabled auto-load, it's safe to set the target file
131 puts $fd "file ./$output_file"
132 # Load & register *our* copy of the pretty-printers
133 puts $fd "source $printer_code"
134 puts $fd "python register_libstdcxx_printers(None)"
135 if { $load_xmethods } {
136 # Load a& register xmethods.
137 puts $fd "source $xmethod_code"
138 puts $fd "python register_libstdcxx_xmethods(None)"
140 # And start the program
141 puts $fd "break $line"
143 # So we can verify that we're using the right libs ...
144 puts $fd "info share"
147 foreach {var result kind rexp} $gdb_tests {
149 set gdb_var($count) $var
150 set gdb_expected($count) $result
151 if {$kind == "whatis"} {
152 if {$do_whatis_tests} {
153 set gdb_is_type($count) 1
154 set gdb_is_regexp($count) $rexp
155 set gdb_command($count) "whatis $var"
157 unsupported "$testname"
162 set gdb_is_type($count) 0
163 set gdb_is_regexp($count) $rexp
164 set gdb_command($count) "print $var"
166 puts $fd $gdb_command($count)
173 set res [remote_spawn target "$gdb_name -nx -nw -quiet -batch -x $cmd_file "]
174 if { $res < 0 || $res == "" } {
175 unsupported "$testname"
180 remote_expect target [timeout_value] {
181 -re {^(type|\$([0-9]+)) = ([^\n\r]*)[\n\r]+} {
182 send_log "got: $expect_out(buffer)"
185 set first $expect_out(3,string)
187 if {$gdb_is_type($test_counter)} {
188 if {$expect_out(1,string) != "type"} {
193 if {$gdb_is_regexp($test_counter)} {
194 set match [regexp -- $gdb_expected($test_counter) $first]
196 set match [expr {![string compare $first \
197 $gdb_expected($test_counter)]}]
201 pass "$testname $gdb_command($test_counter)"
203 fail "$testname $gdb_command($test_counter)"
204 verbose " got =>$first<="
205 verbose "expected =>$gdb_expected($test_counter)<="
208 if {$test_counter == $count} {
216 -re {Python scripting is not supported in this copy of GDB.[\n\r]+} {
217 unsupported "$testname"
222 -re {Error while executing Python code.[\n\r]} {
228 -re {^[^$][^\n\r]*[\n\r]+} {
229 send_log "skipping: $expect_out(buffer)"
234 unsupported "$testname"
241 unsupported "$testname"
245 # Invoke gdb with a command and pattern-match the output.
246 proc gdb_batch_check {command pattern} {
247 set gdb_name $::env(GUALITY_GDB_NAME)
248 set cmd "$gdb_name -nw -nx -quiet -batch -ex \"$command\""
249 send_log "Spawning: $cmd\n"
250 if [catch { set res [remote_spawn target "$cmd"] } ] {
253 if { $res < 0 || $res == "" } {
257 remote_expect target [timeout_value] {
262 -re {^[^\n\r]*[\n\r]+} {
263 verbose "skipping: $expect_out(buffer)"
277 # Check for a new-enough version of gdb. The pretty-printer tests
278 # require gdb 7.3, but we don't want to test versions, so instead we
279 # check for the python "lookup_global_symbol" method, which is in 7.3
280 # but not earlier versions.
281 # Return 1 if the version is ok, 0 otherwise.
282 proc gdb_version_check {} {
283 return [gdb_batch_check "python print(gdb.lookup_global_symbol)" \
284 "<built-in function lookup_global_symbol>"]
287 # Check for a version of gdb which supports xmethod tests. It is done
288 # in a manner similar to the check for a version of gdb which supports the
289 # pretty-printer tests below.
290 proc gdb_version_check_xmethods {} {
291 return [gdb_batch_check \
292 "python import gdb.xmethod; print(gdb.xmethod.XMethod)" \
293 "<class 'gdb\\.xmethod\\.XMethod'>"]
296 # Like dg-runtest but keep the .exe around. dg-test has an option for
297 # this but there is no way to pass it through dg-runtest.
298 proc gdb-dg-runtest {args} {
299 global dg-interpreter-batch-mode
300 set saved-dg-interpreter-batch-mode ${dg-interpreter-batch-mode}
301 set dg-interpreter-batch-mode 1
303 eval dg-runtest $args
305 set dg-interpreter-batch-mode ${saved-dg-interpreter-batch-mode}