1 # Copyright (C) 1998-2019 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 this program. If not, see <http://www.gnu.org/licenses/>.
16 # written by Elena Zannoni (ezannoni@cygnus.com)
17 # modified by Michael Chastain (chastain@redhat.com)
19 # This file is part of the gdb testsuite
21 # tests for overloaded member functions. Set breakpoints on
22 # overloaded member functions
28 # test running programs
31 if { [skip_cplus_tests] } { continue }
35 if {[prepare_for_testing "failed to prepare" $testfile $srcfile {debug c++}]} {
39 # set it up at a breakpoint so we can play with the variable values
42 perror "couldn't run to breakpoint"
46 # When I ask gdb to set a breakpoint on an overloaded function,
47 # gdb gives me a choice menu. I might get stuck in that choice menu
48 # (for example, if C++ name mangling is not working properly).
50 # This procedure issues a command that works at either the menu
51 # prompt or the command prompt to get back to the command prompt.
53 # Note that an empty line won't do it (it means 'repeat the previous command'
54 # at top level). A line with a single space in it works nicely.
56 proc take_gdb_out_of_choice_menu {} {
58 gdb_test_multiple " " " " {
59 -re ".*$gdb_prompt $" {
62 perror "could not resynchronize to command prompt (timeout)"
70 # This procedure sets an overloaded breakpoint.
71 # When I ask for such a breakpoint, gdb gives me a menu of 'cancel' 'all'
72 # and a bunch of choices. I then choose from that menu by number.
74 proc set_bp_overloaded {name expectedmenu mychoice bpnumber linenumber} {
75 global gdb_prompt hex srcfile
77 # Get into the overload menu.
78 send_gdb "break $name\n"
81 pass "bp menu for $name choice $mychoice"
84 send_gdb "$mychoice\n"
86 -re "Breakpoint $bpnumber at $hex: file.*$srcfile, line $linenumber.\r\n$gdb_prompt $" {
87 pass "set bp $bpnumber on $name $mychoice line $linenumber"
89 -re ".*$gdb_prompt $" {
90 fail "set bp $bpnumber on $name $mychoice line $linenumber (bad bp)"
93 fail "set bp $bpnumber on $name $mychoice line $linenumber (timeout)"
94 take_gdb_out_of_choice_menu
99 fail "bp menu for $name choice $mychoice (bad menu)"
100 take_gdb_out_of_choice_menu
102 -re ".*$gdb_prompt $" {
103 fail "bp menu for $name choice $mychoice (no menu)"
106 fail "bp menu for $name choice $mychoice (timeout)"
107 take_gdb_out_of_choice_menu
112 # Compute the expected menu for overload1arg.
113 # Note the arg type variations for void and integer types.
114 # This accommodates different versions of g++.
116 # Probe for the real types. This will do some unnecessary checking
117 # for some simple types (like "int"), but it's just easier to loop
118 # over all_types instead of calling out just the exceptions.
119 # This list /must/ remain in the same order that the methods are
120 # called in the source code. Otherwise the order in which breakpoints
121 # are hit (tested below) will be incorrect.
122 set all_types [list void char signed_char unsigned_char short_int \
123 unsigned_short_int int unsigned_int long_int \
124 unsigned_long_int float double]
126 # ARGUMENTS is an array that will map from synthetic type to argument
127 # expressions in the source code, which is of the form "arg = $decimal".
128 # ARGUMENTS stores this decimal number.
129 array set arguments {
144 unset -nocomplain line types
145 foreach type $all_types {
146 # TYPES is an array that maps the synthetic names in ALL_TYPES
147 # to the real type used in the debugger. These will be checked
148 # below and changed if the debugger thinks they are different from
149 # their default values.
150 set types($type) [join [split $type "_"] " "]
152 # LINE is an array that will map from synthetic type to line number.
153 # in the source code.
154 set line($type) [gdb_get_line_number "fo1 $type"]
156 # Probe for the actual type.
157 gdb_test_multiple "print &foo::overload1arg($types($type))" \
158 "probe $types($type)" {
159 -re ".*\<foo::.*\>.*$gdb_prompt $" {
160 regexp {<.*>} $expect_out(0,string) func
161 regexp {\(.*\)} $func real_type
163 # Store the real type into TYPES.
164 set types($type) [string trim $real_type {()}]
166 # Create an inverse mapping of the actual type to
167 # the synthetic type.
168 set type_map("$types($type)") $type
174 # This is a list of the actual overloaded method arguments.
176 foreach type $all_types {
177 lappend overloads $types($type)
180 # Sort this list alphabetically.
181 set overloads [lsort $overloads]
183 # Create the menu list.
184 set items {"cancel" "all"}
185 foreach ovld $overloads {
186 lappend items "$srcfile:foo::overload1arg\\($ovld\\)"
190 foreach item $items {
191 lappend menu_items ".$idx. .*$item"
194 set menu_overload1arg [join $menu_items {[\r\n]*}]
195 append menu_overload1arg {[\r\n]*> $}
197 # Set multiple-symbols to "ask", to allow us to test the use
198 # of the multiple-choice menu when breaking on an overloaded method.
199 gdb_test_no_output "set multiple-symbols ask"
201 # Set breakpoints on foo::overload1arg, one by one.
203 set method "foo::overload1arg"
204 for {set idx 0} {$idx < [llength $overloads]} {incr idx} {
205 set type [lindex $overloads $idx]
206 set_bp_overloaded $method $menu_overload1arg \
207 [expr {$idx + 2}] [incr bpnum] $line($type_map("$type"))
210 # Verify the breakpoints.
211 set bptable "Num\[\t \]+Type\[\t \]+Disp Enb Address\[\t \]+What.*\[\r\n]+"
212 append bptable "\[0-9\]+\[\t \]+breakpoint\[\t \]+keep\[\t \]y\[\t \]+$hex\[\t \]+in main(\\((|void)\\))? at.*$srcfile:49\[\r\n\]+"
213 append bptable "\[\t \]+breakpoint already hit 1 time\[\r\n\]+."
214 foreach ovld $overloads {
215 append bptable [format "\[0-9\]+\[\t \]+breakpoint\[\t \]+keep y\[\t \]+$hex\[\t \]+in foo::overload1arg\\(%s\\) at.*$srcfile:%d\[\r\n\]+" $ovld \
216 $line($type_map("$ovld"))]
218 gdb_test "info break" $bptable "breakpoint info (after setting one-by-one)"
220 # Test choice "cancel".
221 # This is copy-and-paste from set_bp_overloaded.
223 send_gdb "break foo::overload1arg\n"
225 -re "$menu_overload1arg" {
226 pass "bp menu for foo::overload1arg choice cancel"
230 -re "canceled\r\n$gdb_prompt $" {
231 pass "set bp on overload1arg canceled"
233 -re "cancelled\r\n$gdb_prompt $" {
234 pass "set bp on overload1arg canceled"
236 -re ".*$gdb_prompt $" {
237 fail "set bp on overload1arg canceled (bad message)"
240 fail "set bp on overload1arg canceled (timeout)"
241 take_gdb_out_of_choice_menu
246 fail "bp menu for foo::overload1arg choice cancel (bad menu)"
247 take_gdb_out_of_choice_menu
249 -re ".*$gdb_prompt $" {
250 fail "bp menu for foo::overload1arg choice cancel (no menu)"
253 fail "bp menu for foo::overload1arg choice cancel (timeout)"
254 take_gdb_out_of_choice_menu
258 gdb_test "info break" $bptable "breakpoint info (after cancel)"
260 # Delete these breakpoints.
262 send_gdb "delete breakpoints\n"
264 -re "Delete all breakpoints.* $" {
267 -re ".*$gdb_prompt $" {
268 pass "delete all breakpoints"
271 fail "delete all breakpoints (timeout)"
276 fail "delete all breakpoints (timeout)"
280 gdb_test "info breakpoints" "No breakpoints or watchpoints." "breakpoint info (after delete)"
285 # This is copy-and-paste from set_bp_overloaded.
287 send_gdb "break foo::overload1arg\n"
289 -re "$menu_overload1arg" {
290 pass "bp menu for foo::overload1arg choice all"
294 -re "Breakpoint $decimal at $hex: foo::overload1arg. .12 locations.\r\n.*$gdb_prompt $" {
295 pass "set bp on overload1arg all"
297 -re ".*$gdb_prompt $" {
298 fail "set bp on overload1arg all (bad message)"
301 fail "set bp on overload1arg all (timeout)"
302 take_gdb_out_of_choice_menu
307 fail "bp menu for foo::overload1arg choice all (bad menu)"
308 take_gdb_out_of_choice_menu
310 -re ".*$gdb_prompt $" {
311 fail "bp menu for foo::overload1arg choice all (no menu)"
314 fail "bp menu for foo::overload1arg choice all (timeout)"
315 take_gdb_out_of_choice_menu
319 # Create the breakpoint table for "info breakpoint".
320 set bptable "Num\[\t \]+Type\[\t \]+Disp Enb Address\[\t \]+What.*\[\r\n]+"
321 append bptable "\[0-9\]+\[\t \]+breakpoint\[\t \]+keep\[\t \]y\[\t \]+<MULTIPLE>.*\[\r\n\]+"
322 foreach ovld {void char signed_char unsigned_char short_int \
323 unsigned_short_int int unsigned_int long_int \
324 unsigned_long_int float double} {
325 append bptable [format "\[0-9\]+.\[0-9\]+\[\t \]+y\[\t \]+$hex\[\t \]+in foo::overload1arg\\(%s\\) at.*$srcfile:%d\[\r\n\]+" \
326 $types($ovld) $line($ovld)]
329 gdb_test "info break" $bptable "breakpoint info (after setting on all)"
331 # Run through each breakpoint.
332 proc continue_to_bp_overloaded {bpnumber might_fail line argtype argument} {
333 global gdb_prompt hex decimal srcfile
335 if {$argument == ""} {
338 set actuals "arg=$argument"
339 if {[regexp {char} $argtype]} {
340 append actuals " \\'\\\\00$argument\\'"
344 if {[string match $argtype "void"]} {
345 set body "return $decimal;"
347 set body "arg = 0; return $decimal;"
350 gdb_test_multiple "continue" "continue to bp overloaded : $argtype" {
351 -re "Continuing.\r\n\r\nBreakpoint $bpnumber, foo::overload1arg \\(this=${hex}(, )?$actuals\\) at .*$srcfile:$line\r\n$decimal\[\t \]+{ $body }.*$gdb_prompt $" {
352 pass "continue to bp overloaded : $argtype"
355 -re "Continuing.\r\n\r\nBreakpoint $bpnumber, foo::overload1arg \\(this=${hex}, arg=.*\\) at .*$srcfile:$line\r\n$decimal\[\t \]+{ $body }.*$gdb_prompt $" {
357 kfail "c++/8130" "continue to bp overloaded : $argtype"
359 fail "continue to bp overloaded : $argtype"
365 # An array which describes which of these methods might be expected
366 # to kfail on GCC 2.95. See C++/8210.
367 array set might_fail {
382 foreach type $all_types {
383 continue_to_bp_overloaded 14 $might_fail($type) $line($type) \
384 $type $arguments($type)
387 # Test breaking on an overloaded function when multiple-symbols
389 gdb_test_no_output "set multiple-symbols cancel"
390 gdb_test "break foo::foofunc" \
393 # Test breaking on an overloaded function when multiple-symbols
395 gdb_test_no_output "set multiple-symbols all"
396 gdb_test "break foo::foofunc" \
397 "Breakpoint \[0-9\]+ at ${hex}: foo::foofunc. .2 locations..*"
401 unset -nocomplain line types
402 gdb_continue_to_end "finish program"