1# This testcase is part of GDB, the GNU debugger. 2 3# Copyright 2011-2023 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 18set syscall_insn "" 19set syscall_register "" 20array set syscall_number {} 21 22# Define the syscall instructions, registers and numbers for each target. 23 24if { [istarget "i\[34567\]86-*-linux*"] || [istarget "x86_64-*-linux*"] } { 25 set syscall_insn "\[ \t\](int|syscall|sysenter)\[ \t\]*" 26 set syscall_register "eax" 27 array set syscall_number {fork "(56|120)" vfork "(58|190)" \ 28 clone "(56|120)"} 29} elseif { [istarget "aarch64*-*-linux*"] || [istarget "arm*-*-linux*"] } { 30 set syscall_insn "\[ \t\](swi|svc)\[ \t\]" 31 32 if { [istarget "aarch64*-*-linux*"] } { 33 set syscall_register "x8" 34 } else { 35 set syscall_register "r7" 36 } 37 38 array set syscall_number {fork "(120|220)" vfork "(190|220)" \ 39 clone "(120|220)"} 40} else { 41 return -1 42} 43 44proc_with_prefix check_pc_after_cross_syscall { displaced syscall syscall_insn_next_addr } { 45 global gdb_prompt 46 47 set syscall_insn_next_addr_found [get_hexadecimal_valueof "\$pc" "0"] 48 49 # After the 'stepi' we expect thread 1 to still be selected. 50 # However, when displaced stepping over a clone bug gdb/19675 51 # means this might not be the case. 52 # 53 # Which thread we end up in depends on a race between the original 54 # thread-1, and the new thread (created by the clone), so we can't 55 # guarantee which thread we will be in at this point. 56 # 57 # For the fork/vfork syscalls, which are correctly handled by 58 # displaced stepping we will always be in thread-1 or the original 59 # process at this point. 60 set curr_thread "unknown" 61 gdb_test_multiple "info threads" "" { 62 -re "Id\\s+Target Id\\s+Frame\\s*\r\n" { 63 exp_continue 64 } 65 -re "^\\* (\\d+)\\s+\[^\r\n\]+\r\n" { 66 set curr_thread $expect_out(1,string) 67 exp_continue 68 } 69 -re "^\\s+\\d+\\s+\[^\r\n\]+\r\n" { 70 exp_continue 71 } 72 -re "$gdb_prompt " { 73 } 74 } 75 76 # If we are displaced stepping over a clone, and we ended up in 77 # the wrong thread then the following check of the $pc value will 78 # fail. 79 if { $displaced == "on" && $syscall == "clone" && $curr_thread != 1 } { 80 # GDB doesn't support stepping over clone syscall with 81 # displaced stepping. 82 setup_kfail "*-*-*" "gdb/19675" 83 } 84 85 gdb_assert {$syscall_insn_next_addr != 0 \ 86 && $syscall_insn_next_addr == $syscall_insn_next_addr_found \ 87 && $curr_thread == 1} \ 88 "single step over $syscall final pc" 89} 90 91# Verify the syscall number is the correct one. 92 93proc syscall_number_matches { syscall } { 94 global syscall_register syscall_number 95 96 if {[gdb_test "p \$$syscall_register" ".*= $syscall_number($syscall)" \ 97 "syscall number matches"] != 0} { 98 return 0 99 } 100 101 return 1 102} 103 104# Restart GDB and set up the test. Return a list in which the first one 105# is the address of syscall instruction and the second one is the address 106# of the next instruction address of syscall instruction. If anything 107# wrong, the two elements of list are -1. 108 109proc setup { syscall } { 110 global gdb_prompt syscall_insn 111 112 global hex 113 set next_insn_addr -1 114 set testfile "step-over-$syscall" 115 116 clean_restart $testfile 117 118 if {![runto_main]} { 119 return -1 120 } 121 122 # Delete the breakpoint on main. 123 gdb_test_no_output "delete break 1" 124 125 gdb_test_no_output "set displaced-stepping off" \ 126 "set displaced-stepping off during test setup" 127 128 gdb_test "break \*$syscall" "Breakpoint \[0-9\]* at .*" 129 130 gdb_test "continue" "Continuing\\..*Breakpoint \[0-9\]+, (.* in |__libc_|)$syscall \\(\\).*" \ 131 "continue to $syscall (1st time)" 132 # Hit the breakpoint on $syscall for the first time. In this time, 133 # we will let PLT resolution done, and the number single steps we will 134 # do later will be reduced. 135 136 gdb_test "continue" "Continuing\\..*Breakpoint \[0-9\]+, (.* in |__libc_|)$syscall \\(\\).*" \ 137 "continue to $syscall (2nd time)" 138 # Hit the breakpoint on $syscall for the second time. In this time, 139 # the address of syscall insn and next insn of syscall are recorded. 140 141 # Check if the first instruction we stopped at is the syscall one. 142 set syscall_insn_addr -1 143 gdb_test_multiple "display/i \$pc" "fetch first stop pc" { 144 -re "display/i .*: x/i .*=> ($hex) .*:.*$syscall_insn.*$gdb_prompt $" { 145 set insn_addr $expect_out(1,string) 146 147 # Is the syscall number the correct one? 148 if {[syscall_number_matches $syscall]} { 149 set syscall_insn_addr $insn_addr 150 } 151 pass $gdb_test_name 152 } 153 -re ".*$gdb_prompt $" { 154 pass $gdb_test_name 155 } 156 } 157 158 # If we are not at the syscall instruction yet, keep looking for it with 159 # stepi commands. 160 if {$syscall_insn_addr == -1} { 161 # Single step until we see a syscall insn or we reach the 162 # upper bound of loop iterations. 163 set steps 0 164 set max_steps 1000 165 gdb_test_multiple "stepi" "find syscall insn in $syscall" { 166 -re ".*$syscall_insn.*$gdb_prompt $" { 167 # Is the syscall number the correct one? 168 if {[syscall_number_matches $syscall]} { 169 pass $gdb_test_name 170 } else { 171 exp_continue 172 } 173 } 174 -re "x/i .*=>.*\r\n$gdb_prompt $" { 175 incr steps 176 if {$steps == $max_steps} { 177 fail $gdb_test_name 178 } else { 179 send_gdb "stepi\n" 180 exp_continue 181 } 182 } 183 } 184 185 if {$steps == $max_steps} { 186 return { -1, -1 } 187 } 188 } 189 190 # We have found the syscall instruction. Now record the next instruction. 191 # Use the X command instead of stepi since we can't guarantee 192 # stepi is working properly. 193 gdb_test_multiple "x/2i \$pc" "pc before/after syscall instruction" { 194 -re "x/2i .*=> ($hex) .*:.*$syscall_insn.* ($hex) .*:.*$gdb_prompt $" { 195 set syscall_insn_addr $expect_out(1,string) 196 set actual_syscall_insn $expect_out(2,string) 197 set next_insn_addr $expect_out(3,string) 198 pass $gdb_test_name 199 } 200 } 201 202 # If we encounter a sequence: 203 # 0xf7fd5155 <__kernel_vsyscall+5>: sysenter 204 # 0xf7fd5157 <__kernel_vsyscall+7>: int $0x80 205 # 0xf7fd5159 <__kernel_vsyscall+9>: pop %ebp 206 # then a stepi at sysenter will step over the int insn, so make sure 207 # next_insn_addr points after the int insn. 208 if { $actual_syscall_insn == "sysenter" } { 209 set test "pc after sysenter instruction" 210 set re_int_insn "\[ \t\]*int\[ \t\]\[^\r\n\]*" 211 set re [multi_line \ 212 "x/2i $hex" \ 213 "\[^\r\n\]* $hex \[^\r\n\]*:$re_int_insn" \ 214 "\[^\r\n\]* ($hex) \[^\r\n\]*:\[^\r\n\]*"] 215 gdb_test_multiple "x/2i $next_insn_addr" $test { 216 -re -wrap $re { 217 set next_insn_addr $expect_out(1,string) 218 } 219 -re -wrap "" { 220 } 221 } 222 } 223 224 if {[gdb_test "stepi" "x/i .*=>.*" "stepi $syscall insn"] != 0} { 225 return { -1, -1 } 226 } 227 228 set pc_after_stepi [get_hexadecimal_valueof "\$pc" "0" \ 229 "pc after stepi"] 230 231 gdb_assert {$next_insn_addr == $pc_after_stepi} \ 232 "pc after stepi matches insn addr after syscall" 233 234 return [list $syscall_insn_addr $pc_after_stepi] 235} 236 237proc step_over_syscall { syscall } { 238 with_test_prefix "$syscall" { 239 global syscall_insn 240 global gdb_prompt 241 242 set testfile "step-over-$syscall" 243 244 set options [list debug] 245 if { $syscall == "clone" } { 246 lappend options "pthreads" 247 } 248 249 if [build_executable ${testfile}.exp ${testfile} ${testfile}.c $options] { 250 untested "failed to compile" 251 return -1 252 } 253 254 foreach_with_prefix displaced {"off" "on"} { 255 if {$displaced == "on" && ![support_displaced_stepping]} { 256 continue 257 } 258 259 set ret [setup $syscall] 260 261 set syscall_insn_addr [lindex $ret 0] 262 set syscall_insn_next_addr [lindex $ret 1] 263 if { $syscall_insn_addr == -1 } { 264 return -1 265 } 266 267 gdb_test "continue" "Continuing\\..*Breakpoint \[0-9\]+, (.* in |__libc_|)$syscall \\(\\).*" \ 268 "continue to $syscall (3rd time)" 269 270 # Hit the breakpoint on $syscall for the third time. In this time, we'll set 271 # breakpoint on the syscall insn we recorded previously, and single step over it. 272 273 set syscall_insn_bp 0 274 gdb_test_multiple "break \*$syscall_insn_addr" "break on syscall insn" { 275 -re "Breakpoint (\[0-9\]*) at .*$gdb_prompt $" { 276 set syscall_insn_bp $expect_out(1,string) 277 pass "break on syscall insns" 278 } 279 } 280 281 # Check if the syscall breakpoint is at the syscall instruction 282 # address. If so, no need to continue, otherwise we will run the 283 # inferior to completion. 284 if {$syscall_insn_addr != [get_hexadecimal_valueof "\$pc" "0"]} { 285 gdb_test "continue" "Continuing\\..*Breakpoint \[0-9\]+, .*" \ 286 "continue to syscall insn $syscall" 287 } 288 289 gdb_test_no_output "set displaced-stepping $displaced" 290 291 # Check the address of next instruction of syscall. 292 if {[gdb_test "stepi" "x/i .*=>.*" "single step over $syscall"] != 0} { 293 return -1 294 } 295 check_pc_after_cross_syscall $displaced $syscall $syscall_insn_next_addr 296 297 # Delete breakpoint syscall insns to avoid interference to other syscalls. 298 delete_breakpoints 299 300 gdb_test "break marker" "Breakpoint.*at.* file .*${testfile}.c, line.*" 301 302 # If we are displaced stepping over a clone syscall then 303 # we expect the following check to fail. See also the 304 # code in check_pc_after_cross_syscall. 305 if { $displaced == "on" && $syscall == "clone" } { 306 # GDB doesn't support stepping over clone syscall with 307 # displaced stepping. 308 setup_kfail "*-*-*" "gdb/19675" 309 } 310 311 gdb_test "continue" "Continuing\\..*Breakpoint \[0-9\]+, marker \\(\\) at.*" \ 312 "continue to marker ($syscall)" 313 } 314 } 315} 316 317# Set a breakpoint with a condition that evals false on syscall 318# instruction. In fact, it tests GDBserver steps over syscall 319# instruction. SYSCALL is the syscall the program calls. 320# FOLLOW_FORK is either "parent" or "child". DETACH_ON_FORK is 321# "on" or "off". 322 323proc break_cond_on_syscall { syscall follow_fork detach_on_fork } { 324 with_test_prefix "break cond on target : $syscall" { 325 set testfile "step-over-$syscall" 326 327 set ret [setup $syscall] 328 329 set syscall_insn_addr [lindex $ret 0] 330 set syscall_insn_next_addr [lindex $ret 1] 331 if { $syscall_insn_addr == -1 } { 332 return -1 333 } 334 335 gdb_test "continue" "Continuing\\..*Breakpoint \[0-9\]+, (.* in |__libc_|)$syscall \\(\\).*" \ 336 "continue to $syscall" 337 # Delete breakpoint syscall insns to avoid interference with other syscalls. 338 delete_breakpoints 339 340 gdb_test "set follow-fork-mode $follow_fork" 341 gdb_test "set detach-on-fork $detach_on_fork" 342 343 # Create a breakpoint with a condition that evals false. 344 gdb_test "break \*$syscall_insn_addr if main == 0" \ 345 "Breakpoint \[0-9\]* at .*" 346 347 if { $syscall == "clone" } { 348 # Create a breakpoint in the child with the condition that 349 # evals false, so that GDBserver can get the event from the 350 # child but GDB doesn't see it. In this way, we don't have 351 # to adjust the test flow for "clone". 352 # This is a regression test for PR server/19736. In this way, 353 # we can test that GDBserver gets an event from the child and 354 # set suspend count correctly while the parent is stepping over 355 # the breakpoint. 356 gdb_test "break clone_fn if main == 0" 357 } 358 359 if { $syscall == "clone" } { 360 # follow-fork and detach-on-fork only make sense to 361 # fork and vfork. 362 gdb_test "break marker" "Breakpoint.*at.* file .*${testfile}.c, line.*" 363 gdb_test "continue" "Continuing\\..*Breakpoint \[0-9\]+, marker \\(\\) at.*" \ 364 "continue to marker" 365 } else { 366 if { $follow_fork == "child" } { 367 gdb_test "continue" "exited normally.*" "continue to end of inf 2" 368 if { $detach_on_fork == "off" } { 369 gdb_test "inferior 1" 370 gdb_test "break marker" "Breakpoint.*at.*" 371 gdb_test "continue" "Continuing\\..*Breakpoint $::bkptno_numopt_re, marker \\(\\) at.*" \ 372 "continue to marker" 373 } 374 } else { 375 gdb_test "break marker" "Breakpoint.*at.* file .*${testfile}.c, line.*" 376 gdb_test "continue" "Continuing\\..*Breakpoint $::bkptno_numopt_re, marker \\(\\) at.*" \ 377 "continue to marker" 378 } 379 } 380 } 381} 382 383step_over_syscall "fork" 384step_over_syscall "vfork" 385step_over_syscall "clone" 386 387set testfile "step-over-fork" 388clean_restart $testfile 389if {![runto_main]} { 390 return -1 391} 392 393set cond_bp_target 1 394 395set test "set breakpoint condition-evaluation target" 396gdb_test_multiple $test $test { 397 -re "warning: Target does not support breakpoint condition evaluation.\r\nUsing host evaluation mode instead.\r\n$gdb_prompt $" { 398 # Target doesn't support breakpoint condition 399 # evaluation on its side. 400 set cond_bp_target 0 401 } 402 -re "^$test\r\n$gdb_prompt $" { 403 } 404} 405 406if { $cond_bp_target } { 407 408 foreach_with_prefix detach-on-fork {"on" "off"} { 409 foreach_with_prefix follow-fork {"parent" "child"} { 410 foreach syscall { "fork" "vfork" "clone" } { 411 412 if { $syscall == "vfork" 413 && ${follow-fork} == "parent" 414 && ${detach-on-fork} == "off" } { 415 # Both vforked child process and parent process are 416 # under GDB's control, but GDB follows the parent 417 # process only, which can't be run until vforked child 418 # finishes. Skip the test in this scenario. 419 continue 420 } 421 break_cond_on_syscall $syscall ${follow-fork} ${detach-on-fork} 422 } 423 } 424 } 425} 426