c446f5b122dc10552bcd7426660963bc12c0edc6
[gcc.git] / gcc / testsuite / lib / gcc-gdb-test.exp
1 # Copyright (C) 2009-2018 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 # Utility for testing variable values using gdb, invoked via dg-final.
18 # Call pass if variable has the desired value, otherwise fail.
19 #
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 { useline args } {
30 if { ![isnative] || [is_remote target] } { return }
31
32 if { [llength $args] >= 4 } {
33 switch [dg-process-target [lindex $args 3]] {
34 "S" { }
35 "N" { return }
36 "F" { setup_xfail "*-*-*" }
37 "P" { }
38 }
39 }
40
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
46
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
55 }
56
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"
61
62 set fd [open $cmd_file "w"]
63 set line [get-absolute-line $useline [lindex $args 0]]
64 puts $fd "break $line"
65 puts $fd "run"
66 puts $fd "$command $var"
67 if { $command == "print" } {
68 # For values, let gdb interpret them by printing them.
69 puts $fd "print [lindex $args 2]"
70 } else {
71 # Since types can span multiple lines, we need an end marker.
72 puts $fd "echo TYPE_END\\n"
73 }
74 puts $fd "quit"
75 close $fd
76
77 send_log "Spawning: $gdb_name -nx -nw -quiet -batch -x $cmd_file ./$output_file\n"
78 set res [remote_spawn target "$gdb_name -nx -nw -quiet -batch -x $cmd_file ./$output_file"]
79 if { $res < 0 || $res == "" } {
80 unsupported "$testname"
81 file delete $cmd_file
82 return
83 }
84
85 remote_expect target [timeout_value] {
86 # Too old GDB
87 -re "Unhandled dwarf expression|Error in sourced command file|<unknown type in " {
88 unsupported "$testname"
89 remote_close target
90 file delete $cmd_file
91 return
92 }
93 # print var; print expected
94 -re {[\n\r]\$1 = ([^\n\r]*)[\n\r]+\$2 = ([^\n\r]*)[\n\r]} {
95 set first $expect_out(1,string)
96 set second $expect_out(2,string)
97 if { $first == $second } {
98 pass "$testname"
99 } else {
100 # We need the -- to disambiguate $first from an option,
101 # as it may be negative.
102 send_log -- "$first != $second\n"
103 fail "$testname"
104 }
105 remote_close target
106 file delete $cmd_file
107 return
108 }
109 # ptype var;
110 -re {[\n\r]type = (.*)[\n\r][\n\r]TYPE_END[\n\r]} {
111 set type $expect_out(1,string)
112 # Squash all extra whitespace/newlines that gdb might use for
113 # "pretty printing" into one so result is just one line.
114 regsub -all {[\n\r\t ]+} $type " " type
115 # Old gdb might output "long int" instead of just "long"
116 # and "short int" instead of just "short". Canonicalize.
117 regsub -all {\mlong int\M} $type "long" type
118 regsub -all {\mshort int\M} $type "short" type
119 set expected [lindex $args 2]
120 if { $type == $expected } {
121 pass "$testname"
122 } else {
123 send_log -- "$type != $expected\n"
124 fail "$testname"
125 }
126 remote_close target
127 file delete $cmd_file
128 return
129 }
130 timeout {
131 unsupported "$testname"
132 remote_close target
133 file delete $cmd_file
134 return
135 }
136 }
137
138 unsupported "$testname"
139 remote_close target
140 file delete $cmd_file
141 return
142 }
143
144 # Report the gdb path and version log the .log file
145 # Argument 0 is the gdb path
146 # Argument 1 is the location where gdb is used
147 #
148 proc report_gdb { gdb loc } {
149 if { [catch { exec which $gdb } msg] } {
150 send_log "gdb not found in $loc: $msg\n"
151 return
152 }
153 set gdb [exec which $gdb]
154 send_log "gdb used in $loc: $gdb\n"
155
156 send_log "gdb used in $loc: "
157 if { [catch { exec $gdb -v } gdb_version] } {
158 send_log "getting version failed:\n"
159 } else {
160 send_log "version:\n"
161 }
162 send_log -- "---\n$gdb_version\n---\n"
163 }