Automatic date update in version.in
[binutils-gdb.git] / gdb / testsuite / gdb.trace / circ.exp
blob41d4c8191ea2160040abf8b9f3cc6e28a3d390d6
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"
18 standard_testfile
20 if {[prepare_for_testing "failed to prepare" $testfile $srcfile {debug nowarnings nopie}]} {
21     return -1
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.
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 { } {
66     global decimal
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"
76 if { ![runto_main] } {
77     return -1
80 if { ![gdb_target_supports_trace] } {
81     unsupported "target does not support trace"
82     return 1
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"
89         return 1
90     }
91     -re "$gdb_prompt $" {
92         pass $test
93     }
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
102         pass $test
103     }
104     -re "$gdb_prompt $" {
105         pass $test
106     }
109 if { $circular_supported < 0 } {
110     unsupported "target does not support circular trace buffer"
111     return 1
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"
131             return -1
132         }
133         -re -wrap "" {
134             pass $gdb_test_name
135             set test_passed true
136         }
137     }
139     if { !$test_passed } {
140         return 0
141     }
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"
149                 return -1
150             }
151             pass $gdb_test_name
152             set test_passed true
153         }
154     }
156     if { !$test_passed } {
157         return 0
158     }
160     return 1
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} {
166     return
169 if {[set_trace_buffer_size 400] < 0} {
170     return
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"
180 set total_size -1
181 set free_size -1
182 set frame_size -1
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.*" \
195         "run to 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)
205             pass $test
206         }
207     }
209     # Check that we get the total_size and free_size.
210     if { $total_size < 0 } {
211         return 1
212     }
214     if { $free_size < 0 } {
215         return 1
216     }
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] } {
226         return 1
227     }
229     run_trace_experiment
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] } {
249         return 1
250     }
252     gdb_test_no_output "set trace-buffer-size $buffer_size" \
253         "shrink the target trace buffer"
255     run_trace_experiment
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.
266 # This means that:
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] } {
274         return 1
275     }
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"
283     run_trace_experiment
285     gdb_test "tstatus" \
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"
293     gdb_test \
294         "tfind pc func9" \
295         ".*Found trace frame $decimal, tracepoint $decimal\r\n#0  func9 .*" \
296         "find frame for func9"