1 # Copyright 1998-2019 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] } {
77 fail "can't run to main to check for trace support"
81 if { ![gdb_target_supports_trace] } {
82 unsupported "target does not support trace"
86 set test "set circular-trace-buffer on"
87 gdb_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"
97 set circular_supported -1
98 set test "check whether circular buffer is supported"
100 gdb_test_multiple "tstatus" $test {
101 -re ".*Trace buffer is circular.*$gdb_prompt $" {
102 set circular_supported 1
105 -re "$gdb_prompt $" {
110 if { $circular_supported < 0 } {
111 unsupported "target does not support circular trace buffer"
115 gdb_test "show circular-trace-buffer" \
116 "Target's use of circular trace buffer is on." \
117 "show circular-trace-buffer (on)"
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.
122 set test "change buffer size to $test_size"
123 gdb_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"
128 -re "$gdb_prompt $" {
133 set test "check whether setting trace buffer size is supported"
134 gdb_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"
146 gdb_test_no_output "set trace-buffer-size $test_size" \
147 "change buffer size to $test_size"
149 gdb_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"
160 gdb_test_no_output "set circular-trace-buffer off" \
161 "set circular-trace-buffer off"
163 gdb_test "show circular-trace-buffer" \
164 "Target's use of circular trace buffer is off." \
165 "show circular-trace-buffer (off)"
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.
174 with_test_prefix "frame size" {
175 set_a_tracepoint func0
177 gdb_breakpoint "end" qualified
179 gdb_test "tstart" "\[\r\n\]*" "start trace"
181 gdb_test "continue" "Continuing.*Breakpoint \[0-9\]+, end.*" \
184 gdb_test "tstop" "\[\r\n\]*" "stop trace"
186 set test "get buffer size"
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)
196 # Check that we get the total_size and free_size.
197 if { $total_size < 0 } {
201 if { $free_size < 0 } {
206 # Calculate the size of a single frame.
207 set frame_size "($total_size - $free_size)"
209 with_test_prefix "normal buffer" {
210 clean_restart $testfile
212 if { ![runto_main] } {
213 fail "can't run to main"
219 # Check that the first frame is actually at func0.
220 gdb_test "tfind start" ".*#0 func0 .*" \
221 "first frame is at func0"
223 gdb_test "tfind pc func9" \
224 ".*Found trace frame $decimal, tracepoint $decimal\r\n#0 func9 .*" \
225 "find frame for func9"
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.
232 set buffer_size "((4 * $frame_size) + 10)"
233 with_test_prefix "small buffer" {
234 clean_restart $testfile
236 if { ![runto_main] } {
237 fail "can't run to main"
241 gdb_test_no_output "set trace-buffer-size $buffer_size" \
242 "shrink the target trace buffer"
246 gdb_test "tfind start" ".*#0 func0 .*" \
247 "first frame is at func0"
249 gdb_test "tfind pc func9" ".* failed to find .*" \
250 "find frame for func9"
253 # Finally, make the buffer circular. Now when it runs out of
254 # space, it should wrap around and overwrite the earliest frames.
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).
259 with_test_prefix "circular buffer" {
260 clean_restart $testfile
262 if { ![runto_main] } {
263 fail "can't run to main"
267 gdb_test_no_output "set trace-buffer-size $buffer_size" \
268 "shrink the target trace buffer"
270 gdb_test_no_output "set circular-trace-buffer on" \
271 "make the target trace buffer circular"
276 ".*Buffer contains $decimal trace frames \\(of $decimal created total\\).*Trace buffer is circular.*" \
277 "trace buffer is circular"
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"
285 ".*Found trace frame $decimal, tracepoint $decimal\r\n#0 func9 .*" \
286 "find frame for func9"