Improve test gdb.dwarf2/dw2-ranges-func.exp
[deliverable/binutils-gdb.git] / gdb / testsuite / gdb.dwarf2 / dw2-ranges-func.exp
CommitLineData
42a4f53d 1# Copyright 2018-2019 Free Software Foundation, Inc.
450d1e88
KB
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/>.
15load_lib dwarf.exp
16
17# Test DW_AT_ranges in the context of a subprogram scope.
18
19# This test can only be run on targets which support DWARF-2 and use gas.
20if {![dwarf2_support]} {
21 unsupported "dwarf2 support required for this test"
22 return 0
23}
24
25if [get_compiler_info] {
26 return -1
27}
28if !$gcc_compiled {
29 unsupported "gcc required for this test"
30 return 0
31}
32
5c076da4
KB
33proc do_test {suffix} {
34 global gdb_test_file_name
35 global testfile binfile srcfile srcfile2 gdb_prompt hex
36
37 # Don't use standard_testfile; we want different binaries for
38 # each suffix.
39 set testfile $gdb_test_file_name-$suffix
40 set binfile [standard_output_file ${testfile}]
41 set srcfile $testfile.c
42 set srcfile2 $testfile-dw2.S
43
44 # We need to know the size of integer and address types in order to
45 # write some of the debugging info we'd like to generate.
46 #
47 # For that, we ask GDB by debugging our test program. Any program
48 # would do, but since we already have it specifically for this
49 # testcase, might as well use that.
450d1e88 50
5c076da4
KB
51 if { [prepare_for_testing "failed to prepare" ${testfile} ${srcfile}] } {
52 return -1
53 }
450d1e88 54
5c076da4
KB
55 set asm_file [standard_output_file $srcfile2]
56 Dwarf::assemble $asm_file {
57 global srcdir subdir srcfile srcfile2
58 declare_labels integer_label volatile_label func_ranges_label cu_ranges_label L
59 set int_size [get_sizeof "int" 4]
60
61 # Find start address and length for our functions.
62 lassign [function_range main [list ${srcdir}/${subdir}/$srcfile]] \
63 main_start main_len
64 set main_end "$main_start + $main_len"
65 lassign [function_range foo [list ${srcdir}/${subdir}/$srcfile]] \
66 foo_start foo_len
67 set foo_end "$foo_start + $foo_len"
68 lassign [function_range foo_cold [list ${srcdir}/${subdir}/$srcfile]] \
69 foo_cold_start foo_cold_len
70 set foo_cold_end "$foo_cold_start + $foo_cold_len"
71 lassign [function_range bar [list ${srcdir}/${subdir}/$srcfile]] \
72 bar_start bar_len
73 set bar_end "$bar_start + $bar_len"
74 lassign [function_range baz [list ${srcdir}/${subdir}/$srcfile]] \
75 baz_start baz_len
76 set baz_end "$baz_start + $baz_len"
77
78 set e_var [gdb_target_symbol e]
79
80 cu {} {
81 compile_unit {
82 {language @DW_LANG_C}
83 {name dw-ranges-func2.c}
84 {stmt_list $L DW_FORM_sec_offset}
85 {low_pc 0 addr}
86 {ranges ${cu_ranges_label} DW_FORM_sec_offset}
87 } {
88 integer_label: DW_TAG_base_type {
89 {DW_AT_byte_size $int_size DW_FORM_sdata}
90 {DW_AT_encoding @DW_ATE_signed}
91 {DW_AT_name integer}
92 }
93 volatile_label: DW_TAG_volatile_type {
94 {type :$integer_label}
95 }
96 DW_TAG_variable {
97 {name e}
98 {external 1 flag}
99 {type :$volatile_label}
100 {location {addr $e_var} SPECIAL_expr}
101 }
102 subprogram {
103 {external 1 flag}
104 {name main}
105 {DW_AT_type :$integer_label}
106 {low_pc $main_start addr}
107 {high_pc $main_len DW_FORM_data4}
108 }
109 subprogram {
110 {external 1 flag}
111 {name foo}
112 {ranges ${func_ranges_label} DW_FORM_sec_offset}
113 }
114 subprogram {
115 {external 1 flag}
116 {name bar}
117 {low_pc $bar_start addr}
118 {high_pc $bar_len DW_FORM_data4}
119 }
120 subprogram {
121 {external 1 flag}
122 {name baz}
123 {low_pc $baz_start addr}
124 {high_pc $baz_len DW_FORM_data4}
125 }
450d1e88 126 }
5c076da4
KB
127 }
128
129 lines {version 2} L {
130 include_dir "${srcdir}/${subdir}"
131 file_name "$srcfile" 1
132
133 # Generate a line table program. An attempt was made to make it
134 # reasonably accurate as it made debugging the test case easier.
135 program {
136 {DW_LNE_set_address $main_start}
137 {DW_LNS_advance_line [expr [gdb_get_line_number "main prologue"] - 1]}
138 {DW_LNS_copy}
139 {DW_LNE_set_address main_label}
140 {DW_LNS_advance_line [expr [gdb_get_line_number "main foo call"] - [gdb_get_line_number "main prologue"]]}
141 {DW_LNS_copy}
142 {DW_LNE_set_address main_label2}
143 {DW_LNS_advance_line [expr [gdb_get_line_number "main return"] - [gdb_get_line_number "main foo call"]]}
144 {DW_LNS_copy}
145 {DW_LNE_set_address $main_end}
146 {DW_LNS_advance_line [expr [gdb_get_line_number "main end"] - [gdb_get_line_number "main return"] + 1]}
147 {DW_LNS_copy}
148 {DW_LNE_end_sequence}
149
150 {DW_LNE_set_address $foo_start}
151 {DW_LNS_advance_line [expr [gdb_get_line_number "foo prologue"] - 1] }
152 {DW_LNS_copy}
153 {DW_LNE_set_address foo_label}
154 {DW_LNS_advance_line [expr [gdb_get_line_number "foo bar call"] - [gdb_get_line_number "foo prologue"]]}
155 {DW_LNS_copy}
156 {DW_LNE_set_address foo_label2}
157 {DW_LNS_advance_line [expr [gdb_get_line_number "foo foo_cold call"] - [gdb_get_line_number "foo bar call"]]}
158 {DW_LNS_copy}
159 {DW_LNE_set_address foo_label3}
160 {DW_LNS_advance_line [expr [gdb_get_line_number "foo end"] - [gdb_get_line_number "foo foo_cold call"]]}
161 {DW_LNS_copy}
162 {DW_LNE_set_address $foo_end}
163 {DW_LNS_advance_line 1}
164 {DW_LNS_copy}
165 {DW_LNE_end_sequence}
166
167 {DW_LNE_set_address $bar_start}
168 {DW_LNS_advance_line [expr [gdb_get_line_number "bar end"] - 1]}
169 {DW_LNS_copy}
170 {DW_LNS_advance_pc $bar_len}
171 {DW_LNS_advance_line 1}
172 {DW_LNS_copy}
173 {DW_LNE_end_sequence}
174
175 {DW_LNE_set_address $baz_start}
176 {DW_LNS_advance_line [expr [gdb_get_line_number "baz end"] - 1]}
177 {DW_LNS_copy}
178 {DW_LNS_advance_pc $baz_len}
179 {DW_LNS_advance_line 1}
180 {DW_LNS_copy}
181 {DW_LNE_end_sequence}
182
183 {DW_LNE_set_address $foo_cold_start}
184 {DW_LNS_advance_line [expr [gdb_get_line_number "foo_cold prologue"] - 1]}
185 {DW_LNS_copy}
186 {DW_LNE_set_address foo_cold_label}
187 {DW_LNS_advance_line [expr [gdb_get_line_number "foo_cold baz call"] - [gdb_get_line_number "foo_cold prologue"]]}
188 {DW_LNS_copy}
189 {DW_LNE_set_address foo_cold_label2}
190 {DW_LNS_advance_line [expr [gdb_get_line_number "foo_cold end"] - [gdb_get_line_number "foo_cold baz call"]]}
191 {DW_LNS_copy}
192 {DW_LNE_set_address $foo_cold_end}
193 {DW_LNS_advance_line 1}
194 {DW_LNS_copy}
195 {DW_LNE_end_sequence}
450d1e88 196 }
5c076da4
KB
197 }
198
199 # Generate ranges data.
200 ranges {is_64 [is_64_target]} {
201 func_ranges_label: sequence {
202 {range {$foo_start } $foo_end}
203 {range {$foo_cold_start} $foo_cold_end}
450d1e88 204 }
5c076da4
KB
205 cu_ranges_label: sequence {
206 {range {$foo_start } $foo_end}
207 {range {$foo_cold_start} $foo_cold_end}
208 {range {$main_start} $main_end}
209 {range {$bar_start} $bar_end}
210 {range {$baz_start} $baz_end}
450d1e88
KB
211 }
212 }
213 }
214
5c076da4
KB
215 if { [prepare_for_testing "failed to prepare" ${testfile} \
216 [list $srcfile $asm_file] {nodebug}] } {
217 return -1
450d1e88
KB
218 }
219
5c076da4
KB
220 if ![runto_main] {
221 return -1
450d1e88 222 }
450d1e88 223
5c076da4
KB
224 set main_prologue_line_num [gdb_get_line_number "main prologue"]
225 # Do a sanity check to make sure that line number info is available.
226 gdb_test "info line main" \
227 "Line ${main_prologue_line_num} of .* starts at address .* and ends at .*"
450d1e88 228
5c076da4
KB
229 with_test_prefix "step-test-1" {
230 set bp_foo_bar [gdb_get_line_number "foo bar call"]
450d1e88 231
5c076da4
KB
232 gdb_test "break $bp_foo_bar" \
233 "Breakpoint.*at.* file .*$srcfile, line $bp_foo_bar\\." \
234 "break at call to bar"
450d1e88 235
5c076da4
KB
236 gdb_test "continue" \
237 "Continuing\\..*Breakpoint \[0-9\]+, foo \\(\\).*$bp_foo_bar\\s+bar\\s\\(\\);.*foo bar call.*" \
238 "continue to call of bar"
450d1e88 239
5c076da4
KB
240 gdb_test "step" \
241 "bar \\(\\).*bar end.*" \
242 "step into bar"
450d1e88 243
5c076da4
KB
244 gdb_test "step" \
245 "foo \\(\\).*foo foo_cold call.*" \
246 "step out of bar, back into foo"
247 }
450d1e88 248
5c076da4
KB
249 with_test_prefix "step-test-2" {
250 clean_restart ${testfile}
251 if ![runto_main] {
252 return -1
253 }
450d1e88 254
5c076da4
KB
255 # Note that the RE used for the following test will fail when the
256 # breakpoint has been set on multiple locations. E.g. "(2 locations)".
257 # This is intentional since that behavior is one of the bugs that
258 # this test case tests for.
259 gdb_test "break foo" \
260 "Breakpoint.*at.* file .*$srcfile, line \\d+\\." \
261 "break foo"
262
263 # Continue to foo. Allow execution to stop either on the prologue
264 # or on the call to bar since either behavior is acceptable though
265 # the latter is preferred.
266 set test "continue to foo"
267 gdb_test_multiple "continue" $test {
268 -re "Breakpoint \\d+, foo \\(\\).*foo prologue.*${gdb_prompt}" {
269 pass $test
270 gdb_test "step" \
271 "foo bar call .*" \
272 "step to call of bar after landing on prologue"
273 }
274 -re "Breakpoint \\d+, foo \\(\\).*foo bar call.*${gdb_prompt}" {
275 pass $test
276 }
277 }
278
279 gdb_test "step" \
280 "bar \\(\\).*bar end.*" \
281 "step into bar"
282
283 gdb_test "step" \
284 "foo \\(\\).*foo foo_cold call.*" \
285 "step out of bar, back into foo"
286 }
450d1e88 287
450d1e88
KB
288 clean_restart ${testfile}
289 if ![runto_main] {
290 return -1
291 }
292
5c076da4
KB
293 # Disassembly of foo should have multiple address ranges.
294 gdb_test_sequence "disassemble foo" "" [list \
295 "Dump of assembler code for function foo:" \
296 "Address range $hex to $hex:" \
297 " $hex <\\+0>:" \
298 "Address range $hex to $hex:" \
299 " $hex <(.+?)>:" \
300 "End of assembler dump\\." \
301 ]
302
303 set foo_cold_addr -1
304 set test "x/i foo_cold"
305 gdb_test_multiple $test $test {
306 -re " ($hex) <foo.*?>.*${gdb_prompt}" {
307 set foo_cold_addr $expect_out(1,string)
450d1e88 308 pass $test
450d1e88 309 }
5c076da4
KB
310 }
311
312 set foo_addr -1
313 set test "x/i foo"
314 gdb_test_multiple $test $test {
315 -re " ($hex) <foo.*?>.*${gdb_prompt}" {
316 set foo_addr $expect_out(1,string)
450d1e88
KB
317 pass $test
318 }
319 }
320
5c076da4 321 gdb_assert {$foo_cold_addr != $foo_addr} "foo and foo_cold are at different addresses"
450d1e88 322
5c076da4
KB
323 # This more permissive RE for "break foo" will allow a breakpoint on
324 # multiple locations to PASS. */
325 gdb_test "break foo" \
326 "Breakpoint.*at.*" \
327 "break foo"
450d1e88 328
5c076da4
KB
329 gdb_test "break baz" \
330 "Breakpoint.*at.* file .*$srcfile, line \\d+\\."
450d1e88 331
5c076da4
KB
332 gdb_test "continue" \
333 "Breakpoint \\d+, foo \\(\\).*" \
334 "continue to foo"
450d1e88 335
5c076da4 336 gdb_test_no_output "set variable e=1"
450d1e88 337
5c076da4
KB
338 # If GDB incorrectly places the foo breakpoint on multiple locations,
339 # then GDB will (incorrectly) stop in foo_cold instead of in baz.
340 gdb_test "continue" \
341 "Breakpoint \\d+, (?:$hex in )?baz \\(\\).*" \
342 "continue to baz"
343
344 with_test_prefix "no-cold-names" {
345
346 # Due to the calling sequence, this backtrace would normally
347 # show function foo_cold for frame #1. However, we don't want
348 # this to be the case due to placing it in the same block
349 # (albeit at a different range) as foo. Thus it is correct to
350 # see foo for frames #1 and #2. It is incorrect to see
351 # foo_cold at frame #1.
352 gdb_test_sequence "bt" "backtrace from baz" {
353 "\[\r\n\]#0 .*? baz \\(\\) "
354 "\[\r\n\]#1 .*? foo \\(\\) "
355 "\[\r\n\]#2 .*? foo \\(\\) "
356 "\[\r\n\]#3 .*? main \\(\\) "
357 }
450d1e88 358
5c076da4
KB
359 # Doing x/2i foo_cold should show foo_cold as the first symbolic
360 # address and an offset from foo for the second. We also check to
361 # make sure that the offset is not too large - we don't GDB to
362 # display really large offsets that would (try to) wrap around the
363 # address space.
364 set foo_cold_offset 0
365 set test "x/2i foo_cold"
366 gdb_test_multiple $test $test {
367 -re " (?:$hex) <foo_cold>.*?\n (?:$hex) <foo\[+-\](\[0-9\]+)>.*${gdb_prompt}" {
368 set foo_cold_offset $expect_out(1,string)
369 pass $test
370 }
371 }
372 gdb_assert {$foo_cold_offset <= 10000} "offset to foo_cold is not too large"
450d1e88 373
5c076da4
KB
374 # Likewise, verify that second address shown by "info line" is at
375 # and offset from foo instead of foo_cold.
376 gdb_test "info line *foo_cold" "starts at address $hex <foo_cold> and ends at $hex <foo\[+-\].*?>.*"
450d1e88 377
5c076da4 378 }
450d1e88 379
5c076da4
KB
380 with_test_prefix "step-test-3" {
381 clean_restart ${testfile}
382 if ![runto_main] {
383 return -1
384 }
450d1e88 385
5c076da4
KB
386 gdb_test "step" \
387 "foo \\(\\).*bar \\(\\);.*foo bar call.*" \
388 "step into foo from main"
450d1e88 389
5c076da4
KB
390 gdb_test "step" \
391 "bar \\(\\).*\}.* bar end.*" \
392 "step into bar from foo"
450d1e88 393
5c076da4
KB
394 gdb_test "step" \
395 "foo(_label2)? \\(\\).*foo_cold \\(\\);.*foo foo_cold call.*" \
396 "step out of bar to foo"
397
398 # The tests in the "enable_foo_cold_stepping" section, below, work
399 # with some versions of gcc, though it's not clear that they
400 # should. This test case causes foo_cold, originally a separate
401 # function invoked via a subroutine call, to be considered as part
402 # of foo via use of DW_AT_ranges. Real code that I've looked at
403 # uses a branch instruction to cause code in the "cold" range to
404 # be executed.
405 #
406 # For the moment though, these tests have been left in place, but
407 # disabled, in case we decide that making such a subroutine call
408 # is a reasonable thing to do that should also be supported by
409 # GDB.
410
411 set enable_foo_cold_stepping false
412
413 if { $enable_foo_cold_stepping } {
414 gdb_test_no_output "set variable e=1"
415
416 set test "step into foo_cold from foo"
417 gdb_test_multiple "step" $test {
418 -re "foo(_low)? \\(\\).*\{.*foo_cold prologue.*${gdb_prompt}" {
419 pass $test
420 gdb_test "step" \
421 "foo \\(\\).*baz \\(\\);.*foo_cold baz call.*" \
422 "step to baz call in foo_cold"
423
424 }
425 -re "foo(_low)? \\(\\).*baz \\(\\);.*foo_cold baz call.*${gdb_prompt}" {
426 pass $test
427 }
428 }
450d1e88 429
5c076da4
KB
430 gdb_test "step" \
431 "baz \\(\\).*\}.*baz end.*" \
432 "step into baz from foo_cold"
450d1e88 433
5c076da4
KB
434 gdb_test "step" \
435 "foo(?:_low(?:_label2)?)? \\(\\).*\}.*foo_cold end.*" \
436 "step out of baz to foo_cold"
450d1e88 437
5c076da4
KB
438 gdb_test "step" \
439 "foo(?:_label3)? \\(\\).*\}.*foo end.*" \
440 "step out of foo_cold to foo"
441 } else {
442 gdb_test "next" \
443 ".*foo end.*" \
444 "next over foo_cold call"
450d1e88
KB
445 }
446
447 gdb_test "step" \
5c076da4
KB
448 "main(?:_label2)? \\(\\).*" \
449 "step out of foo to main"
450 }
451}
450d1e88 452
5c076da4
KB
453# foreach_with_prefix could be used here, but the log file output is somewhat
454# less verbose when using an explicit "with_test_prefix".
450d1e88 455
5c076da4
KB
456foreach test_suffix { "lo-cold" "hi-cold" } {
457 with_test_prefix $test_suffix {
458 do_test $test_suffix
450d1e88 459 }
450d1e88 460}
This page took 0.180873 seconds and 4 git commands to generate.