1# Copyright 2012-2020 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# Note that the testcase gdb.dwarf2/dw2-inline-break.exp largely
17# mirrors this testcase, and should be updated if this testcase is
18# changed.
19
20standard_testfile
21
22if { [prepare_for_testing "failed to prepare" $testfile $srcfile \
23          {debug additional_flags=-Winline}] } {
24    return -1
25}
26
27# Return a string that may be used to match the output of "info break NUM".
28#
29# Optional arguments:
30#
31# source    - the name of the source file
32# func      - the name of the function
33# disp      - the event disposition
34# enabled   - enable state
35# locs      - number of locations
36# line      - source line number (ignored without -source)
37
38proc break_info_1 {num args} {
39    global decimal
40
41    # Column delimiter
42    set c {[\t ]+}
43
44    # Row delimiter
45    set end {[\r\n \t]+}
46
47    # Table header
48    set header "[join [list Num Type Disp Enb Address What] ${c}]"
49
50    # Get/configure any optional parameters.
51    parse_args [list {source ""} {func ".*"} {disp "keep"} \
52		    {enabled "y"} {locs 1} [list line $decimal] \
53		    {type "breakpoint"}]
54
55    if {$source != ""} {
56	set source "$source:$line"
57    }
58
59    # Result starts with the standard header.
60    set result "$header${end}"
61
62    # Set up for multi-location breakpoint marker.
63    if {$locs == 1} {
64	set multi ".*"
65    } else {
66	set multi "<MULTIPLE>${end}"
67    }
68    append result "[join [list $num $type $disp $enabled $multi] $c]"
69
70    # Add location info.
71    for {set i 1} {$i <= $locs} {incr i} {
72	if {$locs > 1} {
73	    append result "[join [list $num.$i $enabled] $c].*"
74	}
75
76	#  Add function/source file info.
77	append result "in $func at .*$source${end}"
78    }
79
80    return $result
81}
82
83#
84# func1 is a static inlined function that is called once.
85# The result should be a single-location breakpoint.
86#
87gdb_test "break func1" \
88    "Breakpoint.*at.* file .*$srcfile, line.*"
89
90#
91# func2 is a non-static inlined function that is called once.
92# The result should be a breakpoint with two locations: the
93# out-of-line function and the single inlined instance.
94#
95gdb_test "break func2" \
96    "Breakpoint.*at.*func2.*\\(2 locations\\)"
97
98#
99# func3b is a static inlined function that is called once from
100# within another static inlined function.  The result should be
101# a single-location breakpoint.
102#
103gdb_test "break func3b" \
104    "Breakpoint.*at.* file .*$srcfile, line.*"
105
106#
107# func4b is a static inlined function that is called once from
108# within a non-static inlined function.  The result should be
109# a breakpoint with two locations: the inlined instance within
110# the inlined call to func4a in main, and the inlined instance
111# within the out-of-line func4a.
112#
113gdb_test "break func4b" \
114    "Breakpoint.*at.*func4b.*\\(2 locations\\)"
115
116#
117# func5b is a non-static inlined function that is called once
118# from within a static inlined function.  The result should be a
119# breakpoint with two locations: the out-of-line function and the
120# inlined instance within the inlined call to func5a in main.
121#
122gdb_test "break func5b" \
123    "Breakpoint.*at.*func5b.*\\(2 locations\\)"
124#
125# func6b is a non-static inlined function that is called once from
126# within another non-static inlined function.  The result should be
127# a breakpoint with three locations: the out-of-line function, the
128# inlined instance within the out-of-line func6a, and the inlined
129# instance within the inlined call to func6a in main,
130#
131gdb_test "break func6b" \
132    "Breakpoint.*at.*func6b.*\\(3 locations\\)"
133
134#
135# func7b is a static inlined function that is called twice: once from
136# func7a, and once from main.  The result should be a breakpoint with
137# two locations: the inlined instance within the inlined instance of
138# func7a, and the inlined instance within main.
139#
140gdb_test "break func7b" \
141    "Breakpoint.*at.*func7b.*\\(2 locations\\)"
142
143#
144# func8b is a non-static inlined function that is called twice: once
145# func8a, and once from main.  The result should be a breakpoint with
146# three locations: the out-of-line function, the inlined instance
147# within the inlined instance of func7a, and the inlined instance
148# within main.
149#
150gdb_test "break func8b" \
151    "Breakpoint.*at.*func8b.*\\(3 locations\\)"
152
153#
154# func1 is a static inlined function.  The result should be that no
155# symbol is found to print.
156#
157gdb_test "print func1" \
158    "No symbol \"func1\" in current context."
159
160#
161# func2 is a non-static inlined function.  The result should be that
162# one symbol is found to print, and that the printed symbol is called
163# "func2".  Note that this does not cover the failure case that two
164# symbols were found, but that gdb chose the out-of-line copy to
165# print, but if this was failing the "print func1" test would likely
166# fail instead.
167#
168gdb_test "print func2" \
169    "\\\$.* = {int \\(int\\)} .* <func2>"
170
171# Test that "info break" reports the location of the breakpoints "inside"
172# the inlined functions
173
174set results(1) [break_info_1 1 -source $srcfile -func "func1"]
175set results(2) [break_info_1 2 -locs 2 -source $srcfile -func "func2"]
176set results(3) [break_info_1 3 -source $srcfile -func "func3b"]
177set results(4) [break_info_1 4 -locs 2 -source $srcfile -func "func4b"]
178set results(5) [break_info_1 5 -locs 2 -source $srcfile -func "func5b"]
179set results(6) [break_info_1 6 -locs 3 -source $srcfile -func "func6b"]
180set results(7) [break_info_1 7 -locs 2 -source $srcfile -func "func7b"]
181set results(8) [break_info_1 8 -locs 3 -source $srcfile -func "func8b"]
182
183for {set i 1} {$i <= [array size results]} {incr i} {
184    send_log "Expecting: $results($i)\n"
185    gdb_test "info break $i" $results($i)
186}
187
188# Test "permanent" and "temporary" breakpoints.
189foreach_with_prefix cmd [list "break" "tbreak"] {
190
191    # Start with a clean state.
192    delete_breakpoints
193    if {![runto main]} {
194	untested "could not run to main"
195	return -1
196    }
197
198    # Assemble flags to pass to gdb_breakpoint.  Lame but this is just
199    # a test suite!
200    set break_flags "message"
201    if {[string match $cmd "tbreak"]} {
202	lappend break_flags "temporary"
203    }
204
205    # Insert breakpoints for all inline_func? and not_inline_func? and check
206    # that we actually stop where we think we should.
207    for {set i 1} {$i < 4} {incr i} {
208	foreach inline {"not_inline" "inline"} {
209	    eval gdb_breakpoint "${inline}_func$i" $break_flags
210	}
211    }
212
213    set ws {[\r\n\t ]+}
214    set backtrace [list "(in|at)? main"]
215    for {set i 3} {$i > 0} {incr i -1} {
216
217	foreach inline {"not_inline" "inline"} {
218
219	    # Check that we stop at the correct location and print out
220	    # the (possibly) inlined frames.
221	    set num [gdb_get_line_number "/* ${inline}_func$i  */"]
222	    set pattern ".*$srcfile:$num${ws}.*$num${ws}int y = $decimal;"
223	    append pattern "${ws}/\\\* ${inline}_func$i  \\\*/"
224	    send_log "Expecting $pattern\n"
225	    gdb_continue_to_breakpoint "${inline}_func$i" $pattern
226
227	    # Also check for the correct backtrace.
228	    set backtrace [linsert $backtrace 0 "(in|at)?${ws}${inline}_func$i"]
229	    gdb_test_sequence "bt" "bt stopped in ${inline}_func$i" $backtrace
230	}
231    }
232}
233
234# func_extern_caller calls func_inline_caller which calls
235# func_inline_callee.  The latter two are both inline functions.  Test
236# that setting a breakpoint on each of the functions reports a stop at
237# that function.  This exercises the inline frame skipping logic.  If
238# we set a breakpoint at function A, we want to present the stop at A,
239# even if A's entry code is an inlined call to another inline function
240# B.
241
242foreach_with_prefix func {
243    "func_extern_caller"
244    "func_inline_caller"
245    "func_inline_callee"
246} {
247    clean_restart $binfile
248
249    if {![runto main]} {
250	untested "could not run to main"
251	continue
252    }
253
254    gdb_breakpoint $func
255    gdb_test "continue" "Breakpoint .* $func .*at .*$srcfile.*" \
256	"breakpoint hit presents stop at breakpointed function"
257}
258
259# Test setting a breakpoint in an inline function by line number and
260# by address, and that GDB presents the stop there.
261
262set line [gdb_get_line_number "break here"]
263
264with_test_prefix "line number" {
265    clean_restart $binfile
266
267    if {![runto main]} {
268	untested "could not run to main"
269	continue
270    }
271
272    # Set the breakpoint by line number, and check that GDB reports
273    # the breakpoint location being the inline function.
274    gdb_test "break $srcfile:$line" ".*Breakpoint .* at .*: file .*$srcfile, line $line."
275
276    gdb_test "info break \$bpnum" "in func1 at .*$srcfile:$line"
277
278    gdb_test "continue" "Breakpoint .*, func1 \\(x=1\\) at .*$srcfile:$line.*break here.*" \
279	"breakpoint hit presents stop at inlined function"
280
281    # Save the PC for the following by-address test.
282    set address [get_hexadecimal_valueof "\$pc" "0"]
283}
284
285# Test setting a breakpoint in an inline function by address, and that
286# GDB presents the stop there.
287
288with_test_prefix "address" {
289
290    clean_restart $binfile
291
292    if {![runto main]} {
293	untested "could not run to main"
294	continue
295    }
296
297    # Set the breakpoint by address, and check that GDB reports the
298    # breakpoint location being the inline function.
299    gdb_test "break *$address" ".*Breakpoint .* at $address: file .*$srcfile, line $line."
300
301    gdb_test "info break \$bpnum" "in func1 at .*$srcfile:$line"
302
303    gdb_test "continue" "Breakpoint .*, func1 \\(x=1\\) at .*$srcfile:$line.*break here.*" \
304	"breakpoint hit presents stop at inlined function"
305}
306
307with_test_prefix "check alignment" {
308
309    clean_restart $binfile
310
311    if {![runto main]} {
312	untested "could not run to main"
313	continue
314    }
315
316    gdb_test "break func4b" \
317	"Breakpoint.*at.*func4b.*\\(2 locations\\)"
318
319    set expected_line_length -1
320    gdb_test_multiple "info break \$bpnum" "xxxx" {
321	-re "Num     Type           Disp Enb Address            What\r\n" {
322	    exp_continue
323	}
324	-re "($decimal \[^\r\n\]+)<MULTIPLE>\[^\r\n\]+\r\n" {
325	    if {$expected_line_length != -1} {
326		fail "multiple header lines seen"
327	    }
328	    set expected_line_length [string length $expect_out(1,string)]
329	    exp_continue
330	}
331	-re "($decimal\.($decimal) \[^\r\n\]+)$hex\[^\r\n\]+\r\n" {
332	    set len [string length $expect_out(1,string)]
333	    set loc $expect_out(2,string)
334	    gdb_assert {$len == $expected_line_length} \
335		"check alignment of location line $loc"
336	    exp_continue
337	}
338	-re "$gdb_prompt $" {
339	}
340    }
341}
342
343unset -nocomplain results
344