1 # This testcase is part of GDB, the GNU debugger.
3 # Copyright 2013-2024 Free Software Foundation, Inc.
5 # Contributed by Intel Corp. <christian.himpel@intel.com>
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.
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.
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/>.
20 require allow_btrace_tests
22 standard_testfile .c .S
23 if [prepare_for_testing "failed to prepare" $testfile "$srcfile $srcfile2" {debug}] {
31 # set bp before loop and continue
32 set bp_location [gdb_get_line_number "bp.1" $srcfile2]
33 gdb_breakpoint $srcfile2:$bp_location
34 gdb_continue_to_breakpoint "cont to $bp_location" ".*$srcfile2:$bp_location.*"
37 gdb_test_no_output "record btrace"
39 # set bp after loop and continue
40 set bp_location [gdb_get_line_number "bp.2" $srcfile2]
41 gdb_breakpoint $srcfile2:$bp_location
42 gdb_continue_to_breakpoint "cont to $bp_location" ".*$srcfile2:$bp_location.*"
44 # The following test cases test if "browsing" through the
45 # instruction history works as expected. So for the tests
46 # it is necessary to count the number of lines that are
47 # shown by the "record instruction-history" command.
50 set testname "determine number of recorded instructions"
51 gdb_test_multiple "info record" $testname {
52 -re "Active record target: record-btrace\r\n.*\r\nRecorded \(\[0-9\]*\) instructions in \(\[0-9\]*\) functions \\\(0 gaps\\\) for thread 1 .*\\.\r\n$gdb_prompt $" {
53 set traced $expect_out(1,string)
58 # we have exactly 11 instructions here
59 set message "exactly 11 instructions"
60 if { $traced != 11 } {
66 # test that we see the expected instructions
67 gdb_test "record instruction-history 3,7" [multi_line \
68 "3\t 0x\[0-9a-f\]+ <loop\\+\[0-9\]+>:\tje 0x\[0-9a-f\]+ <loop\\+\[0-9\]+>" \
69 "4\t 0x\[0-9a-f\]+ <loop\\+\[0-9\]+>:\tdec %eax" \
70 "5\t 0x\[0-9a-f\]+ <loop\\+\[0-9\]+>:\tjmp 0x\[0-9a-f\]+ <loop\\+\[0-9\]+>" \
71 "6\t 0x\[0-9a-f\]+ <loop\\+\[0-9\]+>:\tcmp \\\$0x0,%eax" \
72 "7\t 0x\[0-9a-f\]+ <loop\\+\[0-9\]+>:\tje 0x\[0-9a-f\]+ <loop\\+\[0-9\]+>" \
75 gdb_test "record instruction-history /f 3,+5" [multi_line \
76 "3\t 0x\[0-9a-f\]+ <\\+\[0-9\]+>:\tje 0x\[0-9a-f\]+ <loop\\+\[0-9\]+>" \
77 "4\t 0x\[0-9a-f\]+ <\\+\[0-9\]+>:\tdec %eax" \
78 "5\t 0x\[0-9a-f\]+ <\\+\[0-9\]+>:\tjmp 0x\[0-9a-f\]+ <loop\\+\[0-9\]+>" \
79 "6\t 0x\[0-9a-f\]+ <\\+\[0-9\]+>:\tcmp \\\$0x0,%eax" \
80 "7\t 0x\[0-9a-f\]+ <\\+\[0-9\]+>:\tje 0x\[0-9a-f\]+ <loop\\+\[0-9\]+>" \
83 gdb_test "record instruction-history /p 7,-5" [multi_line \
84 "3\t 0x\[0-9a-f\]+ <loop\\+\[0-9\]+>:\tje 0x\[0-9a-f\]+ <loop\\+\[0-9\]+>" \
85 "4\t 0x\[0-9a-f\]+ <loop\\+\[0-9\]+>:\tdec %eax" \
86 "5\t 0x\[0-9a-f\]+ <loop\\+\[0-9\]+>:\tjmp 0x\[0-9a-f\]+ <loop\\+\[0-9\]+>" \
87 "6\t 0x\[0-9a-f\]+ <loop\\+\[0-9\]+>:\tcmp \\\$0x0,%eax" \
88 "7\t 0x\[0-9a-f\]+ <loop\\+\[0-9\]+>:\tje 0x\[0-9a-f\]+ <loop\\+\[0-9\]+>" \
91 gdb_test "record instruction-history /pf 3,7" [multi_line \
92 "3\t 0x\[0-9a-f\]+ <\\+\[0-9\]+>:\tje 0x\[0-9a-f\]+ <loop\\+\[0-9\]+>" \
93 "4\t 0x\[0-9a-f\]+ <\\+\[0-9\]+>:\tdec %eax" \
94 "5\t 0x\[0-9a-f\]+ <\\+\[0-9\]+>:\tjmp 0x\[0-9a-f\]+ <loop\\+\[0-9\]+>" \
95 "6\t 0x\[0-9a-f\]+ <\\+\[0-9\]+>:\tcmp \\\$0x0,%eax" \
96 "7\t 0x\[0-9a-f\]+ <\\+\[0-9\]+>:\tje 0x\[0-9a-f\]+ <loop\\+\[0-9\]+>" \
99 gdb_test "record instruction-history 3,3" "3\t 0x\[0-9a-f\]+ <loop\\+\[0-9\]+>:\tje 0x\[0-9a-f\]+ <loop\\+\[0-9\]+>"
101 # the following tests are checking the iterators
102 # to avoid lots of regexps, we just check the number of lines that
103 # were printed during command execution.
105 # test_lines_output returns the output lines from command as a list.
106 proc test_lines_output { command message } {
108 set message "test_lines_output: $message"
109 gdb_test_multiple $command $message {
110 -re "\n\(.*\)\r\n$gdb_prompt $" {
111 return [split [string trim $expect_out(1,string)] "\n"]
116 # test_lines_length returns the number of lines from command.
117 proc test_lines_length { command message } {
118 return [llength [test_lines_output $command $message]]
121 # show instruction history with unlimited size, we expect to see
122 # all $traced instructions
123 gdb_test_no_output "set record instruction-history-size 0"
124 set message "record instruction-history - unlimited"
125 set lines [test_lines_length "record instruction-history 1" $message]
126 if { $traced != $lines } {
132 gdb_test_no_output "set record instruction-history-size $traced"
133 set message "record instruction-history - traced"
134 set lines [test_lines_length "record instruction-history 1" $message]
135 if { $traced != $lines } {
141 # test that the iterator works
143 gdb_test_no_output "set record instruction-history-size $history_size"
144 set message "browse history forward start"
145 set lines [test_lines_length "record instruction-history 1" $message]
146 if { $lines != $history_size } {
152 set message "browse history forward middle"
153 set lines [test_lines_length "record instruction-history +" $message]
154 if { $lines != $history_size } {
160 set message "browse history forward last"
161 set lines [test_lines_length "record instruction-history +" $message]
168 gdb_test "record instruction-history" "At the end of the branch trace record\\." "browse history forward beyond 1"
170 # make sure we cannot move further
171 gdb_test "record instruction-history" "At the end of the branch trace record\\." "browse history forward beyond 2"
173 set message "browse history backward last"
174 set lines [test_lines_length "record instruction-history -" $message]
175 if { $lines != $history_size } {
181 set message "browse history backward middle"
182 set lines [test_lines_length "record instruction-history -" $message]
183 if { $lines != $history_size } {
189 set message "browse history backward first"
190 set lines [test_lines_length "record instruction-history -" $message]
197 gdb_test "record instruction-history -" "At the start of the branch trace record\\." "browse history backward beyond 1"
199 # make sure we cannot move further back
200 gdb_test "record instruction-history -" "At the start of the branch trace record\\." "browse history backward beyond 2"