]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/testsuite/gdb.multi/tids.exp
Automatic Copyright Year update after running gdb/copyright.py
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.multi / tids.exp
CommitLineData
4a94e368 1# Copyright 2015-2022 Free Software Foundation, Inc.
5d5658a1
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 thread ID parsing and display.
17
18load_lib gdb-python.exp
19
20standard_testfile
21
22# Multiple inferiors are needed, therefore both native and extended
23# gdbserver modes are supported. Only non-extended gdbserver is not
24# supported.
079670b9 25if [use_gdb_stub] {
5b362f04 26 untested "using gdb stub"
5d5658a1
PA
27 return
28}
29
30if { [prepare_for_testing "failed to prepare" ${testfile} ${srcfile} {pthreads debug}] } {
31 return -1
32}
33
5d5658a1
PA
34if { ![runto_main] } then {
35 return -1
36}
37
38# Issue "thread apply TID_LIST p 1234" and expect EXP_TID_LIST (a list
39# of thread ids) to be displayed.
40proc thread_apply {tid_list exp_tid_list {message ""}} {
41 global decimal
42 set any "\[^\r\n\]*"
43 set expected [string_to_regexp $exp_tid_list]
44
45 set r ""
46 foreach tid $expected {
47 append r "\[\r\n\]+"
48 append r "Thread $tid $any:\r\n"
49 append r "\\$$decimal = 1234"
50 }
51
52 set cmd "thread apply $tid_list"
d1e36019
TV
53 if {$message != ""} {
54 gdb_test "$cmd p 1234" $r $message
55 return
5d5658a1 56 }
d1e36019
TV
57
58 gdb_test "$cmd p 1234" $r
5d5658a1
PA
59}
60
61# Issue "info threads TID_LIST" and expect EXP_TID_LIST (a list of
62# thread ids) to be displayed.
63proc info_threads {tid_list exp_tid_list {message ""}} {
64 set any "\[^\r\n\]*"
65 set expected [string_to_regexp $exp_tid_list]
66 set r [join $expected " ${any}\r\n${any} "]
67 set r "${any} $r ${any}"
68 set cmd "info threads $tid_list"
d1e36019
TV
69 if {$message != ""} {
70 gdb_test $cmd $r $message
71 return
5d5658a1 72 }
d1e36019 73 gdb_test $cmd $r
5d5658a1
PA
74}
75
76# Issue "info threads TID_LIST" and expect INFO_THR output. Then
77# issue "thread apply TID_LIST" and expect THR_APPLY output. If
78# THR_APPLY is omitted, INFO_THR is expected instead.
79proc thr_apply_info_thr {tid_list info_thr {thr_apply ""}} {
80 if {$thr_apply == ""} {
81 set thr_apply $info_thr
82 }
83
84 info_threads $tid_list $info_thr
85 thread_apply $tid_list $thr_apply
86}
87
024a5840
TV
88# Issue both "thread apply TID_LIST" and "info threads TID_LIST" and
89# expect commands to error out with EXP_ERROR_APPLY and EXP_ERROR_INFO.
90# If EXP_ERROR_INFO is missing, default to EXP_ERROR_APPLY.
91proc thr_apply_info_thr_error {tid_list exp_error_apply {exp_error_info ""}} {
92 if { "$exp_error_info" == "" } {
93 set exp_error_info "$exp_error_apply"
94 }
95
5d5658a1 96 gdb_test "info threads $tid_list" \
024a5840 97 $exp_error_info
5d5658a1 98
3f5b7598 99 gdb_test "thread apply $tid_list" \
d1e36019 100 $exp_error_apply
5d5658a1
PA
101}
102
103# Issue both "info threads TID_LIST" and "thread apply TID_LIST" and
104# expect the command to error out with "Invalid thread ID: $EXPECTED".
3f5b7598
PA
105# EXPECTED is a literal string, not a regexp. If EXPECTED is omitted,
106# TID_LIST is expected instead.
107proc thr_apply_info_thr_invalid {tid_list {expected ""}} {
108 if {$expected == ""} {
109 set expected $tid_list
110 }
5d5658a1
PA
111 set expected [string_to_regexp $expected]
112 gdb_test "info threads $tid_list" \
113 "Invalid thread ID: $expected"
114
115 gdb_test "thread apply $tid_list p 1234" \
116 "Invalid thread ID: $expected p 1234" \
117 "thread apply $tid_list"
118}
119
120# "info threads" while there's only inferior 1 should show
121# single-number thread IDs.
122with_test_prefix "single inferior" {
123 info_threads "" "1"
124
125 gdb_test "thread" "Current thread is 1 .*"
126}
127
128# "info threads" while there are multiple inferiors should show
129# qualified thread IDs.
130with_test_prefix "two inferiors" {
131 # Add another inferior.
132 gdb_test "add-inferior" "Added inferior 2.*" "add empty inferior 2"
133
134 # Now that we've added another inferior, thread IDs now show the
135 # inferior number.
136 info_threads "" "1.1"
137
138 gdb_test "thread" "Current thread is 1\.1 .*"
139
140 gdb_test "inferior 2" "Switching to inferior 2 .*" "switch to inferior 2"
141 gdb_test "file ${binfile}" ".*" "load file in inferior 2"
142
143 runto_main
144
145 # Now that we've added another inferior, thread IDs now show the
146 # inferior number.
147 info_threads "" "1.1 2.1" \
148 "info threads show inferior numbers"
149
150 gdb_test "thread" "Current thread is 2\.1 .*" \
151 "switch to thread using extended thread ID"
152
153 gdb_breakpoint "thread_function1"
154
155 gdb_continue_to_breakpoint "once"
156 gdb_test "inferior 1" "Switching to inferior 1 .*"
157 gdb_continue_to_breakpoint "twice"
158
159 info_threads "" "1.1 1.2 2.1 2.2" \
160 "info threads again"
161
c84f6bbf
PA
162 # Same, but show the global ID.
163 gdb_test "info threads -gid" \
164 [multi_line \
165 " 1\.1 +1 +.*" \
166 "\\* 1\.2 +4 +.* thread_function1 .* at .*$srcfile:.*" \
167 " 2\.1 +2 +.*" \
168 " 2\.2 +3 +.* thread_function1 .* at .*$srcfile:.*"]
169
663f6d42 170 # Confirm the convenience variables show the expected numbers.
5d5658a1 171 gdb_test "p \$_thread == 2" " = 1"
663f6d42 172 gdb_test "p \$_gthread == 4" " = 1"
5d5658a1
PA
173
174 # Without an explicit inferior component, GDB defaults to the
175 # current inferior. Make sure we don't refer to a thread by
176 # global ID by mistake.
177 gdb_test "thread 4" "Unknown thread 1.4\\."
178
179 # Test thread ID list parsing. Test qualified and unqualified
180 # IDs; qualified and unqualified ranges; invalid IDs and invalid
181 # ranges.
182
183 # First spawn a couple more threads so ranges includes more than
184 # two threads.
185 with_test_prefix "more threads" {
186 gdb_breakpoint "thread_function2"
187
188 gdb_test "inferior 2" "Switching to inferior 2 .*"
189 gdb_continue_to_breakpoint "once"
190
191 gdb_test "inferior 1" "Switching to inferior 1 .*"
192 gdb_continue_to_breakpoint "twice"
193 }
194
3f5b7598
PA
195 thr_apply_info_thr "1" \
196 "1.1"
197
198 thr_apply_info_thr "1.1" \
199 "1.1"
200
5d5658a1
PA
201 thr_apply_info_thr "1 2 3" \
202 "1.1 1.2 1.3"
203
204 # Same, but with qualified thread IDs.
205 thr_apply_info_thr "1.1 1.2 1.3 2.1 2.2" \
206 "1.1 1.2 1.3 2.1 2.2"
207
208 # Test a thread number range.
209 thr_apply_info_thr "1-3" \
210 "1.1 1.2 1.3"
211
212 # Same, but using a qualified range.
213 thr_apply_info_thr "1.1-3" \
214 "1.1 1.2 1.3"
215
216 # A mix of qualified and unqualified thread IDs/ranges.
217 thr_apply_info_thr "1.1 2-3" \
218 "1.1 1.2 1.3"
219
220 thr_apply_info_thr "1 1.2-3" \
221 "1.1 1.2 1.3"
222
223 # Likewise, but mix inferiors too.
224 thr_apply_info_thr "2.1 2-3" \
225 "1.2 1.3 2.1" \
226 "2.1 1.2 1.3"
227
228 # Multiple ranges with mixed explicit inferiors.
229 thr_apply_info_thr "1.1-2 2.2-3" \
230 "1.1 1.2 2.2 2.3"
231
5af962df
AA
232 # All threads.
233 thread_apply "all" \
234 "2.3 2.2 2.1 1.3 1.2 1.1"
235 thread_apply "all -ascending" \
236 "1.1 1.2 1.3 2.1 2.2 2.3"
237
3f5b7598
PA
238 # Now test using GDB convenience variables.
239
240 gdb_test "p \$inf = 1" " = 1"
241 gdb_test "p \$thr_start = 2" " = 2"
242 gdb_test "p \$thr_end = 3" " = 3"
243
244 # Convenience variable for the inferior number, only.
245 thr_apply_info_thr "\$inf.2" \
246 "1.2"
247 thr_apply_info_thr "\$inf.2-3" \
248 "1.2 1.3"
249
250 # Convenience variables for thread numbers as well.
251 foreach prefix {"" "1." "\$inf."} {
252 thr_apply_info_thr "${prefix}\$thr_start" \
253 "1.2"
254 thr_apply_info_thr "${prefix}\$thr_start-\$thr_end" \
255 "1.2 1.3"
256 thr_apply_info_thr "${prefix}2-\$thr_end" \
257 "1.2 1.3"
258 thr_apply_info_thr "${prefix}\$thr_start-3" \
259 "1.2 1.3"
260
261 # Undefined convenience variable.
262 set prefix_re [string_to_regexp $prefix]
263 thr_apply_info_thr_error "${prefix}\$conv123" \
264 [multi_line \
265 "Convenience variable must have integer value\." \
266 "Invalid thread ID: ${prefix_re}\\\$conv123"]
267 }
268
269 # Convenience variables pointing at an inexisting thread and/or
270 # inferior.
271 gdb_test "p \$inf = 30" " = 30"
272 gdb_test "p \$thr = 20" " = 20"
273 # Try both the convenience variable and the literal number.
274 foreach thr {"\$thr" "20" "1.20" "\$inf.1" "30.1" } {
275 set expected [string_to_regexp $thr]
276 gdb_test "info threads $thr" "No threads match '${expected}'."
277 # "info threads" works like a filter. If there's any other
278 # valid thread in the list, there's no error.
279 info_threads "$thr 1.1" "1.1"
280 info_threads "1.1 $thr" "1.1"
281 }
282
283 gdb_test "thread apply \$thr p 1234" \
284 "warning: Unknown thread 1.20" \
285 "thread apply \$thr"
286
287 gdb_test "thread apply \$inf.1 p 1234" \
288 "warning: Unknown thread 30.1" \
289 "thread apply \$inf.1"
290
71ef29a8
PA
291 # Star ranges.
292
293 thr_apply_info_thr "1.*" \
294 "1.1 1.2 1.3"
295
296 thr_apply_info_thr "*" \
297 "1.1 1.2 1.3"
298
299 thr_apply_info_thr "1.* 2.1" \
300 "1.1 1.2 1.3 2.1"
301
302 thr_apply_info_thr "2.1 1.*" \
303 "1.1 1.2 1.3 2.1" \
304 "2.1 1.1 1.2 1.3"
305
306 thr_apply_info_thr "1.* 2.*" \
307 "1.1 1.2 1.3 2.1 2.2 2.3"
308
309 thr_apply_info_thr "2.* 1.*" \
310 "1.1 1.2 1.3 2.1 2.2 2.3" \
311 "2.1 2.2 2.3 1.1 1.2 1.3"
312
313 # There's no inferior 3, but "info threads" treats the thread list
314 # as a filter, so it's OK. "thread apply" complains about the
315 # unknown inferior through.
316 info_threads "1.1 3.*" \
317 "1.1"
318 gdb_test "thread apply 1.1 3.* p 1" \
319 "Thread 1.1.*warning: Unknown inferior 3"
320
5d5658a1
PA
321 # Now test a set of invalid thread IDs/ranges.
322
323 thr_apply_info_thr_invalid "1." \
324 "1."
325
326 thr_apply_info_thr_invalid "1-3 1." \
327 "1."
328
329 thr_apply_info_thr_invalid "1.1.1" \
330 "1.1.1"
331
332 thr_apply_info_thr_invalid "2 1.1.1" \
333 "1.1.1"
334
335 thr_apply_info_thr_invalid "1.1.1 2" \
336 "1.1.1 2"
337
338 thr_apply_info_thr_invalid "1-2.1" \
339 "1-2.1"
340
3f5b7598
PA
341 gdb_test "p \$zero = 0" " = 0"
342 gdb_test "p \$one = 1" " = 1"
343 gdb_test "p \$minus_one = -11" " = -11"
344 foreach prefix {"" "1." "$one."} {
345 set prefix_re [string_to_regexp $prefix]
346
347 thr_apply_info_thr_invalid "${prefix}foo"
348 thr_apply_info_thr_invalid "${prefix}1foo"
349 thr_apply_info_thr_invalid "${prefix}foo1"
350
351 thr_apply_info_thr_error "${prefix}1-0" "inverted range"
352 thr_apply_info_thr_error "${prefix}1-\$zero" "inverted range"
353 thr_apply_info_thr_error "${prefix}\$one-0" "inverted range"
354 thr_apply_info_thr_error "${prefix}\$one-\$zero" "inverted range"
355 thr_apply_info_thr_error "${prefix}1-" "inverted range"
356 thr_apply_info_thr_error "${prefix}2-1" "inverted range"
357 thr_apply_info_thr_error "${prefix}2-\$one" "inverted range"
b9a3f842 358 if {$prefix == ""} {
024a5840
TV
359 thr_apply_info_thr_error "${prefix}-1" "Invalid thread ID: -1" \
360 "Unrecognized option at: -1"
361 thr_apply_info_thr_error "${prefix}-\$one" \
362 "Invalid thread ID: -\\\$one" "Unrecognized option at: -\\\$one"
b9a3f842
PA
363 } else {
364 thr_apply_info_thr_error "${prefix}-1" "negative value"
365 thr_apply_info_thr_error "${prefix}-\$one" "negative value"
366 }
3f5b7598
PA
367 thr_apply_info_thr_error "${prefix}\$minus_one" \
368 "negative value: ${prefix_re}\\\$minus_one"
71ef29a8
PA
369
370 thr_apply_info_thr_error "${prefix}1-*" "inverted range"
371 thr_apply_info_thr_invalid "${prefix}*1"
372 thr_apply_info_thr_invalid "${prefix}*foo"
373 thr_apply_info_thr_invalid "${prefix}foo*"
3f5b7598 374 }
5d5658a1 375
3f5b7598
PA
376 # Check that a valid thread ID list with a missing command errors
377 # out.
378 with_test_prefix "missing command" {
379 set output "Please specify a command following the thread ID list"
380 gdb_test "thread apply 1" $output
381 gdb_test "thread apply 1.1" $output
382 gdb_test "thread apply 1.1 1.2" $output
383 gdb_test "thread apply 1-2" $output
384 gdb_test "thread apply 1.1-2" $output
385 gdb_test "thread apply $thr" $output
71ef29a8 386 gdb_test "thread apply 1.*" $output
3f5b7598 387 }
5d5658a1 388
b9a3f842
PA
389 # Check that thread ID list parsing stops at the non-number token
390 # "foo" in a corner case where the "foo" is followed by hyphens.
391 # In this corner case, GDB used to skip past "foo", and then parse
392 # "--1" as a tid range for the current inferior.
393 gdb_test "thread apply 1 foo --1" \
394 "Undefined command: \"foo\". Try \"help\"\\."
395
5d5658a1
PA
396 # Check that we do parse the inferior number and don't confuse it.
397 gdb_test "info threads 3.1" \
398 "No threads match '3.1'\."
399}
400
401if { ![skip_python_tests] } {
402 with_test_prefix "python" {
22a02324
PA
403 # Check that InferiorThread.num and InferiorThread.global_num
404 # return the expected numbers.
5d5658a1
PA
405 gdb_py_test_silent_cmd "python t0 = gdb.selected_thread ()" \
406 "test gdb.selected_thread" 1
407 gdb_test "python print ('result = %s' % t0.num)" " = 3" \
408 "test InferiorThread.num"
22a02324
PA
409 gdb_test "python print ('result = %s' % t0.global_num)" " = 6" \
410 "test InferiorThread.global_num"
411
412 # Breakpoint.thread expects global IDs. Confirm that that
413 # works as expected.
414 delete_breakpoints
415 gdb_breakpoint "thread_function1"
416
417 gdb_py_test_silent_cmd "python bp = gdb.breakpoints()\[0\]" \
418 "get python breakpoint" 0
419 gdb_test "python bp.thread = 6" "thread = 6" \
420 "make breakpoint thread-specific with python"
421 # Check that the inferior-qualified ID is correct.
422 gdb_test "info breakpoint" \
423 "stop only in thread 1.3\r\n.*" \
424 "thread specific breakpoint right thread"
5d5658a1
PA
425 }
426}
427
428# Remove the second inferior and confirm that GDB goes back to showing
429# single-number thread IDs.
430with_test_prefix "back to one inferior" {
431 gdb_test "kill inferior 2" "" "kill inferior 2" "Kill the program being debugged.*" "y"
432 gdb_test "thread 1.1" "Switching to thread 1\.1 .*"
433 gdb_test "remove-inferior 2" ".*" "remove inferior 2"
434
435 # "info threads" while there's only inferior 1 should show
436 # single-number thread IDs.
437 info_threads "" "1 2 3"
438
439 gdb_test "thread" "Current thread is 1 .*"
440}
441
442# Add another inferior and remove inferior 1. Since even though
443# there's a single inferior, its number is not 1, GDB should show
444# inferior-qualified thread IDs.
445with_test_prefix "single-inferior but not initial" {
446 # Add another inferior.
447 gdb_test "add-inferior" "Added inferior 3.*" "add empty inferior"
448
449 # Now that we'd added another inferior, thread IDs should show the
450 # inferior number.
451 info_threads "" "1.1 1.2 1.3" \
452 "info threads with multiple inferiors"
453
454 gdb_test "thread" "Current thread is 1\.1 .*"
455
456 gdb_test "inferior 3" "Switching to inferior 3 .*" "switch to inferior 3"
457 gdb_test "file ${binfile}" ".*" "load file in inferior 3"
458
459 runto_main
460
461 gdb_test "remove-inferior 1" ".*" "remove inferior 1"
462
463 # Even though we have a single inferior, its number is > 1, so
464 # thread IDs should include the inferior number.
465 info_threads "" "3.1" \
466 "info threads with single inferior"
467
468 gdb_test "thread" "Current thread is 3\.1 .*" "thread again"
469}