1 # Copyright 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 load_lib "trace-support.exp"
20 if {[prepare_for_testing "failed to prepare" $testfile $srcfile {debug nowarnings nopie}]} {
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.
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.
43 proc 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" "^$"
49 # Sets the tracepoints from func0 to func9 using set_a_tracepoint.
50 proc 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
64 # Start the trace, run to end and then stop the trace.
65 proc run_trace_experiment { } {
69 gdb_breakpoint "end" qualified
70 gdb_test "tstart" "\[\r\n\]*" "start trace experiment"
71 gdb_test "continue" "Continuing.*Breakpoint \[0-9\]+, end.*" \
73 gdb_test "tstop" "\[\r\n\]*" "stop trace experiment"
76 if { ![runto_main] } {
80 if { ![gdb_target_supports_trace] } {
81 unsupported "target does not support trace"
85 set test "set circular-trace-buffer on"
86 gdb_test_multiple "set circular-trace-buffer on" $test {
87 -re ".*Target does not support this command.*$gdb_prompt $" {
88 unsupported "target does not support circular trace buffer"
96 set circular_supported -1
97 set test "check whether circular buffer is supported"
99 gdb_test_multiple "tstatus" $test {
100 -re ".*Trace buffer is circular.*$gdb_prompt $" {
101 set circular_supported 1
104 -re "$gdb_prompt $" {
109 if { $circular_supported < 0 } {
110 unsupported "target does not support circular trace buffer"
114 gdb_test "show circular-trace-buffer" \
115 "Target's use of circular trace buffer is on." \
116 "check circular-trace-buffer is on"
118 # Use 'set trace-buffer-size' to change the trace buffer size to
119 # REQUIRED_SIZE. Return -1 if the current target doesn't support
120 # adjusting the trace buffer size. Return 0 if the adjustment failed
121 # for some other reason. Return 1 if the trace buffer size was
122 # correctly adjusted.
124 # If this proc returns -1 then a suitable call to the unsupported proc
125 # will have already been made.
126 proc set_trace_buffer_size { required_size } {
127 set test_passed false
128 gdb_test_multiple "set trace-buffer-size $required_size" "" {
129 -re -wrap ".*Target does not support this command.*" {
130 unsupported "target does not support changing trace buffer size"
139 if { !$test_passed } {
143 set test_passed false
144 gdb_test_multiple "tstatus" "check trace-buffer-size is $required_size" {
145 -re -wrap ".*Trace buffer has ($::decimal) bytes of ($::decimal) bytes free.*" {
146 set total_size $expect_out(2,string)
147 if { $required_size != $total_size } {
148 unsupported "target does not support changing trace buffer size"
156 if { !$test_passed } {
163 # Check if changing the trace buffer size is supported. This step is
164 # repeated twice. This helps in case the trace buffer size is 100.
165 if {[set_trace_buffer_size 100] < 0} {
169 if {[set_trace_buffer_size 400] < 0} {
173 gdb_test_no_output "set circular-trace-buffer off" \
174 "set circular-trace-buffer off"
176 gdb_test "show circular-trace-buffer" \
177 "Target's use of circular trace buffer is off." \
178 "check circular-trace-buffer is off"
184 # Determine the size used by a single frame. Set a single tracepoint,
185 # run and then check the total and free size using the tstatus command.
186 # Then subtracting free from total gives us the size of a frame.
187 with_test_prefix "frame size" {
188 set_a_tracepoint func0
190 gdb_breakpoint "end" qualified
192 gdb_test "tstart" "\[\r\n\]*" "start trace"
194 gdb_test "continue" "Continuing.*Breakpoint \[0-9\]+, end.*" \
197 gdb_test "tstop" "\[\r\n\]*" "stop trace"
199 set test "get buffer size"
201 gdb_test_multiple "tstatus" $test {
202 -re ".*Trace buffer has ($decimal) bytes of ($decimal) bytes free.*$gdb_prompt $" {
203 set free_size $expect_out(1,string)
204 set total_size $expect_out(2,string)
209 # Check that we get the total_size and free_size.
210 if { $total_size < 0 } {
214 if { $free_size < 0 } {
219 # Calculate the size of a single frame.
220 set frame_size "($total_size - $free_size)"
222 with_test_prefix "normal buffer" {
223 clean_restart $testfile
225 if { ![runto_main] } {
231 # Check that the first frame is actually at func0.
232 gdb_test "tfind start" ".*#0 func0 .*" \
233 "first frame is at func0"
235 gdb_test "tfind pc func9" \
236 ".*Found trace frame $decimal, tracepoint $decimal\r\n#0 func9 .*" \
237 "find frame for func9"
240 # Shrink the trace buffer so that it will not hold
241 # all ten trace frames. Verify that the frame for func0 is still
242 # collected, but the frame for func9 is not.
244 set buffer_size "((4 * $frame_size) + 10)"
245 with_test_prefix "small buffer" {
246 clean_restart $testfile
248 if { ![runto_main] } {
252 gdb_test_no_output "set trace-buffer-size $buffer_size" \
253 "shrink the target trace buffer"
257 gdb_test "tfind start" ".*#0 func0 .*" \
258 "first frame is at func0"
260 gdb_test "tfind pc func9" ".* failed to find .*" \
261 "find frame for func9"
264 # Finally, make the buffer circular. Now when it runs out of
265 # space, it should wrap around and overwrite the earliest frames.
267 # 1) the first frame will be overwritten and therefore unavailable.
268 # 2) the earliest frame in the buffer will not be for func0.
269 # 3) the frame for func9 will be available (unlike "small buffer" case).
270 with_test_prefix "circular buffer" {
271 clean_restart $testfile
273 if { ![runto_main] } {
277 gdb_test_no_output "set trace-buffer-size $buffer_size" \
278 "shrink the target trace buffer"
280 gdb_test_no_output "set circular-trace-buffer on" \
281 "make the target trace buffer circular"
286 ".*Buffer contains $decimal trace frames \\(of $decimal created total\\).*Trace buffer is circular.*" \
287 "trace buffer is circular"
289 # The first frame should not be at func0.
290 gdb_test "tfind start" ".*#0 func\[1-9\] .*" \
291 "first frame is NOT at func0"
295 ".*Found trace frame $decimal, tracepoint $decimal\r\n#0 func9 .*" \
296 "find frame for func9"