xref: /netbsd-src/external/gpl3/gdb.old/dist/gdb/testsuite/gdb.btrace/instruction_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
25standard_testfile .c .S
26if [prepare_for_testing "failed to prepare" $testfile "$srcfile $srcfile2" {debug}] {
27    return -1
28}
29
30if ![runto_main] {
31    return -1
32}
33
34# set bp before loop and continue
35set bp_location [gdb_get_line_number "bp.1" $srcfile2]
36gdb_breakpoint $srcfile2:$bp_location
37gdb_continue_to_breakpoint "cont to $bp_location" ".*$srcfile2:$bp_location.*"
38
39# start btrace
40gdb_test_no_output "record btrace"
41
42# set bp after loop and continue
43set bp_location [gdb_get_line_number "bp.2" $srcfile2]
44gdb_breakpoint $srcfile2:$bp_location
45gdb_continue_to_breakpoint "cont to $bp_location" ".*$srcfile2:$bp_location.*"
46
47# The following test cases test if "browsing" through the
48# instruction history works as expected. So for the tests
49# it is necessary to count the number of lines that are
50# shown by the "record instruction-history" command.
51
52set traced {}
53set testname "determine number of recorded instructions"
54gdb_test_multiple "info record" $testname {
55    -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 $" {
56        set traced $expect_out(1,string)
57        pass $testname
58    }
59}
60
61# we have exactly 11 instructions here
62set message "exactly 11 instructions"
63if { $traced != 11 } {
64    fail $message
65} else {
66    pass $message
67}
68
69# test that we see the expected instructions
70gdb_test "record instruction-history 3,7" [multi_line \
71  "3\t   0x\[0-9a-f\]+ <loop\\+\[0-9\]+>:\tje     0x\[0-9a-f\]+ <loop\\+\[0-9\]+>" \
72  "4\t   0x\[0-9a-f\]+ <loop\\+\[0-9\]+>:\tdec    %eax" \
73  "5\t   0x\[0-9a-f\]+ <loop\\+\[0-9\]+>:\tjmp    0x\[0-9a-f\]+ <loop\\+\[0-9\]+>" \
74  "6\t   0x\[0-9a-f\]+ <loop\\+\[0-9\]+>:\tcmp    \\\$0x0,%eax" \
75  "7\t   0x\[0-9a-f\]+ <loop\\+\[0-9\]+>:\tje     0x\[0-9a-f\]+ <loop\\+\[0-9\]+>\r" \
76  ]
77
78gdb_test "record instruction-history /f 3,+5" [multi_line \
79  "3\t   0x\[0-9a-f\]+ <\\+\[0-9\]+>:\tje     0x\[0-9a-f\]+ <loop\\+\[0-9\]+>" \
80  "4\t   0x\[0-9a-f\]+ <\\+\[0-9\]+>:\tdec    %eax" \
81  "5\t   0x\[0-9a-f\]+ <\\+\[0-9\]+>:\tjmp    0x\[0-9a-f\]+ <loop\\+\[0-9\]+>" \
82  "6\t   0x\[0-9a-f\]+ <\\+\[0-9\]+>:\tcmp    \\\$0x0,%eax" \
83  "7\t   0x\[0-9a-f\]+ <\\+\[0-9\]+>:\tje     0x\[0-9a-f\]+ <loop\\+\[0-9\]+>\r" \
84  ]
85
86gdb_test "record instruction-history /p 7,-5" [multi_line \
87  "3\t   0x\[0-9a-f\]+ <loop\\+\[0-9\]+>:\tje     0x\[0-9a-f\]+ <loop\\+\[0-9\]+>" \
88  "4\t   0x\[0-9a-f\]+ <loop\\+\[0-9\]+>:\tdec    %eax" \
89  "5\t   0x\[0-9a-f\]+ <loop\\+\[0-9\]+>:\tjmp    0x\[0-9a-f\]+ <loop\\+\[0-9\]+>" \
90  "6\t   0x\[0-9a-f\]+ <loop\\+\[0-9\]+>:\tcmp    \\\$0x0,%eax" \
91  "7\t   0x\[0-9a-f\]+ <loop\\+\[0-9\]+>:\tje     0x\[0-9a-f\]+ <loop\\+\[0-9\]+>\r" \
92  ]
93
94gdb_test "record instruction-history /pf 3,7" [multi_line \
95  "3\t   0x\[0-9a-f\]+ <\\+\[0-9\]+>:\tje     0x\[0-9a-f\]+ <loop\\+\[0-9\]+>" \
96  "4\t   0x\[0-9a-f\]+ <\\+\[0-9\]+>:\tdec    %eax" \
97  "5\t   0x\[0-9a-f\]+ <\\+\[0-9\]+>:\tjmp    0x\[0-9a-f\]+ <loop\\+\[0-9\]+>" \
98  "6\t   0x\[0-9a-f\]+ <\\+\[0-9\]+>:\tcmp    \\\$0x0,%eax" \
99  "7\t   0x\[0-9a-f\]+ <\\+\[0-9\]+>:\tje     0x\[0-9a-f\]+ <loop\\+\[0-9\]+>\r" \
100  ]
101
102gdb_test "record instruction-history 3,3" "3\t   0x\[0-9a-f\]+ <loop\\+\[0-9\]+>:\tje     0x\[0-9a-f\]+ <loop\\+\[0-9\]+>\r"
103
104# the following tests are checking the iterators
105# to avoid lots of regexps, we just check the number of lines that
106# were printed during command execution.
107
108# test_lines_output returns the output lines from command as a list.
109proc test_lines_output { command message } {
110    global gdb_prompt
111    set message "test_lines_output: $message"
112    gdb_test_multiple $command $message {
113        -re "\n\(.*\)\r\n$gdb_prompt $" {
114            return [split [string trim $expect_out(1,string)] "\n"]
115        }
116    }
117}
118
119# test_lines_length returns the number of lines from command.
120proc test_lines_length { command message } {
121    return [llength [test_lines_output $command $message]]
122}
123
124# show instruction history with unlimited size, we expect to see
125# all $traced instructions
126gdb_test_no_output "set record instruction-history-size 0"
127set message "record instruction-history - unlimited"
128set lines [test_lines_length "record instruction-history 1" $message]
129if { $traced != $lines } {
130    fail $message
131} else {
132    pass $message
133}
134
135gdb_test_no_output "set record instruction-history-size $traced"
136set message "record instruction-history - traced"
137set lines [test_lines_length "record instruction-history 1" $message]
138if { $traced != $lines } {
139    fail $message
140} else {
141    pass $message
142}
143
144# test that the iterator works
145set history_size 4
146gdb_test_no_output "set record instruction-history-size $history_size"
147set message "browse history forward start"
148set lines [test_lines_length "record instruction-history 1" $message]
149if { $lines != $history_size } {
150    fail $message
151} else {
152    pass $message
153}
154
155set message "browse history forward middle"
156set lines [test_lines_length "record instruction-history +" $message]
157if { $lines != $history_size } {
158    fail $message
159} else {
160    pass $message
161}
162
163set message "browse history forward last"
164set lines [test_lines_length "record instruction-history +" $message]
165if { $lines != 3 } {
166    fail $message
167} else {
168    pass $message
169}
170
171gdb_test "record instruction-history" "At the end of the branch trace record\\." "browse history forward beyond 1"
172
173# make sure we cannot move further
174gdb_test "record instruction-history" "At the end of the branch trace record\\." "browse history forward beyond 2"
175
176set message "browse history backward last"
177set lines [test_lines_length "record instruction-history -" $message]
178if { $lines != $history_size } {
179    fail $message
180} else {
181    pass $message
182}
183
184set message "browse history backward middle"
185set lines [test_lines_length "record instruction-history -" $message]
186if { $lines != $history_size } {
187    fail $message
188} else {
189    pass $message
190}
191
192set message "browse history backward first"
193set lines [test_lines_length "record instruction-history -" $message]
194if { $lines != 3 } {
195    fail $message
196} else {
197    pass $message
198}
199
200gdb_test "record instruction-history -" "At the start of the branch trace record\\." "browse history backward beyond 1"
201
202# make sure we cannot move further back
203gdb_test "record instruction-history -" "At the start of the branch trace record\\." "browse history backward beyond 2"
204