xref: /netbsd-src/external/gpl3/gdb.old/dist/gdb/testsuite/gdb.btrace/function_call_history.exp (revision 8b657b0747480f8989760d71343d6dd33f8d4cf9)
1# This testcase is part of GDB, the GNU debugger.
2#
3# Copyright 2013-2023 Free Software Foundation, Inc.
4#
5# Contributed by Intel Corp. <christian.himpel@intel.com>
6#
7# This program is free software; you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation; either version 3 of the License, or
10# (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
20if { [skip_btrace_tests] } {
21    unsupported "target does not support record-btrace"
22    return -1
23}
24
25# We expect a specific function call history.  This gets messed up with
26# PIE on 32-bit.
27standard_testfile
28if [prepare_for_testing "failed to prepare" $testfile {} {nopie debug}] {
29    return -1
30}
31
32if ![runto_main] {
33    return -1
34}
35
36# start btrace
37gdb_test_no_output "record btrace"
38
39# set bp after increment loop and continue
40set bp_location [gdb_get_line_number "bp.1" $testfile.c]
41gdb_breakpoint $bp_location
42gdb_continue_to_breakpoint "cont to $bp_location" ".*$testfile.c:$bp_location.*"
43
44proc rec_fun_all {} {
45  gdb_test "record function-call-history 1" [multi_line \
46    "1\tmain" \
47    "2\tinc" \
48    "3\tmain" \
49    "4\tinc" \
50    "5\tmain" \
51    "6\tinc" \
52    "7\tmain" \
53    "8\tinc" \
54    "9\tmain" \
55    "10\tinc" \
56    "11\tmain" \
57    "12\tinc" \
58    "13\tmain" \
59    "14\tinc" \
60    "15\tmain" \
61    "16\tinc" \
62    "17\tmain" \
63    "18\tinc" \
64    "19\tmain" \
65    "20\tinc" \
66    "21\tmain"]
67}
68
69# show function call history with unlimited size, we expect to see all 21 entries
70gdb_test_no_output "set record function-call-history-size 0"
71with_test_prefix "size unlimited" rec_fun_all
72
73# show function call history with size of 21, we expect to see all 21 entries
74gdb_test_no_output "set record function-call-history-size 21"
75with_test_prefix "size 21" rec_fun_all
76
77# show first 15 entries
78gdb_test_no_output "set record function-call-history-size 15"
79gdb_test "record function-call-history 1" [multi_line \
80  "1\tmain" \
81  "2\tinc" \
82  "3\tmain" \
83  "4\tinc" \
84  "5\tmain" \
85  "6\tinc" \
86  "7\tmain" \
87  "8\tinc" \
88  "9\tmain" \
89  "10\tinc" \
90  "11\tmain" \
91  "12\tinc" \
92  "13\tmain" \
93  "14\tinc" \
94  "15\tmain"] "forward - 1"
95
96# show last 6 entries
97gdb_test "record function-call-history +" [multi_line \
98  "16\tinc" \
99  "17\tmain" \
100  "18\tinc" \
101  "19\tmain" \
102  "20\tinc" \
103  "21\tmain"] "forward - 2"
104
105# moving further should not work
106gdb_test "record function-call-history +" "At the end of the branch trace record\\." "forward - 3"
107
108# make sure we cannot move any further a second time
109gdb_test "record function-call-history +" "At the end of the branch trace record\\." "forward - 4"
110
111# moving back showing the latest 15 function calls
112gdb_test "record function-call-history -" [multi_line \
113  "7\tmain" \
114  "8\tinc" \
115  "9\tmain" \
116  "10\tinc" \
117  "11\tmain" \
118  "12\tinc" \
119  "13\tmain" \
120  "14\tinc" \
121  "15\tmain" \
122  "16\tinc" \
123  "17\tmain" \
124  "18\tinc" \
125  "19\tmain" \
126  "20\tinc" \
127  "21\tmain"] "backward - 1"
128
129# moving further back shows the 6 first function calls
130gdb_test "record function-call-history -" [multi_line \
131  "1\tmain" \
132  "2\tinc" \
133  "3\tmain" \
134  "4\tinc" \
135  "5\tmain" \
136  "6\tinc"] "backward - 2"
137
138# moving further back shouldn't work
139gdb_test "record function-call-history -" "At the start of the branch trace record\\." "backward - 3"
140
141# make sure we cannot move any further back
142gdb_test "record function-call-history -" "At the start of the branch trace record\\." "backward - 4"
143
144# don't mess around with path names
145gdb_test_no_output "set filename-display basename"
146
147# moving forward again, but this time with file and line number, expected to see the first 15 entries
148gdb_test "record function-call-history /l +" [multi_line \
149  "\[0-9\]*\tmain\tat $srcfile:40,41" \
150  "\[0-9\]*\tinc\tat $srcfile:22,2\[34\]" \
151  "\[0-9\]*\tmain\tat $srcfile:40,41" \
152  "\[0-9\]*\tinc\tat $srcfile:22,2\[34\]" \
153  "\[0-9\]*\tmain\tat $srcfile:40,41" \
154  "\[0-9\]*\tinc\tat $srcfile:22,2\[34\]" \
155  "\[0-9\]*\tmain\tat $srcfile:40,41" \
156  "\[0-9\]*\tinc\tat $srcfile:22,2\[34\]" \
157  "\[0-9\]*\tmain\tat $srcfile:40,41" \
158  "\[0-9\]*\tinc\tat $srcfile:22,2\[34\]" \
159  "\[0-9\]*\tmain\tat $srcfile:40,41" \
160  "\[0-9\]*\tinc\tat $srcfile:22,2\[34\]" \
161  "\[0-9\]*\tmain\tat $srcfile:40,41" \
162  "\[0-9\]*\tinc\tat $srcfile:22,2\[34\]" \
163  "\[0-9\]*\tmain\tat $srcfile:40,41" \
164  ] "forward /l - 1"
165
166# moving forward and expect to see the latest 6 entries
167gdb_test "record function-call-history /l +" [multi_line \
168  "\[0-9\]*\tinc\tat $srcfile:22,2\[34\]" \
169  "\[0-9\]*\tmain\tat $srcfile:40,41" \
170  "\[0-9\]*\tinc\tat $srcfile:22,2\[34\]" \
171  "\[0-9\]*\tmain\tat $srcfile:40,41" \
172  "\[0-9\]*\tinc\tat $srcfile:22,2\[34\]" \
173  "\[0-9\]*\tmain\tat $srcfile:40,43" \
174  ] "forward /l - 2"
175
176# moving further forward shouldn't work
177gdb_test "record function-call-history /l +" "At the end of the branch trace record\\." "forward /l - 3"
178gdb_test "record function-call-history /l" "At the end of the branch trace record\\." "forward /l - 4"
179
180set expected_range [multi_line \
181  "4\tinc" \
182  "5\tmain" \
183  "6\tinc" \
184  "7\tmain" \
185  "8\tinc" \
186  "9\tmain" \
187  "10\tinc"]
188
189# show functions in instruction range
190gdb_test "record function-call-history 4,10" $expected_range
191gdb_test "record function-call-history 4,+7" $expected_range
192gdb_test "record function-call-history 10,-7" $expected_range
193gdb_test "record function-call-history 4,4" "4\tinc\r"
194
195# set bp after fib recursion and continue
196set bp_location [gdb_get_line_number "bp.2" $testfile.c]
197gdb_breakpoint $bp_location
198gdb_continue_to_breakpoint "cont to $bp_location" ".*$testfile.c:$bp_location.*"
199
200# at this point we expect to have main, fib, ..., fib, main, where fib occurs 9 times,
201# so we limit the output to only show the latest 11 function calls
202gdb_test_no_output "set record function-call-history-size 11"
203gdb_test "record function-call-history" [multi_line \
204  "21\tmain" \
205  "22\tfib" \
206  "23\tfib" \
207  "24\tfib" \
208  "25\tfib" \
209  "26\tfib" \
210  "27\tfib" \
211  "28\tfib" \
212  "29\tfib" \
213  "30\tfib" \
214  "31\tmain"] "recursive"
215
216# show indented function call history for fib
217gdb_test "record function-call-history /c 21, +11" [multi_line \
218  "21\tmain" \
219  "22\t  fib" \
220  "23\t    fib" \
221  "24\t  fib" \
222  "25\t    fib" \
223  "26\t      fib" \
224  "27\t    fib" \
225  "28\t      fib" \
226  "29\t    fib" \
227  "30\t  fib" \
228  "31\tmain" \
229  ] "indented"
230
231# make sure we can handle incomplete trace with respect to indentation
232if ![runto_main] {
233    return -1
234}
235# navigate to the fib in line 24 above
236gdb_breakpoint fib
237gdb_continue_to_breakpoint "cont to fib.1"
238gdb_continue_to_breakpoint "cont to fib.2"
239gdb_continue_to_breakpoint "cont to fib.3"
240gdb_continue_to_breakpoint "cont to fib.4"
241
242# start tracing
243gdb_test_no_output "record btrace" "start recording after rerun"
244
245# continue until line 30 above
246delete_breakpoints
247set bp_location [gdb_get_line_number "bp.2" $testfile.c]
248gdb_breakpoint $bp_location
249gdb_continue_to_breakpoint "cont to bp.2" ".*$testfile.c:$bp_location\r\n.*"
250
251# let's look at the trace. we expect to see the tail of the above listing.
252gdb_test "record function-call-history /c" [multi_line \
253  "1\t      fib" \
254  "2\t    fib" \
255  "3\t      fib" \
256  "4\t    fib" \
257  "5\t  fib" \
258  "6\tmain" \
259  ] "indented tail"
260