]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/testsuite/gdb.base/step-over-syscall.exp
061d706c614f144a5cd22e79be7d5b3fbe559bfe
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.base / step-over-syscall.exp
1 # This testcase is part of GDB, the GNU debugger.
2
3 # Copyright 2011-2019 Free Software Foundation, Inc.
4
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.
9 #
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.
14 #
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/>.
17
18 set syscall_insn ""
19
20 # Define the syscall instruction for each target.
21
22 if { [istarget "i\[34567\]86-*-linux*"] || [istarget "x86_64-*-linux*"] } {
23 set syscall_insn "\[ \t\](int|syscall|sysenter)\[ \t\]"
24 } elseif { [istarget "aarch64*-*-linux*"] || [istarget "arm*-*-linux*"] } {
25 set syscall_insn "\[ \t\](swi|svc)\[ \t\]"
26 } else {
27 return -1
28 }
29
30 proc_with_prefix check_pc_after_cross_syscall { syscall syscall_insn_next_addr } {
31 set syscall_insn_next_addr_found [get_hexadecimal_valueof "\$pc" "0"]
32
33 set test "single step over $syscall final pc"
34 if {$syscall_insn_next_addr != 0
35 && $syscall_insn_next_addr == $syscall_insn_next_addr_found} {
36 pass $test
37 } else {
38 fail $test
39 }
40 }
41
42 # Restart GDB and set up the test. Return a list in which the first one
43 # is the address of syscall instruction and the second one is the address
44 # of the next instruction address of syscall instruction. If anything
45 # wrong, the two elements of list are -1.
46
47 proc setup { syscall } {
48 global gdb_prompt syscall_insn
49
50 set testfile "step-over-$syscall"
51
52 clean_restart $testfile
53
54 if { ![runto main] } then {
55 fail "run to main ($syscall)"
56 return -1
57 }
58
59 # Delete the breakpoint on main.
60 gdb_test_no_output "delete break 1"
61
62 gdb_test_no_output "set displaced-stepping off" \
63 "set displaced-stepping off during test setup"
64
65 gdb_test "break $syscall" "Breakpoint \[0-9\]* at .*"
66
67 gdb_test "continue" "Continuing\\..*Breakpoint \[0-9\]+, (.* in |__libc_|)$syscall \\(\\).*" \
68 "continue to $syscall (1st time)"
69 # Hit the breakpoint on $syscall for the first time. In this time,
70 # we will let PLT resolution done, and the number single steps we will
71 # do later will be reduced.
72
73 gdb_test "continue" "Continuing\\..*Breakpoint \[0-9\]+, (.* in |__libc_|)$syscall \\(\\).*" \
74 "continue to $syscall (2nd time)"
75 # Hit the breakpoint on $syscall for the second time. In this time,
76 # the address of syscall insn and next insn of syscall are recorded.
77
78 gdb_test "display/i \$pc" ".*"
79
80 # Single step until we see a syscall insn or we reach the
81 # upper bound of loop iterations.
82 set msg "find syscall insn in $syscall"
83 set steps 0
84 set max_steps 1000
85 gdb_test_multiple "stepi" $msg {
86 -re ".*$syscall_insn.*$gdb_prompt $" {
87 pass $msg
88 }
89 -re "x/i .*=>.*\r\n$gdb_prompt $" {
90 incr steps
91 if {$steps == $max_steps} {
92 fail $msg
93 } else {
94 send_gdb "stepi\n"
95 exp_continue
96 }
97 }
98 }
99
100 if {$steps == $max_steps} {
101 return { -1, -1 }
102 }
103
104 set syscall_insn_addr [get_hexadecimal_valueof "\$pc" "0" \
105 "pc before stepi"]
106 if {[gdb_test "stepi" "x/i .*=>.*" "stepi $syscall insn"] != 0} {
107 return { -1, -1 }
108 }
109 return [list $syscall_insn_addr [get_hexadecimal_valueof "\$pc" \
110 "0" "pc after stepi"]]
111 }
112
113 proc step_over_syscall { syscall } {
114 with_test_prefix "$syscall" {
115 global syscall_insn
116 global gdb_prompt
117
118 set testfile "step-over-$syscall"
119
120 if [build_executable ${testfile}.exp ${testfile} ${testfile}.c {debug}] {
121 untested "failed to compile"
122 return -1
123 }
124
125 foreach_with_prefix displaced {"off" "on"} {
126 if {$displaced == "on" && ![support_displaced_stepping]} {
127 continue
128 }
129
130 if { $displaced == "on" && $syscall == "clone" } {
131 # GDB doesn't support stepping over clone syscall with
132 # displaced stepping.
133 kfail "gdb/19675" "single step over clone"
134 continue
135 }
136
137 set ret [setup $syscall]
138
139 set syscall_insn_addr [lindex $ret 0]
140 set syscall_insn_next_addr [lindex $ret 1]
141 if { $syscall_insn_addr == -1 } {
142 return -1
143 }
144
145 gdb_test "continue" "Continuing\\..*Breakpoint \[0-9\]+, (.* in |__libc_|)$syscall \\(\\).*" \
146 "continue to $syscall (3rd time)"
147
148 # Hit the breakpoint on $syscall for the third time. In this time, we'll set
149 # breakpoint on the syscall insn we recorded previously, and single step over it.
150
151 set syscall_insn_bp 0
152 gdb_test_multiple "break \*$syscall_insn_addr" "break on syscall insn" {
153 -re "Breakpoint (\[0-9\]*) at .*$gdb_prompt $" {
154 set syscall_insn_bp $expect_out(1,string)
155 pass "break on syscall insns"
156 }
157 }
158
159 gdb_test "continue" "Continuing\\..*Breakpoint \[0-9\]+, .*" \
160 "continue to syscall insn $syscall"
161
162 gdb_test_no_output "set displaced-stepping $displaced"
163
164 # Check the address of next instruction of syscall.
165 if {[gdb_test "stepi" "x/i .*=>.*" "single step over $syscall"] != 0} {
166 return -1
167 }
168 check_pc_after_cross_syscall $syscall $syscall_insn_next_addr
169
170 # Delete breakpoint syscall insns to avoid interference to other syscalls.
171 delete_breakpoints
172
173 gdb_test "break marker" "Breakpoint.*at.* file .*${testfile}.c, line.*"
174 gdb_test "continue" "Continuing\\..*Breakpoint \[0-9\]+, marker \\(\\) at.*" \
175 "continue to marker ($syscall)"
176 }
177 }
178 }
179
180 # Set a breakpoint with a condition that evals false on syscall
181 # instruction. In fact, it tests GDBserver steps over syscall
182 # instruction. SYSCALL is the syscall the program calls.
183 # FOLLOW_FORK is either "parent" or "child". DETACH_ON_FORK is
184 # "on" or "off".
185
186 proc break_cond_on_syscall { syscall follow_fork detach_on_fork } {
187 with_test_prefix "break cond on target : $syscall" {
188 set testfile "step-over-$syscall"
189
190 set ret [setup $syscall]
191
192 set syscall_insn_addr [lindex $ret 0]
193 set syscall_insn_next_addr [lindex $ret 1]
194 if { $syscall_insn_addr == -1 } {
195 return -1
196 }
197
198 gdb_test "continue" "Continuing\\..*Breakpoint \[0-9\]+, (.* in |__libc_|)$syscall \\(\\).*" \
199 "continue to $syscall"
200 # Delete breakpoint syscall insns to avoid interference with other syscalls.
201 delete_breakpoints
202
203 gdb_test "set follow-fork-mode $follow_fork"
204 gdb_test "set detach-on-fork $detach_on_fork"
205
206 # Create a breakpoint with a condition that evals false.
207 gdb_test "break \*$syscall_insn_addr if main == 0" \
208 "Breakpoint \[0-9\]* at .*"
209
210 if { $syscall == "clone" } {
211 # Create a breakpoint in the child with the condition that
212 # evals false, so that GDBserver can get the event from the
213 # child but GDB doesn't see it. In this way, we don't have
214 # to adjust the test flow for "clone".
215 # This is a regression test for PR server/19736. In this way,
216 # we can test that GDBserver gets an event from the child and
217 # set suspend count correctly while the parent is stepping over
218 # the breakpoint.
219 gdb_test "break clone_fn if main == 0"
220 }
221
222 if { $syscall == "clone" } {
223 # follow-fork and detach-on-fork only make sense to
224 # fork and vfork.
225 gdb_test "break marker" "Breakpoint.*at.* file .*${testfile}.c, line.*"
226 gdb_test "continue" "Continuing\\..*Breakpoint \[0-9\]+, marker \\(\\) at.*" \
227 "continue to marker"
228 } else {
229 if { $follow_fork == "child" } {
230 gdb_test "continue" "exited normally.*" "continue to end of inf 2"
231 if { $detach_on_fork == "off" } {
232 gdb_test "inferior 1"
233 gdb_test "break marker" "Breakpoint.*at.*"
234 gdb_test "continue" "Continuing\\..*Breakpoint \[0-9\]+, marker \\(\\) at.*" \
235 "continue to marker"
236 }
237 } else {
238 gdb_test "break marker" "Breakpoint.*at.* file .*${testfile}.c, line.*"
239 gdb_test "continue" "Continuing\\..*Breakpoint \[0-9\]+, marker \\(\\) at.*" \
240 "continue to marker"
241 }
242 }
243 }
244 }
245
246 step_over_syscall "fork"
247 step_over_syscall "vfork"
248 step_over_syscall "clone"
249
250 set testfile "step-over-fork"
251 clean_restart $testfile
252 if { ![runto main] } then {
253 fail "run to main"
254 return -1
255 }
256
257 set cond_bp_target 1
258
259 set test "set breakpoint condition-evaluation target"
260 gdb_test_multiple $test $test {
261 -re "warning: Target does not support breakpoint condition evaluation.\r\nUsing host evaluation mode instead.\r\n$gdb_prompt $" {
262 # Target doesn't support breakpoint condition
263 # evaluation on its side.
264 set cond_bp_target 0
265 }
266 -re "^$test\r\n$gdb_prompt $" {
267 }
268 }
269
270 if { $cond_bp_target } {
271
272 foreach_with_prefix detach-on-fork {"on" "off"} {
273 foreach_with_prefix follow-fork {"parent" "child"} {
274 foreach syscall { "fork" "vfork" "clone" } {
275
276 if { $syscall == "vfork"
277 && ${follow-fork} == "parent"
278 && ${detach-on-fork} == "off" } {
279 # Both vforked child process and parent process are
280 # under GDB's control, but GDB follows the parent
281 # process only, which can't be run until vforked child
282 # finishes. Skip the test in this scenario.
283 continue
284 }
285 break_cond_on_syscall $syscall ${follow-fork} ${detach-on-fork}
286 }
287 }
288 }
289 }