1 # Copyright (C) 1998-2024 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 require allow_cplus_tests
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
45 # When I ask gdb to set a breakpoint on an overloaded function,
46 # gdb gives me a choice menu. I might get stuck in that choice menu
47 # (for example, if C++ name mangling is not working properly).
49 # This procedure issues a command that works at either the menu
50 # prompt or the command prompt to get back to the command prompt.
52 # Note that an empty line won't do it (it means 'repeat the previous command'
53 # at top level). A line with a single space in it works nicely.
55 proc take_gdb_out_of_choice_menu {} {
57 gdb_test_multiple " " " " {
58 -re ".*$gdb_prompt $" {
61 perror "could not resynchronize to command prompt (timeout)"
69 # This procedure sets an overloaded breakpoint. When users ask for
70 # such a breakpoint, gdb gives a menu of 'cancel' 'all' and one choice
71 # per overload. Users can then choose from that menu by number.
73 # NAME is the spec to use to create the breakpoint. EXPECTEDMENU is
74 # the expected menu. MYCHOICE is the choice selected. Can be more
75 # than one overload, e.g. "2-3". BPNUMBER is the expected next
76 # breakpoint created. LINENUMBERS is a list of line numbers, one
77 # element per expected breakpoint created.
79 proc set_bp_overloaded {name expectedmenu mychoice bpnumber linenumbers} {
80 global gdb_prompt hex decimal srcfile
82 # Get into the overload menu.
83 gdb_test_multiple "break $name" "bp menu for $name choice $mychoice" {
89 # True if we've seen a bad breakpoint.
92 # How many breakpoints we expect to see.
93 set expected_bps [llength $linenumbers]
95 # The count of seen breakpoints.
99 gdb_test_multiple "$mychoice" "set bp $bpnumber on $name $mychoice line $linenumbers" {
100 -re "Breakpoint ($decimal) at $hex: file$any$srcfile, line ($decimal).\r\n" {
102 set got_num $expect_out(1,string)
103 set got_line $expect_out(2,string)
105 if {$seen_bps >= $expected_bps} {
108 set linenumber [lindex $linenumbers $seen_bps]
110 if {$got_num != $bpnumber || $got_line != $linenumber} {
119 -re "$gdb_prompt $" {
120 gdb_assert {!$bad_bp && $seen_bps == $expected_bps} \
124 fail "$gdb_test_name (timeout)"
125 take_gdb_out_of_choice_menu
130 fail "$gdb_test_name (bad menu)"
131 take_gdb_out_of_choice_menu
133 -re ".*$gdb_prompt $" {
134 fail "$gdb_test_name (no menu)"
137 fail "$gdb_test_name (timeout)"
138 take_gdb_out_of_choice_menu
143 # Compute the expected menu for overload1arg.
144 # Note the arg type variations for void and integer types.
145 # This accommodates different versions of g++.
147 # Probe for the real types. This will do some unnecessary checking
148 # for some simple types (like "int"), but it's just easier to loop
149 # over all_types instead of calling out just the exceptions.
150 # This list /must/ remain in the same order that the methods are
151 # called in the source code. Otherwise the order in which breakpoints
152 # are hit (tested below) will be incorrect.
153 set all_types [list void char signed_char unsigned_char short_int \
154 unsigned_short_int int unsigned_int long_int \
155 unsigned_long_int float double]
157 # ARGUMENTS is an array that will map from synthetic type to argument
158 # expressions in the source code, which is of the form "arg = $decimal".
159 # ARGUMENTS stores this decimal number.
160 array set arguments {
175 unset -nocomplain line types
176 foreach type $all_types {
177 # TYPES is an array that maps the synthetic names in ALL_TYPES
178 # to the real type used in the debugger. These will be checked
179 # below and changed if the debugger thinks they are different from
180 # their default values.
181 set types($type) [join [split $type "_"] " "]
183 # LINE is an array that will map from synthetic type to line number.
184 # in the source code.
185 set line($type) [gdb_get_line_number "fo1 $type"]
187 # Probe for the actual type.
188 gdb_test_multiple "print &foo::overload1arg($types($type))" \
189 "probe $types($type)" {
190 -re ".*\<foo::.*\>.*$gdb_prompt $" {
191 regexp {<.*>} $expect_out(0,string) func
192 regexp {\(.*\)} $func real_type
194 # Store the real type into TYPES.
195 set types($type) [string trim $real_type {()}]
197 # Create an inverse mapping of the actual type to
198 # the synthetic type.
199 set type_map("$types($type)") $type
205 # This is a list of the actual overloaded method arguments.
207 foreach type $all_types {
208 lappend overloads $types($type)
211 # Sort this list alphabetically.
212 set overloads [lsort $overloads]
214 # Create the menu list.
215 set items {"cancel" "all"}
216 foreach ovld $overloads {
217 lappend items "$srcfile:foo::overload1arg\\($ovld\\)"
221 foreach item $items {
222 lappend menu_items ".$idx. .*$item"
225 set menu_overload1arg [join $menu_items {[\r\n]*}]
226 append menu_overload1arg {[\r\n]*> $}
228 # Set multiple-symbols to "ask", to allow us to test the use
229 # of the multiple-choice menu when breaking on an overloaded method.
230 gdb_test_no_output "set multiple-symbols ask"
232 # The last breakpoint created.
235 # Set breakpoints on foo::overload1arg, one by one.
236 set method "foo::overload1arg"
237 for {set idx 0} {$idx < [llength $overloads]} {incr idx} {
238 set type [lindex $overloads $idx]
239 set_bp_overloaded $method $menu_overload1arg \
240 [expr {$idx + 2}] [incr bpnum] $line($type_map("$type"))
243 # Verify the breakpoints.
244 set bptable "Num\[\t \]+Type\[\t \]+Disp Enb Address\[\t \]+What\\s*\r\n"
245 append bptable "\[0-9\]+\[\t \]+breakpoint\[\t \]+keep\[\t \]y\[\t \]+$hex\[\t \]+in main(\\((|void)\\))? at.*$srcfile:49\r\n"
246 append bptable "\[\t \]+breakpoint already hit 1 time"
247 foreach ovld $overloads {
248 append bptable [format "\r\n\[0-9\]+\[\t \]+breakpoint\[\t \]+keep y\[\t \]+$hex\[\t \]+in foo::overload1arg\\(%s\\) at.*$srcfile:%d" $ovld \
249 $line($type_map("$ovld"))]
251 gdb_test "info break" $bptable "breakpoint info, after setting one-by-one"
253 # Test choice "cancel".
254 # This is copy-and-paste from set_bp_overloaded.
256 send_gdb "break foo::overload1arg\n"
258 -re "$menu_overload1arg" {
259 pass "bp menu for foo::overload1arg choice cancel"
263 -re "canceled\r\n$gdb_prompt $" {
264 pass "set bp on overload1arg canceled"
266 -re "cancelled\r\n$gdb_prompt $" {
267 pass "set bp on overload1arg canceled"
269 -re ".*$gdb_prompt $" {
270 fail "set bp on overload1arg canceled (bad message)"
273 fail "set bp on overload1arg canceled (timeout)"
274 take_gdb_out_of_choice_menu
279 fail "bp menu for foo::overload1arg choice cancel (bad menu)"
280 take_gdb_out_of_choice_menu
282 -re ".*$gdb_prompt $" {
283 fail "bp menu for foo::overload1arg choice cancel (no menu)"
286 fail "bp menu for foo::overload1arg choice cancel (timeout)"
287 take_gdb_out_of_choice_menu
291 gdb_test "info break" $bptable "breakpoint info, after cancel"
293 # Test that if the user selects multiple entries from the option list,
294 # GDB creates one breakpoint per entry.
295 with_test_prefix "multiple breakpoints" {
296 set method "foo::overload1arg"
298 set expected_lines {}
299 for {set i 0} {$i < 2} {incr i} {
300 set type [lindex $overloads $i]
301 lappend expected_lines $line($type_map("$type"))
303 set_bp_overloaded $method $menu_overload1arg \
304 "2-3" [incr bpnum] $expected_lines
308 # Delete these breakpoints.
310 send_gdb "delete breakpoints\n"
312 -re "Delete all breakpoints, watchpoints, tracepoints, and catchpoints.* $" {
315 -re ".*$gdb_prompt $" {
316 pass "delete all breakpoints, watchpoints, tracepoints, and catchpoints"
319 fail "delete all breakpoints, watchpoints, tracepoints, and catchpoints (timeout)"
324 fail "delete all breakpoints, watchpoints, tracepoints, and catchpoints (timeout)"
328 gdb_test "info breakpoints" "No breakpoints, watchpoints, tracepoints, or catchpoints." "breakpoint info, after delete"
333 # This is copy-and-paste from set_bp_overloaded.
336 send_gdb "break foo::overload1arg\n"
338 -re "$menu_overload1arg" {
339 pass "bp menu for foo::overload1arg choice all"
343 -re "Breakpoint $bpnum at $hex: foo::overload1arg. .12 locations.\r\n.*$gdb_prompt $" {
344 pass "set bp on overload1arg all"
346 -re ".*$gdb_prompt $" {
347 fail "set bp on overload1arg all (bad message)"
350 fail "set bp on overload1arg all (timeout)"
351 take_gdb_out_of_choice_menu
356 fail "bp menu for foo::overload1arg choice all (bad menu)"
357 take_gdb_out_of_choice_menu
359 -re ".*$gdb_prompt $" {
360 fail "bp menu for foo::overload1arg choice all (no menu)"
363 fail "bp menu for foo::overload1arg choice all (timeout)"
364 take_gdb_out_of_choice_menu
368 # Create the breakpoint table for "info breakpoint".
369 set bptable "Num\[\t \]+Type\[\t \]+Disp Enb Address\[\t \]+What\\s*\r\n"
370 append bptable "\[0-9\]+\[\t \]+breakpoint\[\t \]+keep\[\t \]y\[\t \]+<MULTIPLE>\\s*"
371 foreach ovld {void char signed_char unsigned_char short_int \
372 unsigned_short_int int unsigned_int long_int \
373 unsigned_long_int float double} {
374 append bptable [format "\r\n\[0-9\]+.\[0-9\]+\[\t \]+y\[\t \]+$hex\[\t \]+in foo::overload1arg\\(%s\\) at.*$srcfile:%d" \
375 $types($ovld) $line($ovld)]
378 gdb_test "info break" $bptable "breakpoint info, after setting on all"
380 # Run through each breakpoint.
381 proc continue_to_bp_overloaded {bpnumber might_fail line argtype argument} {
382 global gdb_prompt hex decimal srcfile bkptno_num_re
384 if {$argument == ""} {
387 set actuals "arg=$argument"
388 if {[regexp {char} $argtype]} {
389 append actuals " \\'\\\\00$argument\\'"
393 if {[string match $argtype "void"]} {
394 set body "return $decimal;"
396 set body "arg = 0; return $decimal;"
399 gdb_test_multiple "continue" "continue to bp overloaded : $argtype" {
400 -re "Continuing.\r\n\r\nBreakpoint $bkptno_num_re, foo::overload1arg \\(this=${hex}(, )?$actuals\\) at .*$srcfile:$line\r\n$decimal\[\t \]+{ $body }.*$gdb_prompt $" {
401 pass "continue to bp overloaded : $argtype"
404 -re "Continuing.\r\n\r\nBreakpoint $bkptno_num_re, foo::overload1arg \\(this=${hex}, arg=.*\\) at .*$srcfile:$line\r\n$decimal\[\t \]+{ $body }.*$gdb_prompt $" {
406 kfail "c++/8130" "continue to bp overloaded : $argtype"
408 fail "continue to bp overloaded : $argtype"
414 # An array which describes which of these methods might be expected
415 # to kfail on GCC 2.95. See C++/8210.
416 array set might_fail {
431 foreach type $all_types {
432 continue_to_bp_overloaded $bpnum $might_fail($type) $line($type) \
433 $type $arguments($type)
436 # Test breaking on an overloaded function when multiple-symbols
438 gdb_test_no_output "set multiple-symbols cancel"
439 gdb_test "break foo::foofunc" \
441 "break on ambiguous symbol when multiple-symbols is set to cancel"
443 # Test breaking on an overloaded function when multiple-symbols
445 gdb_test_no_output "set multiple-symbols all"
446 gdb_test "break foo::foofunc" \
447 "Breakpoint \[0-9\]+ at ${hex}: foo::foofunc. .2 locations..*" \
448 "break on ambiguous symbol when multiple-symbols is set to all"
452 unset -nocomplain line types
453 gdb_continue_to_end "finish program"