1 # Copyright 2009-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 # This file is part of the gdb testsuite.
18 # Test amd64 displaced stepping.
20 require is_x86_64_m64_target
22 set newline "\[\r\n\]*"
24 set opts {debug nopie}
25 standard_testfile .S -signal.c
27 if { [prepare_for_testing "failed to prepare" $testfile "$srcfile $srcfile2" $opts] } {
31 gdb_test "set displaced-stepping on" ""
32 gdb_test "show displaced-stepping" ".* displaced stepping .* is on.*"
38 ##########################################
42 gdb_test "break test_call" \
43 "Breakpoint.*at.* file .*$srcfile, line.*"
44 gdb_test "break test_call_end" \
45 "Breakpoint.*at.* file .*$srcfile, line.*"
47 gdb_test "break test_ret" \
48 "Breakpoint.*at.* file .*$srcfile, line.*"
49 gdb_test "break test_ret_end" \
50 "Breakpoint.*at.* file .*$srcfile, line.*"
53 "Continuing.*Breakpoint.*, test_call ().*" \
54 "continue to test_call"
56 "Continuing.*Breakpoint.*, test_call_end ().*" \
57 "continue to test_call_end"
60 "Continuing.*Breakpoint.*, test_ret ().*" \
61 "continue to test_ret"
63 "Continuing.*Breakpoint.*, test_ret_end ().*" \
64 "continue to test_ret_end"
66 ##########################################
68 # Test abs-jmp/rep-ret.
70 gdb_test "break test_abs_jmp" \
71 "Breakpoint.*at.* file .*$srcfile, line.*"
72 gdb_test "break test_abs_jmp_end" \
73 "Breakpoint.*at.* file .*$srcfile, line.*"
75 gdb_test "break test_rep_ret" \
76 "Breakpoint.*at.* file .*$srcfile, line.*"
77 gdb_test "break test_rep_ret_end" \
78 "Breakpoint.*at.* file .*$srcfile, line.*"
81 "Continuing.*Breakpoint.*, test_abs_jmp ().*" \
82 "continue to test_abs_jmp"
84 "Continuing.*Breakpoint.*, test_abs_jmp_end ().*" \
85 "continue to test_abs_jmp_end"
88 "Continuing.*Breakpoint.*, test_rep_ret ().*" \
89 "continue to test_rep_ret"
91 "Continuing.*Breakpoint.*, test_rep_ret_end ().*" \
92 "continue to test_rep_ret_end"
94 ##########################################
98 gdb_test "break test_syscall" \
99 "Breakpoint.*at.* file .*$srcfile, line.*"
100 gdb_test "break test_syscall_end" \
101 "Breakpoint.*at.* file .*$srcfile, line.*"
103 gdb_test "continue" \
104 "Continuing.*Breakpoint.*, test_syscall ().*" \
105 "continue to test_syscall"
106 gdb_test "continue" \
107 "Continuing.*Breakpoint.*, test_syscall_end ().*" \
108 "continue to test_syscall_end"
110 ##########################################
112 # int3 (with prefixes)
113 # These don't occur in normal code, but gdb should still DTRT.
115 gdb_test "break test_int3" \
116 "Breakpoint.*at.* file .*$srcfile, line.*"
117 gdb_test "break test_int3_end" \
118 "Breakpoint.*at.* file .*$srcfile, line.*"
120 gdb_test "continue" \
121 "Continuing.*Breakpoint.*, test_int3 ().*" \
122 "continue to test_int3"
124 gdb_test "continue" \
125 "Continuing.*Breakpoint.*, test_int3_end ().*" \
126 "continue to test_int3_end"
128 ##########################################
131 # GDB picks a spare register to hold the rip-relative address.
132 # Exercise all the possibilities (rax-rdi, sans rsp).
134 # The order must much the order in srcfile.
135 set rip_regs { "rax" "rbx" "rcx" "rdx" "rbp" "rsi" "rdi" }
137 # Assign val to all specified regs.
139 proc set_regs { regs val } {
142 foreach reg ${regs} {
143 # Use send_gdb/gdb_expect so that these aren't logged as pass/fail.
144 send_gdb "set \$${reg} = ${val}\n"
146 -re "$gdb_prompt $" {
147 verbose "Setting ${reg} to ${val}." 2
150 warning "Couldn't set ${reg} to ${val}."
156 # Verify all REGS equal VAL, except EXCEPT_REG which equals
159 # It is fine for EXCEPT_REG to be the empty string, in which case no
160 # register will be checked for EXCEPT_REG_VAL.
162 proc_with_prefix verify_regs { regs val except_reg except_reg_val } {
165 foreach reg ${regs} {
167 if { "${reg}" == "${except_reg}" } {
168 set expected ${except_reg_val}
170 # The cast to (int) is because RBP is printed as a pointer.
171 gdb_test "p (int) \$${reg}" " = ${expected}${newline}" "${reg} expected value"
175 # Run the rip-relative tests.
177 # TEST_START_LABEL and TEST_END_LABEL are two labels that delimit the
178 # test in the srcfile.
180 # REG is either the name of a register which is the destination
181 # location (when testing the add instruction), otherwise REG should be
182 # the empty string, when testing the 'jmpq*' instruction.
184 # SIGNAL_MODES is a list which always contains 'off' and optionally
185 # might also contain 'on'. The 'on' value is only included if the
186 # target supports sending SIGALRM to the inferior. The test is
187 # repeated for each signal mode. With signal mode 'on' we send a
188 # signal to the inferior while it is performing a displaced step.
189 proc rip_test { reg test_start_label test_end_label signal_modes } {
190 global srcfile rip_regs
192 gdb_test "break ${test_start_label}" \
193 "Breakpoint.*at.* file .*$srcfile, line.*"
194 gdb_test "break ${test_end_label}" \
195 "Breakpoint.*at.* file .*$srcfile, line.*"
197 foreach_with_prefix send_signal $signal_modes {
198 if {$send_signal eq [lindex $signal_modes 0]} {
199 # The first time through we can just continue to the
201 gdb_test "continue" \
202 "Continuing.*Breakpoint.*, ${test_start_label} ().*" \
203 "continue to ${test_start_label}"
205 # For the second time through the test we need to jump
206 # back to the beginning.
207 gdb_test "jump ${test_start_label}" \
208 "Breakpoint.*, ${test_start_label} ().*" \
209 "jump back to ${test_start_label}"
212 set_regs ${rip_regs} 0
215 # The signal sending tests require that the signal appear to
216 # arrive from an outside source, i.e. we can't use GDB's 'signal'
217 # command to deliver it.
219 # The signal must arrive while GDB is processing the displaced
222 # If we use 'signal' to send the signal GDB doesn't actually do
223 # the displaced step, but instead just delivers the signal.
224 set inferior_pid [get_inferior_pid]
225 # Ensure that $inferior_pid refers to a single process.
226 gdb_assert {[expr $inferior_pid > 0]} \
227 "check for a sane inferior pid"
228 if {$inferior_pid > 0} {
229 remote_exec target "kill -ALRM $inferior_pid"
233 gdb_test "continue" \
234 "Continuing.*Breakpoint.*, ${test_end_label} ().*" \
235 "continue to ${test_end_label}"
237 verify_regs ${rip_regs} 0 ${reg} 42
241 if {![target_info exists gdb,nosignals] && ![istarget "*-*-mingw*"]} {
242 # Only targets that support SIGALRM can run the signal tests.
243 set signal_modes { off on }
245 set signal_modes { off }
248 # The rip-relative add instructions. There's a test writing to
249 # each register in RIP_REGS in turn.
250 foreach reg ${rip_regs} {
251 with_test_prefix "add into ${reg}" {
252 rip_test $reg "test_rip_${reg}" "test_rip_${reg}_end" $signal_modes
256 # Now test the rip-relative 'jmpq*' instruction.
257 with_test_prefix "rip-relative jmpq*" {
258 rip_test "" "test_jmp" "test_jmp_end" $signal_modes
261 ##########################################
263 # Done, run program to exit.
265 gdb_continue_to_end "amd64-disp-step"