1# Copyright 2022-2023 Free Software Foundation, Inc. 2 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. 7# 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. 12# 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/>. 15 16# Test doing a "next" on a thread during which forks or vforks happen in other 17# threads. 18 19standard_testfile 20 21# Line where to stop the main thread. 22set break_here_line [gdb_get_line_number "break here"] 23 24# Build executables, one for each fork flavor. 25foreach_with_prefix fork_func {fork vfork} { 26 set opts [list debug pthreads additional_flags=-DFORK_FUNC=${fork_func}] 27 if { [build_executable "failed to prepare" \ 28 ${testfile}-${fork_func} ${srcfile} $opts] } { 29 return 30 } 31} 32 33# If testing against GDBserver, consume all it its output. 34 35proc drain_gdbserver_output { } { 36 if { [info exists ::server_spawn_id] } { 37 gdb_test_multiple "" "" { 38 -i "$::server_spawn_id" 39 -timeout 0 40 -re ".+" { 41 exp_continue 42 } 43 } 44 } 45} 46 47# Run the test with the given parameters: 48# 49# - FORK_FUNC: fork flavor, "fork" or "vfork". 50# - TARGET-NON-STOP: "maintenance set target-non-stop" value, "auto", "on" or 51# "off". 52# - NON-STOP: "set non-stop" value, "on" or "off". 53# - DISPLACED-STEPPING: "set displaced-stepping" value, "auto", "on" or "off". 54 55proc do_test { fork_func target-non-stop non-stop displaced-stepping } { 56 save_vars { ::GDBFLAGS } { 57 append ::GDBFLAGS " -ex \"maintenance set target-non-stop ${target-non-stop}\"" 58 append ::GDBFLAGS " -ex \"set non-stop ${non-stop}\"" 59 clean_restart ${::binfile}-${fork_func} 60 } 61 62 gdb_test_no_output "set displaced-stepping ${displaced-stepping}" 63 64 if { ![runto_main] } { 65 return 66 } 67 68 # The "Detached after (v)fork" messages get in the way in non-stop, disable 69 # them. 70 gdb_test_no_output "set print inferior-events off" 71 72 # Advance the next-ing thread to the point where we'll execute the nexts. 73 # Leave the breakpoint in: it will force GDB to step over it while next-ing, 74 # which exercises some additional code paths. 75 gdb_test "break $::break_here_line" "Breakpoint $::decimal at $::hex.*" 76 gdb_test "continue" "hit Breakpoint $::decimal, main.*" 77 78 # Next an arbitrary number of times over the lines of the loop. 79 # 80 # It is useful to bump this number to a larger value (e.g. 200) to stress 81 # test more, but it makes the test case run for considerably longer. If 82 # you increase the number of loops, you might want to adjust the alarm 83 # time in the .c file accordingly. 84 for { set i 0 } { $i < 20 } { incr i } { 85 # If testing against GDBserver, the forking threads cause a lot of 86 # "Detaching from process XYZ" messages to appear. If we don't consume 87 # that output, GDBserver eventually blocks on a full stderr. Drain it 88 # once every loop. It may not be needed for 20 iterations, but it's 89 # needed if you increase to 200 iterations. 90 drain_gdbserver_output 91 92 with_test_prefix "i=$i" { 93 if { [gdb_test "next" "other line.*" "next to other line"] != 0 } { 94 return 95 } 96 97 if { [gdb_test "next" "for loop.*" "next to for loop"] != 0 } { 98 return 99 } 100 101 if { [gdb_test "next" "break here.*" "next to break here"] != 0} { 102 return 103 } 104 } 105 } 106} 107 108foreach_with_prefix fork_func {fork vfork} { 109 foreach_with_prefix target-non-stop {auto on off} { 110 foreach_with_prefix non-stop {off on} { 111 foreach_with_prefix displaced-stepping {auto on off} { 112 do_test ${fork_func} ${target-non-stop} ${non-stop} ${displaced-stepping} 113 } 114 } 115 } 116} 117