]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/lib/gdb-test.exp
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / lib / gdb-test.exp
1 # Copyright (C) 2009-2023 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 set do_whatis_tests [gdb_batch_check "python print(gdb.type_printers)" \
111 "\\\[\\\]"]
112 if {!$do_whatis_tests} {
113 send_log "skipping 'whatis' tests - gdb too old"
114 }
115
116 # This assumes that we are three frames down from dg-test, and that
117 # it still stores the filename of the testcase in a local variable "name".
118 # A cleaner solution would require a new DejaGnu release.
119 upvar 2 name testcase
120 upvar 2 prog prog
121
122 set line [get_line_number $prog $marker]
123
124 set gdb_name $::env(GUALITY_GDB_NAME)
125 set testname "$testcase"
126 set output_file "[file rootname [file tail $prog]].exe"
127 set cmd_file "[file rootname [file tail $prog]].gdb"
128
129 global srcdir
130 set printer_code [file join $srcdir .. python libstdcxx v6 printers.py]
131 set xmethod_code [file join $srcdir .. python libstdcxx v6 xmethods.py]
132
133 global gdb_tests
134
135 set fd [open $cmd_file "w"]
136 # We don't want the system copy of the pretty-printers loaded
137 puts $fd "set auto-load no"
138 # Now that we've disabled auto-load, it's safe to set the target file
139 puts $fd "file ./$output_file"
140 # Load & register *our* copy of the pretty-printers
141 puts $fd "source $printer_code"
142 puts $fd "python register_libstdcxx_printers(None)"
143 if { $load_xmethods } {
144 # Load a& register xmethods.
145 puts $fd "source $xmethod_code"
146 puts $fd "python register_libstdcxx_xmethods(None)"
147 }
148 # And start the program
149 puts $fd "break $line"
150 puts $fd "run"
151 # So we can verify that we're using the right libs ...
152 puts $fd "info share"
153
154 set count 0
155 foreach {var result kind rexp xfail} $gdb_tests {
156 incr count
157 set gdb_var($count) $var
158 set gdb_expected($count) $result
159 if {$kind == "whatis"} {
160 if {$do_whatis_tests} {
161 set gdb_is_type($count) 1
162 set gdb_is_regexp($count) $rexp
163 set gdb_is_xfail($count) $xfail
164 set gdb_command($count) "whatis $var"
165 } else {
166 unsupported "$testname"
167 close $fd
168 return
169 }
170 } else {
171 set gdb_is_type($count) 0
172 set gdb_is_regexp($count) $rexp
173 set gdb_is_xfail($count) $xfail
174 set gdb_command($count) "print $var"
175 }
176 puts $fd $gdb_command($count)
177 }
178 set gdb_tests {}
179
180 puts $fd "quit"
181 close $fd
182
183 set res [remote_spawn target "$gdb_name -nx -nw -quiet -batch -x $cmd_file "]
184 if { $res < 0 || $res == "" } {
185 unsupported "$testname"
186 return
187 }
188
189 set test_counter 0
190 remote_expect target [timeout_value] {
191 -re {^(type|\$([0-9]+)) = ([^\n\r]*)[\n\r]+} {
192 send_log "got: $expect_out(buffer)"
193
194 incr test_counter
195 set first $expect_out(3,string)
196
197 if {$gdb_is_type($test_counter)} {
198 if {$expect_out(1,string) != "type"} {
199 error "gdb failure"
200 }
201 }
202
203 if {$gdb_is_regexp($test_counter)} {
204 set match [regexp -- $gdb_expected($test_counter) $first]
205 } else {
206 set match [expr {![string compare $first \
207 $gdb_expected($test_counter)]}]
208 }
209
210 if {$match} {
211 if {$gdb_is_xfail($test_counter)} {
212 xpass "$testname $gdb_command($test_counter)"
213 verbose " matched =>$first<="
214 } else {
215 pass "$testname $gdb_command($test_counter)"
216 }
217 } else {
218 if {$gdb_is_xfail($test_counter)} {
219 xfail "$testname $gdb_command($test_counter)"
220 } else {
221 fail "$testname $gdb_command($test_counter)"
222 verbose " got =>$first<="
223 verbose "expected =>$gdb_expected($test_counter)<="
224 }
225 }
226
227 if {$test_counter == $count} {
228 remote_close target
229 return
230 } else {
231 exp_continue
232 }
233 }
234
235 -re {Python scripting is not supported in this copy of GDB.[\n\r]+} {
236 unsupported "$testname"
237 remote_close target
238 return
239 }
240
241 -re {Error while executing Python code.[\n\r]} {
242 fail "$testname"
243 remote_close target
244 return
245 }
246
247 -re {^[^$][^\n\r]*[\n\r]+} {
248 send_log "skipping: $expect_out(buffer)"
249 exp_continue
250 }
251
252 timeout {
253 unsupported "$testname"
254 remote_close target
255 return
256 }
257 }
258
259 remote_close target
260 unsupported "$testname"
261 return
262 }
263
264 # Invoke gdb with a command and pattern-match the output.
265 proc gdb_batch_check {command pattern} {
266 set gdb_name $::env(GUALITY_GDB_NAME)
267 set cmd "$gdb_name -nw -nx -quiet -batch -ex \"$command\""
268 send_log "Spawning: $cmd\n"
269 if [catch { set res [remote_spawn target "$cmd"] } ] {
270 return 0
271 }
272 if { $res < 0 || $res == "" } {
273 return 0
274 }
275
276 remote_expect target [timeout_value] {
277 -re $pattern {
278 return 1
279 }
280
281 -re {^[^\n\r]*[\n\r]+} {
282 verbose "skipping: $expect_out(buffer)"
283 exp_continue
284 }
285
286 timeout {
287 remote_close target
288 return 0
289 }
290 }
291
292 remote_close target
293 return 0
294 }
295
296 # Check for a new-enough version of gdb. The pretty-printer tests
297 # require gdb 7.3, but we don't want to test versions, so instead we
298 # check for the python "lookup_global_symbol" method, which is in 7.3
299 # but not earlier versions.
300 # Return 1 if the version is ok, 0 otherwise.
301 proc gdb_version_check {} {
302 if { ![isnative] || [is_remote target] } { return 0 }
303
304 return [gdb_batch_check "python print(gdb.lookup_global_symbol)" \
305 "<built-in function lookup_global_symbol>"]
306 }
307
308 # Check for a version of gdb which supports xmethod tests. It is done
309 # in a manner similar to the check for a version of gdb which supports the
310 # pretty-printer tests below.
311 proc gdb_version_check_xmethods {} {
312 if { ![isnative] || [is_remote target] } { return 0 }
313
314 return [gdb_batch_check \
315 "python import gdb.xmethod; print(gdb.xmethod.XMethod)" \
316 "<class 'gdb\\.xmethod\\.XMethod'>"]
317 }
318
319 # Like dg-runtest but keep the .exe around. dg-test has an option for
320 # this but there is no way to pass it through dg-runtest.
321 proc gdb-dg-runtest {args} {
322 global dg-interpreter-batch-mode
323 set saved-dg-interpreter-batch-mode ${dg-interpreter-batch-mode}
324 set dg-interpreter-batch-mode 1
325
326 eval dg-runtest $args
327
328 set dg-interpreter-batch-mode ${saved-dg-interpreter-batch-mode}
329 }