]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/testsuite/gdb.threads/non-stop-fair-events.exp
Update copyright year range in header of all files managed by GDB
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.threads / non-stop-fair-events.exp
CommitLineData
1d506c26 1# Copyright (C) 2014-2024 Free Software Foundation, Inc.
ede9f622
PA
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 that GDB in non-stop mode gives roughly equal priority to
17# events of all threads.
18
19standard_testfile
20set executable ${testfile}
21
450d26c8 22require {!target_info exists gdb,nosignals}
ede9f622 23
337532fa
YQ
24set options { "additional_flags=-DTIMEOUT=$timeout" debug pthreads }
25if {[prepare_for_testing "failed to prepare" $testfile $srcfile $options] == -1} {
ede9f622
PA
26 return -1
27}
28
29gdb_test_no_output "set non-stop on"
30
31if ![runto_main] {
32 return -1
33}
34
35# We want "handle print", to make sure the target backend reports the
36# signal to the run control core.
37gdb_test "handle SIGUSR1 print nostop pass" ""
38
39# Get current value of VAR from the inferior. TEST is used as test
40# message.
41
42proc get_value {var test} {
43 global expect_out
44 global gdb_prompt
45 global decimal
46
47 set value -1
48 gdb_test_multiple "print $var" "$test" {
49 -re ".*= ($decimal).*\r\n$gdb_prompt $" {
50 set value $expect_out(1,string)
51 pass "$test"
52 }
53 }
54 return ${value}
55}
56
57set NUM_THREADS [get_value "num_threads" "get num_threads"]
58
59# Account for the main thread.
60incr NUM_THREADS
61
1ed415e2
PA
62# Probe for displaced stepping support. We're stopped at the main
63# breakpoint. If displaced stepping is supported, we should see
64# related debug output.
65set displaced_stepping_enabled 0
66set msg "check displaced-stepping"
67gdb_test_no_output "set debug displaced 1"
68gdb_test_multiple "next" $msg {
187b041e 69 -re "prepared successfully .*$gdb_prompt $" {
1ed415e2
PA
70 set displaced_stepping_enabled 1
71 }
72 -re ".*$gdb_prompt $" {
73 }
74}
75gdb_test_no_output "set debug displaced 0"
76
ede9f622
PA
77# Run threads to their start positions. This prepares for a new test
78# sequence.
79
80proc restart {} {
81 global gdb_prompt
82 global NUM_THREADS
83
84 delete_breakpoints
85
86 gdb_test "print got_sig = 0" " = 0"
87
88 gdb_breakpoint [gdb_get_line_number "set thread breakpoint here"]
89 gdb_breakpoint [gdb_get_line_number "set kill breakpoint here"]
90
91 set test "continue -a&"
92 gdb_test_multiple $test $test {
93 -re "Continuing.\r\n$gdb_prompt " {
94 pass $test
95 }
96 }
97
98 for {set i 1} { $i <= $NUM_THREADS } { incr i } {
99 set test "thread $i restarted"
100 gdb_test_multiple "" $test {
101 -re "breakpoint here" {
102 # The prompt was already matched in the "continue &"
103 # test above. We're now consuming asynchronous output
104 # that comes after the prompt.
105 pass $test
106 }
107 }
108 }
109
110 delete_breakpoints
111}
112
d136eff5
PA
113# Run command and wait for the prompt, without end anchor.
114
115proc gdb_test_no_anchor {cmd} {
116 global gdb_prompt
117
118 gdb_test_multiple $cmd $cmd {
119 -re "$gdb_prompt " {
120 pass $cmd
121 }
122 }
123}
124
125# Enable/disable debugging.
126
127proc enable_debug {enable} {
128
129 # Comment out to debug problems with the test.
130 return
131
132 gdb_test_no_anchor "set debug infrun $enable"
133 gdb_test_no_anchor "set debug displaced $enable"
134}
135
ede9f622
PA
136# The test proper. SIGNAL_THREAD is the thread that has been elected
137# to receive the SIGUSR1 signal.
138
139proc test {signal_thread} {
140 global gdb_prompt
141 global NUM_THREADS
1ed415e2
PA
142 global timeout
143 global displaced_stepping_enabled
ede9f622
PA
144
145 with_test_prefix "signal_thread=$signal_thread" {
146 restart
147
148 # Set all threads stepping the infinite loop line in parallel.
149 for {set i 2} { $i <= $NUM_THREADS } { incr i } {
150 gdb_test "thread $i" \
151 "child_function.*set thread breakpoint here.*" \
152 "switch to thread $i to step it"
153
154 if {$i == $signal_thread} {
155 gdb_test "print signal_thread = self" " = .*"
156 }
157
158 gdb_test "step&" "" "set $i thread stepping"
159 }
160
161 gdb_test "thread 1" "Switching to .*" \
162 "switch to the main thread to queue signal"
163
164 # Let the main thread queue the signal.
165 gdb_breakpoint "loop_broke"
d136eff5
PA
166
167 enable_debug 1
168
1ed415e2
PA
169 # On software single-step targets that don't support displaced
170 # stepping, threads keep hitting each others' single-step
171 # breakpoints, and then GDB needs to pause all threads to step
172 # past those. The end result is that progress in the main
173 # thread will be slower and it may take a bit longer for the
174 # signal to be queued; bump the timeout.
175 if {!$displaced_stepping_enabled && ![can_hardware_single_step]} {
176 # The more threads we have, the longer it takes.
177 set factor $NUM_THREADS
178 } else {
179 set factor 1
ede9f622 180 }
1ed415e2
PA
181 with_timeout_factor $factor {
182 gdb_test "print timeout = $timeout" " = $timeout" \
183 "set timeout in the inferior"
184
185 set saw_continuing 0
186 set test "continue &"
187 gdb_test_multiple $test $test {
188 -re "Continuing.\r\n" {
189 set saw_continuing 1
190 exp_continue
191 }
192 -re "$gdb_prompt " {
193 gdb_assert $saw_continuing $test
ede9f622 194 }
d136eff5
PA
195 -re "infrun:" {
196 exp_continue
197 }
198 }
1ed415e2
PA
199
200 set gotit 0
201
202 # Wait for all threads to finish their steps, and for the main
203 # thread to hit the breakpoint.
204 for {set i 1} { $i <= $NUM_THREADS } { incr i } {
205 set test "thread $i broke out of loop"
206 set gotit 0
207 gdb_test_multiple "" $test {
208 -re "loop_broke" {
209 # The prompt was already matched in the "continue
210 # &" test above. We're now consuming asynchronous
211 # output that comes after the prompt.
212 set gotit 1
213 pass $test
214 }
215 -re "infrun:" {
216 exp_continue
217 }
218 }
219 if {!$gotit} {
220 break
221 }
ede9f622
PA
222 }
223 }
224
d136eff5
PA
225 enable_debug 0
226
ede9f622
PA
227 # It's helpful to have this in the log if the test ever
228 # happens to fail.
229 gdb_test "info threads"
d136eff5
PA
230
231 return $gotit
ede9f622
PA
232 }
233}
234
235# The kernel/debug API may always walk its thread list looking for the
236# first with an event, resulting in giving priority to e.g. the thread
237# with lowest kernel thread ID. So test once with the signal pending
238# in each thread, except the main thread.
239for {set i 2} { $i <= $NUM_THREADS } { incr i } {
d136eff5
PA
240 if {![test $i]} {
241 # Avoid cascading timeouts, and bail out.
242 return
243 }
ede9f622 244}