]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/testsuite/gdb.cp/ovldbreak.exp
Show locno for 'multi location' breakpoint hit msg+conv var $_hit_bbnum $_hit_locno...
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.cp / ovldbreak.exp
1 # Copyright (C) 1998-2022 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 this program. If not, see <http://www.gnu.org/licenses/>.
15
16 # written by Elena Zannoni (ezannoni@cygnus.com)
17 # modified by Michael Chastain (chastain@redhat.com)
18
19 # This file is part of the gdb testsuite
20 #
21 # tests for overloaded member functions. Set breakpoints on
22 # overloaded member functions
23 #
24
25 global timeout
26 set timeout 15
27 #
28 # test running programs
29 #
30
31 if { [skip_cplus_tests] } { return }
32
33 standard_testfile .cc
34
35 if {[prepare_for_testing "failed to prepare" $testfile $srcfile {debug c++}]} {
36 return -1
37 }
38
39 # set it up at a breakpoint so we can play with the variable values
40 #
41 if {![runto_main]} {
42 perror "couldn't run to breakpoint"
43 return
44 }
45
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).
49 #
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.
52 #
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.
55
56 proc take_gdb_out_of_choice_menu {} {
57 global gdb_prompt
58 gdb_test_multiple " " " " {
59 -re ".*$gdb_prompt $" {
60 }
61 timeout {
62 perror "could not resynchronize to command prompt (timeout)"
63 continue
64 }
65 }
66 }
67
68
69
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.
73 #
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.
79
80 proc set_bp_overloaded {name expectedmenu mychoice bpnumber linenumbers} {
81 global gdb_prompt hex decimal srcfile
82
83 # Get into the overload menu.
84 gdb_test_multiple "break $name" "bp menu for $name choice $mychoice" {
85 -re "$expectedmenu" {
86 pass $gdb_test_name
87
88 set any "\[^\r\n\]*"
89
90 # True if we've seen a bad breakpoint.
91 set bad_bp 0
92
93 # How many breakpoints we expect to see.
94 set expected_bps [llength $linenumbers]
95
96 # The count of seen breakpoints.
97 set seen_bps 0
98
99 # Choose my choice.
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" {
102
103 set got_num $expect_out(1,string)
104 set got_line $expect_out(2,string)
105
106 if {$seen_bps >= $expected_bps} {
107 set bad_bp 1
108 } else {
109 set linenumber [lindex $linenumbers $seen_bps]
110
111 if {$got_num != $bpnumber || $got_line != $linenumber} {
112 set bad_bp 1
113 }
114
115 incr bpnumber
116 incr seen_bps
117 }
118 exp_continue
119 }
120 -re "$gdb_prompt $" {
121 gdb_assert {!$bad_bp && $seen_bps == $expected_bps} \
122 $gdb_test_name
123 }
124 timeout {
125 fail "$gdb_test_name (timeout)"
126 take_gdb_out_of_choice_menu
127 }
128 }
129 }
130 -re ".*\r\n> " {
131 fail "$gdb_test_name (bad menu)"
132 take_gdb_out_of_choice_menu
133 }
134 -re ".*$gdb_prompt $" {
135 fail "$gdb_test_name (no menu)"
136 }
137 timeout {
138 fail "$gdb_test_name (timeout)"
139 take_gdb_out_of_choice_menu
140 }
141 }
142 }
143
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++.
147
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]
157
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 {
162 void ""
163 char 2
164 signed_char 3
165 unsigned_char 4
166 short_int 5
167 unsigned_short_int 6
168 int 7
169 unsigned_int 8
170 long_int 9
171 unsigned_long_int 10
172 float 100(.0)?
173 double 200(.0)?
174 }
175
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 "_"] " "]
183
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"]
187
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
194
195 # Store the real type into TYPES.
196 set types($type) [string trim $real_type {()}]
197
198 # Create an inverse mapping of the actual type to
199 # the synthetic type.
200 set type_map("$types($type)") $type
201 pass "detect $type"
202 }
203 }
204 }
205
206 # This is a list of the actual overloaded method arguments.
207 set overloads {}
208 foreach type $all_types {
209 lappend overloads $types($type)
210 }
211
212 # Sort this list alphabetically.
213 set overloads [lsort $overloads]
214
215 # Create the menu list.
216 set items {"cancel" "all"}
217 foreach ovld $overloads {
218 lappend items "$srcfile:foo::overload1arg\\($ovld\\)"
219 }
220 set menu_items {}
221 set idx 0
222 foreach item $items {
223 lappend menu_items ".$idx. .*$item"
224 incr idx
225 }
226 set menu_overload1arg [join $menu_items {[\r\n]*}]
227 append menu_overload1arg {[\r\n]*> $}
228
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"
232
233 # The last breakpoint created.
234 set bpnum 1
235
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"))
242 }
243
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"))]
251 }
252 gdb_test "info break" $bptable "breakpoint info (after setting one-by-one)"
253
254 # Test choice "cancel".
255 # This is copy-and-paste from set_bp_overloaded.
256
257 send_gdb "break foo::overload1arg\n"
258 gdb_expect {
259 -re "$menu_overload1arg" {
260 pass "bp menu for foo::overload1arg choice cancel"
261 # Choose cancel.
262 send_gdb "0\n"
263 gdb_expect {
264 -re "canceled\r\n$gdb_prompt $" {
265 pass "set bp on overload1arg canceled"
266 }
267 -re "cancelled\r\n$gdb_prompt $" {
268 pass "set bp on overload1arg canceled"
269 }
270 -re ".*$gdb_prompt $" {
271 fail "set bp on overload1arg canceled (bad message)"
272 }
273 timeout {
274 fail "set bp on overload1arg canceled (timeout)"
275 take_gdb_out_of_choice_menu
276 }
277 }
278 }
279 -re ".*\r\n> " {
280 fail "bp menu for foo::overload1arg choice cancel (bad menu)"
281 take_gdb_out_of_choice_menu
282 }
283 -re ".*$gdb_prompt $" {
284 fail "bp menu for foo::overload1arg choice cancel (no menu)"
285 }
286 timeout {
287 fail "bp menu for foo::overload1arg choice cancel (timeout)"
288 take_gdb_out_of_choice_menu
289 }
290 }
291
292 gdb_test "info break" $bptable "breakpoint info (after cancel)"
293
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"
298
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"))
303 }
304 set_bp_overloaded $method $menu_overload1arg \
305 "2-3" [incr bpnum] $expected_lines
306 incr bpnum
307 }
308
309 # Delete these breakpoints.
310
311 send_gdb "delete breakpoints\n"
312 gdb_expect {
313 -re "Delete all breakpoints.* $" {
314 send_gdb "y\n"
315 gdb_expect {
316 -re ".*$gdb_prompt $" {
317 pass "delete all breakpoints"
318 }
319 timeout {
320 fail "delete all breakpoints (timeout)"
321 }
322 }
323 }
324 timeout {
325 fail "delete all breakpoints (timeout)"
326 }
327 }
328
329 gdb_test "info breakpoints" "No breakpoints or watchpoints." "breakpoint info (after delete)"
330
331
332
333 # Test choice "all".
334 # This is copy-and-paste from set_bp_overloaded.
335
336 incr bpnum
337 send_gdb "break foo::overload1arg\n"
338 gdb_expect {
339 -re "$menu_overload1arg" {
340 pass "bp menu for foo::overload1arg choice all"
341 # Choose all.
342 send_gdb "1\n"
343 gdb_expect {
344 -re "Breakpoint $bpnum at $hex: foo::overload1arg. .12 locations.\r\n.*$gdb_prompt $" {
345 pass "set bp on overload1arg all"
346 }
347 -re ".*$gdb_prompt $" {
348 fail "set bp on overload1arg all (bad message)"
349 }
350 timeout {
351 fail "set bp on overload1arg all (timeout)"
352 take_gdb_out_of_choice_menu
353 }
354 }
355 }
356 -re ".*\r\n> " {
357 fail "bp menu for foo::overload1arg choice all (bad menu)"
358 take_gdb_out_of_choice_menu
359 }
360 -re ".*$gdb_prompt $" {
361 fail "bp menu for foo::overload1arg choice all (no menu)"
362 }
363 timeout {
364 fail "bp menu for foo::overload1arg choice all (timeout)"
365 take_gdb_out_of_choice_menu
366 }
367 }
368
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)]
377 }
378
379 gdb_test "info break" $bptable "breakpoint info (after setting on all)"
380
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
384
385 if {$argument == ""} {
386 set actuals ""
387 } else {
388 set actuals "arg=$argument"
389 if {[regexp {char} $argtype]} {
390 append actuals " \\'\\\\00$argument\\'"
391 }
392 }
393
394 if {[string match $argtype "void"]} {
395 set body "return $decimal;"
396 } else {
397 set body "arg = 0; return $decimal;"
398 }
399
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"
403 }
404
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 $" {
406 if $might_kfail {
407 kfail "c++/8130" "continue to bp overloaded : $argtype"
408 } else {
409 fail "continue to bp overloaded : $argtype"
410 }
411 }
412 }
413 }
414
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 {
418 void 0
419 char 1
420 signed_char 1
421 unsigned_char 1
422 short_int 1
423 unsigned_short_int 1
424 int 0
425 unsigned_int 0
426 long_int 0
427 unsigned_long_int 0
428 float 0
429 double 1
430 }
431
432 foreach type $all_types {
433 continue_to_bp_overloaded $bpnum $might_fail($type) $line($type) \
434 $type $arguments($type)
435 }
436
437 # Test breaking on an overloaded function when multiple-symbols
438 # is set to "cancel"
439 gdb_test_no_output "set multiple-symbols cancel"
440 gdb_test "break foo::foofunc" \
441 "canceled.*" \
442 "break on ambiguous symbol when multiple-symbols is set to cancel"
443
444 # Test breaking on an overloaded function when multiple-symbols
445 # is set to "all"
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"
450
451 # That's all, folks.
452
453 unset -nocomplain line types
454 gdb_continue_to_end "finish program"