2017-09-12 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / testsuite / lib / gcc-gdb-test.exp
blob93bd701b7480d2c8495b3895452ab992e78b08c7
1 # Copyright (C) 2009-2017 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/>.
17 # Utility for testing variable values using gdb, invoked via dg-final.
18 # Call pass if variable has the desired value, otherwise fail.
20 # Argument 0 is the line number on which to put a breakpoint
21 # Argument 1 is the name of the variable to be checked
22 # possibly prefixed with type: to get the type of the variable
23 # instead of the value of the variable (the default).
24 # Argument 2 is the expected value (or type) of the variable
25 # When asking for the value, the expected value is produced
26 # calling print on it in gdb. When asking for the type it is
27 # the literal string with extra whitespace removed.
28 # Argument 3 handles expected failures and the like
29 proc gdb-test { args } {
30 if { ![isnative] || [is_remote target] } { return }
32 if { [llength $args] >= 4 } {
33 switch [dg-process-target [lindex $args 3]] {
34 "S" { }
35 "N" { return }
36 "F" { setup_xfail "*-*-*" }
37 "P" { }
41 # This assumes that we are three frames down from dg-test, and that
42 # it still stores the filename of the testcase in a local variable "name".
43 # A cleaner solution would require a new DejaGnu release.
44 upvar 2 name testcase
45 upvar 2 prog prog
47 # The command to run on the variable
48 set arg1 [lindex $args 1]
49 if { [string equal -length 5 "type:" $arg1] == 1 } {
50 set command "ptype"
51 set var [string range $arg1 5 end]
52 } else {
53 set command "print"
54 set var $arg1
57 set gdb_name $::env(GUALITY_GDB_NAME)
58 set testname "$testcase line [lindex $args 0] [lindex $args 1] == [lindex $args 2]"
59 set output_file "[file rootname [file tail $prog]].exe"
60 set cmd_file "[file rootname [file tail $prog]].gdb"
62 set fd [open $cmd_file "w"]
63 puts $fd "break [lindex $args 0]"
64 puts $fd "run"
65 puts $fd "$command $var"
66 if { $command == "print" } {
67 # For values, let gdb interpret them by printing them.
68 puts $fd "print [lindex $args 2]"
69 } else {
70 # Since types can span multiple lines, we need an end marker.
71 puts $fd "echo TYPE_END\\n"
73 puts $fd "quit"
74 close $fd
76 send_log "Spawning: $gdb_name -nx -nw -quiet -batch -x $cmd_file ./$output_file\n"
77 set res [remote_spawn target "$gdb_name -nx -nw -quiet -batch -x $cmd_file ./$output_file"]
78 if { $res < 0 || $res == "" } {
79 unsupported "$testname"
80 file delete $cmd_file
81 return
84 remote_expect target [timeout_value] {
85 # Too old GDB
86 -re "Unhandled dwarf expression|Error in sourced command file|<unknown type in " {
87 unsupported "$testname"
88 remote_close target
89 file delete $cmd_file
90 return
92 # print var; print expected
93 -re {[\n\r]\$1 = ([^\n\r]*)[\n\r]+\$2 = ([^\n\r]*)[\n\r]} {
94 set first $expect_out(1,string)
95 set second $expect_out(2,string)
96 if { $first == $second } {
97 pass "$testname"
98 } else {
99 # We need the -- to disambiguate $first from an option,
100 # as it may be negative.
101 send_log -- "$first != $second\n"
102 fail "$testname"
104 remote_close target
105 file delete $cmd_file
106 return
108 # ptype var;
109 -re {[\n\r]type = (.*)[\n\r][\n\r]TYPE_END[\n\r]} {
110 set type $expect_out(1,string)
111 # Squash all extra whitespace/newlines that gdb might use for
112 # "pretty printing" into one so result is just one line.
113 regsub -all {[\n\r\t ]+} $type " " type
114 # Old gdb might output "long int" instead of just "long"
115 # and "short int" instead of just "short". Canonicalize.
116 regsub -all {\mlong int\M} $type "long" type
117 regsub -all {\mshort int\M} $type "short" type
118 set expected [lindex $args 2]
119 if { $type == $expected } {
120 pass "$testname"
121 } else {
122 send_log -- "$type != $expected\n"
123 fail "$testname"
125 remote_close target
126 file delete $cmd_file
127 return
129 timeout {
130 unsupported "$testname"
131 remote_close target
132 file delete $cmd_file
133 return
137 unsupported "$testname"
138 remote_close target
139 file delete $cmd_file
140 return