1 # This testcase is part of GDB, the GNU debugger.
3 # Copyright 2016-2024 Free Software Foundation, Inc.
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 # Skip this test if btrace is disabled.
20 require allow_btrace_tests allow_python_tests
22 load_lib gdb-python.exp
26 if [prepare_for_testing "failed to prepare" $testfile $srcfile] { return -1 }
32 with_test_prefix "no or double record" {
33 gdb_test "python print(gdb.current_recording())" "None"
35 gdb_test_no_output "python gdb.start_recording(\"btrace\")"
36 gdb_test "python gdb.start_recording(\"btrace\")" \
37 ".*gdb\.error: The process is already being recorded\..*" \
40 gdb_test_no_output "python gdb.stop_recording()" "first"
41 gdb_test "python gdb.stop_recording()" \
42 ".*gdb\.error: No recording is currently active\..*" "second"
45 with_test_prefix "preopened record btrace" {
46 gdb_test_no_output "record btrace"
47 gdb_test "python print(gdb.current_recording().method)" "btrace"
48 gdb_test "python print(gdb.current_recording().format)" "pt|bts"
49 gdb_test_no_output "python gdb.stop_recording()"
52 with_test_prefix "prepare record" {
53 gdb_test_no_output "python r = gdb.start_recording(\"btrace\")"
54 gdb_test "python print(r.method)" "btrace"
55 gdb_test "python print(r.format)" "pt|bts"
56 gdb_test "stepi 100" ".*"
58 # There's a HW bug affecting Processor Trace on some Intel processors.
59 # The bug was exposed by linux kernel commit 670638477aed
60 # ("perf/x86/intel/pt: Opportunistically use single range output mode"),
61 # added in version v5.5.0, and was worked around by commit ce0d998be927
62 # ("perf/x86/intel/pt: Fix sampling using single range output") in version
63 # 6.1.0. Detect the kernel version range for which the problem may
66 set v [linux_kernel_version]
69 [expr [version_compare [list 5 5 0] <= $v] \
70 && [version_compare $v < [list 6 1 0]]]
76 "warning: Decode error \\($nonl_re*\\) at instruction $decimal" \
77 "\\(offset = $hex, pc = $hex\\):" \
82 "warning: Non-contiguous trace at instruction $decimal" \
83 "\\(offset = $hex\\)\\."]]
86 set cmd "python insn = r.instruction_history"
87 gdb_test_multiple $cmd "" {
88 -re "^[string_to_regexp $cmd]\r\n$::gdb_prompt $" {
91 -re -wrap "($xfail_re|$xfail_re_2)" {
104 # Also handle the case that we're running into the hw bug without
105 # triggering a decode error.
106 gdb_test_multiple "python print(len(insn))" "" {
123 gdb_test_no_output "python call = r.function_call_history"
124 gdb_test_no_output "python i = insn\[0\]"
125 gdb_test_no_output "python c = call\[0\]"
128 with_test_prefix "replay begin" {
129 gdb_test "python print(r.replay_position)" "None"
130 gdb_test "python r.goto(r.begin)"
131 gdb_test "python print(r.replay_position.number)" "1"
134 with_test_prefix "replay end" {
135 gdb_test "python r.goto(r.end)"
136 gdb_test "python print(r.replay_position)" "None"
139 with_test_prefix "instruction " {
140 gdb_test "python print(i.number)" "1"
141 gdb_test "python print(i.sal)" "symbol and line for .*"
142 gdb_test "python print(i.pc)" "$decimal"
143 gdb_test "python print(repr(i.data))" "<memory at $hex>"
144 gdb_test "python print(i.decoded)" ".*"
145 gdb_test "python print(i.size)" "$decimal"
146 gdb_test "python print(i.is_speculative)" "False"
149 with_test_prefix "function call" {
150 gdb_test "python print(c.number)" "1"
151 gdb_test "python print(c.symbol)" "main"
152 gdb_test "python print(c.level)" "$decimal"
153 gdb_test "python print(len(c.instructions))" "$decimal"
154 gdb_test "python print(c.up)" "None"
155 gdb_test "python print(c.prev)" "None"
156 gdb_test "python print(c == c.next.prev)" "True"
159 with_test_prefix "list" {
160 gdb_test "python print(len(insn\[23:65\]))" "42"
161 gdb_test "python print(insn\[17:\]\[2\].number)" "20"
162 gdb_test "python print(i in insn)" "True"
163 gdb_test "python print(i in call)" "False"
164 gdb_test "python print(c in insn)" "False"
165 gdb_test "python print(c in call)" "True"
166 gdb_test "python print(insn.index(i))" "0"
167 gdb_test "python print(insn.count(i))" "1"
170 with_test_prefix "sublist" {
171 gdb_test_no_output "python s1 = insn\[3:72:5\]"
172 gdb_test_no_output "python s2 = s1\[2:13:3\]"
173 gdb_test_no_output "python s3 = s1\[13:2:-3\]"
174 gdb_test_no_output "python s4 = insn\[::-1\]"
176 gdb_test "python print(\[i.number for i in s1\])" "\\\[4, 9, 14, 19, 24, 29, 34, 39, 44, 49, 54, 59, 64, 69\\\]"
177 gdb_test "python print(\[i.number for i in s2\])" "\\\[14, 29, 44, 59\\\]"
178 gdb_test "python print(\[i.number for i in s3\])" "\\\[69, 54, 39, 24\\\]"
180 gdb_test "python print(len(s1))" "14"
181 gdb_test "python print(len(s2))" "4"
182 gdb_test "python print(len(s3))" "4"
183 gdb_test "python print(len(s4))" "100"
185 gdb_test "python print(s4\[5\].number)" "95"
186 gdb_test "python print(s4\[-5\].number)" "5"
187 gdb_test "python print(s4\[100\].number)" ".*IndexError.*"
188 gdb_test "python print(s4\[-101\].number)" ".*IndexError.*"
191 with_test_prefix "level" {
192 gdb_test_no_output "python gdb.stop_recording()"
193 gdb_test "break inner" "Breakpoint.*"
194 gdb_test "continue" "Continuing\..*"
195 gdb_test_no_output "record btrace"
196 gdb_test "step" "outer ().*" "step one"
197 gdb_test "step" "main ().*" "step two"
198 gdb_test "python print(gdb.current_recording().function_call_history\[0\].level)" "1"
199 gdb_test "python print(gdb.current_recording().function_call_history\[1\].level)" "0"