1# Copyright 1998-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
16load_lib "trace-support.exp"
17
18standard_testfile
19
20if {[prepare_for_testing "failed to prepare" $testfile $srcfile {debug nowarnings nopie}]} {
21    return -1
22}
23
24# Tests:
25# 1) Calculate the size taken by one trace frame.
26# 2) Set up a trace experiment that will collect approximately 10 frames,
27#    requiring more than 512 but less than 1024 bytes of cache buffer.
28#    (most targets should have at least 1024 bytes of cache buffer!)
29#    Run and confirm that it collects all 10 frames.
30# 3) Artificially limit the trace buffer to 4x + a bytes.  Here x is the size
31#    of single trace frame and a is a small constant.  Rerun the
32#    experiment.  Confirm that the frame for the first tracepoint is collected,
33#    but frames for the last several tracepoints are not.
34# 4) Set trace buffer to circular mode, with the buffer size as in
35#    step 3 above.  Rerun the experiment.  Confirm that the frame for the last
36#    tracepoint is collected but not for the first one.
37#
38
39# Set a tracepoint on given func.  The tracepoint is set at entry
40# address and not 'after prologue' address because we use
41# 'tfind pc func' to find the corresponding trace frame afterwards,
42# and that looks for entry address.
43proc set_a_tracepoint { func } {
44    gdb_test "trace \*$func" "Tracepoint \[0-9\]+ at .*" \
45	"set tracepoint at $func"
46    gdb_trace_setactions "set actions for $func" "" "collect testload" "^$"
47}
48
49# Sets the tracepoints from func0 to func9 using set_a_tracepoint.
50proc setup_tracepoints { } {
51    gdb_delete_tracepoints
52    set_a_tracepoint func0
53    set_a_tracepoint func1
54    set_a_tracepoint func2
55    set_a_tracepoint func3
56    set_a_tracepoint func4
57    set_a_tracepoint func5
58    set_a_tracepoint func6
59    set_a_tracepoint func7
60    set_a_tracepoint func8
61    set_a_tracepoint func9
62}
63
64# Start the trace, run to end and then stop the trace.
65proc run_trace_experiment { } {
66    global decimal
67
68    setup_tracepoints
69    gdb_breakpoint "end" qualified
70    gdb_test "tstart" "\[\r\n\]*" "start trace experiment"
71    gdb_test "continue" "Continuing.*Breakpoint \[0-9\]+, end.*" \
72	"run to end"
73    gdb_test "tstop" "\[\r\n\]*" "stop trace experiment"
74}
75
76if { ![runto_main] } {
77    fail "can't run to main to check for trace support"
78    return -1
79}
80
81if { ![gdb_target_supports_trace] } {
82    unsupported "target does not support trace"
83    return 1
84}
85
86set test "set circular-trace-buffer on"
87gdb_test_multiple "set circular-trace-buffer on" $test {
88    -re ".*Target does not support this command.*$gdb_prompt $" {
89	unsupported "target does not support circular trace buffer"
90	return 1
91    }
92    -re "$gdb_prompt $" {
93	pass $test
94    }
95}
96
97set circular_supported -1
98set test "check whether circular buffer is supported"
99
100gdb_test_multiple "tstatus" $test {
101    -re ".*Trace buffer is circular.*$gdb_prompt $" {
102	set circular_supported 1
103	pass $test
104    }
105    -re "$gdb_prompt $" {
106	pass $test
107    }
108}
109
110if { $circular_supported < 0 } {
111    unsupported "target does not support circular trace buffer"
112    return 1
113}
114
115gdb_test "show circular-trace-buffer" \
116    "Target's use of circular trace buffer is on." \
117    "show circular-trace-buffer (on)"
118
119# Check if changing the trace buffer size is supported.  This step is
120# repeated twice.  This helps in case the trace buffer size is 100.
121set test_size 100
122set test "change buffer size to $test_size"
123gdb_test_multiple "set trace-buffer-size $test_size" $test {
124    -re ".*Target does not support this command.*$gdb_prompt $" {
125	unsupported "target does not support changing trace buffer size"
126	return 1
127    }
128    -re "$gdb_prompt $" {
129	pass $test
130    }
131}
132
133set test "check whether setting trace buffer size is supported"
134gdb_test_multiple "tstatus" $test {
135    -re ".*Trace buffer has ($decimal) bytes of ($decimal) bytes free.*$gdb_prompt $" {
136	set total_size $expect_out(2,string)
137	if { $test_size != $total_size } {
138	    unsupported "target does not support changing trace buffer size"
139	    return 1
140	}
141	pass $test
142    }
143}
144
145set test_size 400
146gdb_test_no_output "set trace-buffer-size $test_size" \
147    "change buffer size to $test_size"
148
149gdb_test_multiple "tstatus" $test {
150    -re ".*Trace buffer has ($decimal) bytes of ($decimal) bytes free.*$gdb_prompt $" {
151	set total_size $expect_out(2,string)
152	if { $test_size != $total_size } {
153	    unsupported "target does not support changing trace buffer size"
154	    return 1
155	}
156	pass $test
157    }
158}
159
160gdb_test_no_output "set circular-trace-buffer off" \
161    "set circular-trace-buffer off"
162
163gdb_test "show circular-trace-buffer" \
164    "Target's use of circular trace buffer is off." \
165    "show circular-trace-buffer (off)"
166
167set total_size -1
168set free_size -1
169set frame_size -1
170
171# Determine the size used by a single frame.  Set a single tracepoint,
172# run and then check the total and free size using the tstatus command.
173# Then subtracting free from total gives us the size of a frame.
174with_test_prefix "frame size" {
175    set_a_tracepoint func0
176
177    gdb_breakpoint "end" qualified
178
179    gdb_test "tstart" "\[\r\n\]*" "start trace"
180
181    gdb_test "continue" "Continuing.*Breakpoint \[0-9\]+, end.*" \
182	"run to end"
183
184    gdb_test "tstop" "\[\r\n\]*" "stop trace"
185
186    set test "get buffer size"
187
188    gdb_test_multiple "tstatus" $test {
189	-re ".*Trace buffer has ($decimal) bytes of ($decimal) bytes free.*$gdb_prompt $" {
190	    set free_size $expect_out(1,string)
191	    set total_size $expect_out(2,string)
192	    pass $test
193	}
194    }
195
196    # Check that we get the total_size and free_size.
197    if { $total_size < 0 } {
198	return 1
199    }
200
201    if { $free_size < 0 } {
202	return 1
203    }
204}
205
206# Calculate the size of a single frame.
207set frame_size "($total_size - $free_size)"
208
209with_test_prefix "normal buffer" {
210    clean_restart $testfile
211
212    if { ![runto_main] } {
213	fail "can't run to main"
214	return 1
215    }
216
217    run_trace_experiment
218
219    # Check that the first frame is actually at func0.
220    gdb_test "tfind start" ".*#0  func0 .*" \
221	"first frame is at func0"
222
223    gdb_test "tfind pc func9" \
224	".*Found trace frame $decimal, tracepoint $decimal\r\n#0  func9 .*" \
225	"find frame for func9"
226}
227
228# Shrink the trace buffer so that it will not hold
229# all ten trace frames.  Verify that the frame for func0 is still
230# collected, but the frame for func9 is not.
231
232set buffer_size "((4 * $frame_size) + 10)"
233with_test_prefix "small buffer" {
234    clean_restart $testfile
235
236    if { ![runto_main] } {
237	fail "can't run to main"
238	return 1
239    }
240
241    gdb_test_no_output "set trace-buffer-size $buffer_size" \
242	"shrink the target trace buffer"
243
244    run_trace_experiment
245
246    gdb_test "tfind start" ".*#0  func0 .*" \
247	"first frame is at func0"
248
249    gdb_test "tfind pc func9" ".* failed to find .*" \
250	"find frame for func9"
251}
252
253# Finally, make the buffer circular.  Now when it runs out of
254# space, it should wrap around and overwrite the earliest frames.
255# This means that:
256# 1) the first frame will be overwritten and therefore unavailable.
257# 2) the earliest frame in the buffer will not be for func0.
258# 3) the frame for func9 will be available (unlike "small buffer" case).
259with_test_prefix "circular buffer" {
260    clean_restart $testfile
261
262    if { ![runto_main] } {
263	fail "can't run to main"
264	return 1
265    }
266
267    gdb_test_no_output "set trace-buffer-size $buffer_size" \
268	"shrink the target trace buffer"
269
270    gdb_test_no_output "set circular-trace-buffer on" \
271	"make the target trace buffer circular"
272
273    run_trace_experiment
274
275    gdb_test "tstatus" \
276	".*Buffer contains $decimal trace frames \\(of $decimal created total\\).*Trace buffer is circular.*" \
277	"trace buffer is circular"
278
279    # The first frame should not be at func0.
280    gdb_test "tfind start" ".*#0  func\[1-9\] .*" \
281	"first frame is NOT at func0"
282
283    gdb_test \
284	"tfind pc func9" \
285	".*Found trace frame $decimal, tracepoint $decimal\r\n#0  func9 .*" \
286	"find frame for func9"
287}
288