1 # Copyright (C) 1998-2022 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] } { return }
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. When users ask for
71 # such a breakpoint, gdb gives a menu of 'cancel' 'all' and one choice
72 # per overload. Users can then choose from that menu by number.
74 # NAME is the spec to use to create the breakpoint. EXPECTEDMENU is
75 # the expected menu. MYCHOICE is the choice selected. Can be more
76 # than one overload, e.g. "2-3". BPNUMBER is the expected next
77 # breakpoint created. LINENUMBERS is a list of line numbers, one
78 # element per expected breakpoint created.
80 proc set_bp_overloaded {name expectedmenu mychoice bpnumber linenumbers} {
81 global gdb_prompt hex decimal srcfile
83 # Get into the overload menu.
84 gdb_test_multiple "break $name" "bp menu for $name choice $mychoice" {
90 # True if we've seen a bad breakpoint.
93 # How many breakpoints we expect to see.
94 set expected_bps [llength $linenumbers]
96 # The count of seen breakpoints.
100 gdb_test_multiple "$mychoice" "set bp $bpnumber on $name $mychoice line $linenumbers" {
101 -re "Breakpoint ($decimal) at $hex: file$any$srcfile, line ($decimal).\r\n" {
103 set got_num $expect_out(1,string)
104 set got_line $expect_out(2,string)
106 if {$seen_bps >= $expected_bps} {
109 set linenumber [lindex $linenumbers $seen_bps]
111 if {$got_num != $bpnumber || $got_line != $linenumber} {
120 -re "$gdb_prompt $" {
121 gdb_assert {!$bad_bp && $seen_bps == $expected_bps} \
125 fail "$gdb_test_name (timeout)"
126 take_gdb_out_of_choice_menu
131 fail "$gdb_test_name (bad menu)"
132 take_gdb_out_of_choice_menu
134 -re ".*$gdb_prompt $" {
135 fail "$gdb_test_name (no menu)"
138 fail "$gdb_test_name (timeout)"
139 take_gdb_out_of_choice_menu
144 # Compute the expected menu for overload1arg.
145 # Note the arg type variations for void and integer types.
146 # This accommodates different versions of g++.
148 # Probe for the real types. This will do some unnecessary checking
149 # for some simple types (like "int"), but it's just easier to loop
150 # over all_types instead of calling out just the exceptions.
151 # This list /must/ remain in the same order that the methods are
152 # called in the source code. Otherwise the order in which breakpoints
153 # are hit (tested below) will be incorrect.
154 set all_types [list void char signed_char unsigned_char short_int \
155 unsigned_short_int int unsigned_int long_int \
156 unsigned_long_int float double]
158 # ARGUMENTS is an array that will map from synthetic type to argument
159 # expressions in the source code, which is of the form "arg = $decimal".
160 # ARGUMENTS stores this decimal number.
161 array set arguments {
176 unset -nocomplain line types
177 foreach type $all_types {
178 # TYPES is an array that maps the synthetic names in ALL_TYPES
179 # to the real type used in the debugger. These will be checked
180 # below and changed if the debugger thinks they are different from
181 # their default values.
182 set types($type) [join [split $type "_"] " "]
184 # LINE is an array that will map from synthetic type to line number.
185 # in the source code.
186 set line($type) [gdb_get_line_number "fo1 $type"]
188 # Probe for the actual type.
189 gdb_test_multiple "print &foo::overload1arg($types($type))" \
190 "probe $types($type)" {
191 -re ".*\<foo::.*\>.*$gdb_prompt $" {
192 regexp {<.*>} $expect_out(0,string) func
193 regexp {\(.*\)} $func real_type
195 # Store the real type into TYPES.
196 set types($type) [string trim $real_type {()}]
198 # Create an inverse mapping of the actual type to
199 # the synthetic type.
200 set type_map("$types($type)") $type
206 # This is a list of the actual overloaded method arguments.
208 foreach type $all_types {
209 lappend overloads $types($type)
212 # Sort this list alphabetically.
213 set overloads [lsort $overloads]
215 # Create the menu list.
216 set items {"cancel" "all"}
217 foreach ovld $overloads {
218 lappend items "$srcfile:foo::overload1arg\\($ovld\\)"
222 foreach item $items {
223 lappend menu_items ".$idx. .*$item"
226 set menu_overload1arg [join $menu_items {[\r\n]*}]
227 append menu_overload1arg {[\r\n]*> $}
229 # Set multiple-symbols to "ask", to allow us to test the use
230 # of the multiple-choice menu when breaking on an overloaded method.
231 gdb_test_no_output "set multiple-symbols ask"
233 # The last breakpoint created.
236 # Set breakpoints on foo::overload1arg, one by one.
237 set method "foo::overload1arg"
238 for {set idx 0} {$idx < [llength $overloads]} {incr idx} {
239 set type [lindex $overloads $idx]
240 set_bp_overloaded $method $menu_overload1arg \
241 [expr {$idx + 2}] [incr bpnum] $line($type_map("$type"))
244 # Verify the breakpoints.
245 set bptable "Num\[\t \]+Type\[\t \]+Disp Enb Address\[\t \]+What.*\[\r\n]+"
246 append bptable "\[0-9\]+\[\t \]+breakpoint\[\t \]+keep\[\t \]y\[\t \]+$hex\[\t \]+in main(\\((|void)\\))? at.*$srcfile:49\[\r\n\]+"
247 append bptable "\[\t \]+breakpoint already hit 1 time\[\r\n\]+."
248 foreach ovld $overloads {
249 append bptable [format "\[0-9\]+\[\t \]+breakpoint\[\t \]+keep y\[\t \]+$hex\[\t \]+in foo::overload1arg\\(%s\\) at.*$srcfile:%d\[\r\n\]+" $ovld \
250 $line($type_map("$ovld"))]
252 gdb_test "info break" $bptable "breakpoint info (after setting one-by-one)"
254 # Test choice "cancel".
255 # This is copy-and-paste from set_bp_overloaded.
257 send_gdb "break foo::overload1arg\n"
259 -re "$menu_overload1arg" {
260 pass "bp menu for foo::overload1arg choice cancel"
264 -re "canceled\r\n$gdb_prompt $" {
265 pass "set bp on overload1arg canceled"
267 -re "cancelled\r\n$gdb_prompt $" {
268 pass "set bp on overload1arg canceled"
270 -re ".*$gdb_prompt $" {
271 fail "set bp on overload1arg canceled (bad message)"
274 fail "set bp on overload1arg canceled (timeout)"
275 take_gdb_out_of_choice_menu
280 fail "bp menu for foo::overload1arg choice cancel (bad menu)"
281 take_gdb_out_of_choice_menu
283 -re ".*$gdb_prompt $" {
284 fail "bp menu for foo::overload1arg choice cancel (no menu)"
287 fail "bp menu for foo::overload1arg choice cancel (timeout)"
288 take_gdb_out_of_choice_menu
292 gdb_test "info break" $bptable "breakpoint info (after cancel)"
294 # Test that if the user selects multiple entries from the option list,
295 # GDB creates one breakpoint per entry.
296 with_test_prefix "multiple breakpoints" {
297 set method "foo::overload1arg"
299 set expected_lines {}
300 for {set i 0} {$i < 2} {incr i} {
301 set type [lindex $overloads $i]
302 lappend expected_lines $line($type_map("$type"))
304 set_bp_overloaded $method $menu_overload1arg \
305 "2-3" [incr bpnum] $expected_lines
309 # Delete these breakpoints.
311 send_gdb "delete breakpoints\n"
313 -re "Delete all breakpoints.* $" {
316 -re ".*$gdb_prompt $" {
317 pass "delete all breakpoints"
320 fail "delete all breakpoints (timeout)"
325 fail "delete all breakpoints (timeout)"
329 gdb_test "info breakpoints" "No breakpoints or watchpoints." "breakpoint info (after delete)"
334 # This is copy-and-paste from set_bp_overloaded.
337 send_gdb "break foo::overload1arg\n"
339 -re "$menu_overload1arg" {
340 pass "bp menu for foo::overload1arg choice all"
344 -re "Breakpoint $bpnum at $hex: foo::overload1arg. .12 locations.\r\n.*$gdb_prompt $" {
345 pass "set bp on overload1arg all"
347 -re ".*$gdb_prompt $" {
348 fail "set bp on overload1arg all (bad message)"
351 fail "set bp on overload1arg all (timeout)"
352 take_gdb_out_of_choice_menu
357 fail "bp menu for foo::overload1arg choice all (bad menu)"
358 take_gdb_out_of_choice_menu
360 -re ".*$gdb_prompt $" {
361 fail "bp menu for foo::overload1arg choice all (no menu)"
364 fail "bp menu for foo::overload1arg choice all (timeout)"
365 take_gdb_out_of_choice_menu
369 # Create the breakpoint table for "info breakpoint".
370 set bptable "Num\[\t \]+Type\[\t \]+Disp Enb Address\[\t \]+What.*\[\r\n]+"
371 append bptable "\[0-9\]+\[\t \]+breakpoint\[\t \]+keep\[\t \]y\[\t \]+<MULTIPLE>.*\[\r\n\]+"
372 foreach ovld {void char signed_char unsigned_char short_int \
373 unsigned_short_int int unsigned_int long_int \
374 unsigned_long_int float double} {
375 append bptable [format "\[0-9\]+.\[0-9\]+\[\t \]+y\[\t \]+$hex\[\t \]+in foo::overload1arg\\(%s\\) at.*$srcfile:%d\[\r\n\]+" \
376 $types($ovld) $line($ovld)]
379 gdb_test "info break" $bptable "breakpoint info (after setting on all)"
381 # Run through each breakpoint.
382 proc continue_to_bp_overloaded {bpnumber might_fail line argtype argument} {
383 global gdb_prompt hex decimal srcfile bkptno_num_re
385 if {$argument == ""} {
388 set actuals "arg=$argument"
389 if {[regexp {char} $argtype]} {
390 append actuals " \\'\\\\00$argument\\'"
394 if {[string match $argtype "void"]} {
395 set body "return $decimal;"
397 set body "arg = 0; return $decimal;"
400 gdb_test_multiple "continue" "continue to bp overloaded : $argtype" {
401 -re "Continuing.\r\n\r\nBreakpoint $bkptno_num_re, foo::overload1arg \\(this=${hex}(, )?$actuals\\) at .*$srcfile:$line\r\n$decimal\[\t \]+{ $body }.*$gdb_prompt $" {
402 pass "continue to bp overloaded : $argtype"
405 -re "Continuing.\r\n\r\nBreakpoint $bkptno_num_re, foo::overload1arg \\(this=${hex}, arg=.*\\) at .*$srcfile:$line\r\n$decimal\[\t \]+{ $body }.*$gdb_prompt $" {
407 kfail "c++/8130" "continue to bp overloaded : $argtype"
409 fail "continue to bp overloaded : $argtype"
415 # An array which describes which of these methods might be expected
416 # to kfail on GCC 2.95. See C++/8210.
417 array set might_fail {
432 foreach type $all_types {
433 continue_to_bp_overloaded $bpnum $might_fail($type) $line($type) \
434 $type $arguments($type)
437 # Test breaking on an overloaded function when multiple-symbols
439 gdb_test_no_output "set multiple-symbols cancel"
440 gdb_test "break foo::foofunc" \
442 "break on ambiguous symbol when multiple-symbols is set to cancel"
444 # Test breaking on an overloaded function when multiple-symbols
446 gdb_test_no_output "set multiple-symbols all"
447 gdb_test "break foo::foofunc" \
448 "Breakpoint \[0-9\]+ at ${hex}: foo::foofunc. .2 locations..*" \
449 "break on ambiguous symbol when multiple-symbols is set to all"
453 unset -nocomplain line types
454 gdb_continue_to_end "finish program"