1 # Copyright 2015-2022 Free Software Foundation, Inc.
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.
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.
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/>.
16 # Test thread ID parsing and display.
18 load_lib gdb-python.exp
22 # Multiple inferiors are needed, therefore both native and extended
23 # gdbserver modes are supported. Only non-extended gdbserver is not
26 untested "using gdb stub"
30 if { [prepare_for_testing "failed to prepare" ${testfile} ${srcfile} {pthreads debug}] } {
38 # Issue "thread apply TID_LIST p 1234" and expect EXP_TID_LIST (a list
39 # of thread ids) to be displayed.
40 proc thread_apply {tid_list exp_tid_list {message ""}} {
43 set expected [string_to_regexp $exp_tid_list]
46 foreach tid $expected {
48 append r "Thread $tid $any:\r\n"
49 append r "\\$$decimal = 1234"
52 set cmd "thread apply $tid_list"
54 gdb_test "$cmd p 1234" $r $message
58 gdb_test "$cmd p 1234" $r
61 # Issue "info threads TID_LIST" and expect EXP_TID_LIST (a list of
62 # thread ids) to be displayed.
63 proc info_threads {tid_list exp_tid_list {message ""}} {
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"
70 gdb_test $cmd $r $message
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.
79 proc thr_apply_info_thr {tid_list info_thr {thr_apply ""}} {
80 if {$thr_apply == ""} {
81 set thr_apply $info_thr
84 info_threads $tid_list $info_thr
85 thread_apply $tid_list $thr_apply
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.
91 proc 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"
96 gdb_test "info threads $tid_list" \
99 gdb_test "thread apply $tid_list" \
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".
105 # EXPECTED is a literal string, not a regexp. If EXPECTED is omitted,
106 # TID_LIST is expected instead.
107 proc thr_apply_info_thr_invalid {tid_list {expected ""}} {
108 if {$expected == ""} {
109 set expected $tid_list
111 set expected [string_to_regexp $expected]
112 gdb_test "info threads $tid_list" \
113 "Invalid thread ID: $expected"
115 gdb_test "thread apply $tid_list p 1234" \
116 "Invalid thread ID: $expected p 1234" \
117 "thread apply $tid_list"
120 # "info threads" while there's only inferior 1 should show
121 # single-number thread IDs.
122 with_test_prefix "single inferior" {
125 gdb_test "thread" "Current thread is 1 .*"
127 gdb_test "print \$_inferior_thread_count" " = 1"
130 # "info threads" while there are multiple inferiors should show
131 # qualified thread IDs.
132 with_test_prefix "two inferiors" {
133 # Add another inferior.
134 gdb_test "add-inferior" "Added inferior 2.*" "add empty inferior 2"
136 # Now that we've added another inferior, thread IDs now show the
138 info_threads "" "1.1"
140 gdb_test "thread" "Current thread is 1\.1 .*"
142 gdb_test "inferior 2" "Switching to inferior 2 .*" "switch to inferior 2"
143 gdb_test "file ${binfile}" ".*" "load file in inferior 2"
145 gdb_test "print \$_inferior_thread_count" " = 0" \
146 "no threads before we start the second inferior"
150 gdb_test "print \$_inferior_thread_count" " = 1" \
151 "no other threads started yet"
153 # Now that we've added another inferior, thread IDs now show the
155 info_threads "" "1.1 2.1" \
156 "info threads show inferior numbers"
158 gdb_test "thread" "Current thread is 2\.1 .*" \
159 "switch to thread using extended thread ID"
161 gdb_breakpoint "thread_function1"
163 gdb_continue_to_breakpoint "once"
164 gdb_test "print \$_inferior_thread_count" " = 2" \
165 "second thread started in inferior 2"
166 gdb_test "inferior 1" "Switching to inferior 1 .*"
167 gdb_test "print \$_inferior_thread_count" " = 1" \
168 "still only one thread in inferior 1"
169 gdb_continue_to_breakpoint "twice"
170 gdb_test "print \$_inferior_thread_count" " = 2" \
171 "second thread started in inferior 1"
173 info_threads "" "1.1 1.2 2.1 2.2" \
176 # Same, but show the global ID.
177 gdb_test "info threads -gid" \
180 "\\* 1\.2 +4 +.* thread_function1 .* at .*$srcfile:.*" \
182 " 2\.2 +3 +.* thread_function1 .* at .*$srcfile:.*"]
184 # Confirm the convenience variables show the expected numbers.
185 gdb_test "p \$_thread == 2" " = 1"
186 gdb_test "p \$_gthread == 4" " = 1"
188 # Without an explicit inferior component, GDB defaults to the
189 # current inferior. Make sure we don't refer to a thread by
190 # global ID by mistake.
191 gdb_test "thread 4" "Unknown thread 1.4\\."
193 # Test thread ID list parsing. Test qualified and unqualified
194 # IDs; qualified and unqualified ranges; invalid IDs and invalid
197 # First spawn a couple more threads so ranges includes more than
199 with_test_prefix "more threads" {
200 gdb_breakpoint "thread_function2"
202 gdb_test "inferior 2" "Switching to inferior 2 .*"
203 gdb_continue_to_breakpoint "once"
204 gdb_test "print \$_inferior_thread_count" " = 3" \
205 "third thread started in inferior 2"
207 gdb_test "inferior 1" "Switching to inferior 1 .*"
208 gdb_test "print \$_inferior_thread_count" " = 2" \
209 "still only two threads in inferior 1"
210 gdb_continue_to_breakpoint "twice"
211 gdb_test "print \$_inferior_thread_count" " = 3" \
212 "third thread started in inferior 1"
215 thr_apply_info_thr "1" \
218 thr_apply_info_thr "1.1" \
221 thr_apply_info_thr "1 2 3" \
224 # Same, but with qualified thread IDs.
225 thr_apply_info_thr "1.1 1.2 1.3 2.1 2.2" \
226 "1.1 1.2 1.3 2.1 2.2"
228 # Test a thread number range.
229 thr_apply_info_thr "1-3" \
232 # Same, but using a qualified range.
233 thr_apply_info_thr "1.1-3" \
236 # A mix of qualified and unqualified thread IDs/ranges.
237 thr_apply_info_thr "1.1 2-3" \
240 thr_apply_info_thr "1 1.2-3" \
243 # Likewise, but mix inferiors too.
244 thr_apply_info_thr "2.1 2-3" \
248 # Multiple ranges with mixed explicit inferiors.
249 thr_apply_info_thr "1.1-2 2.2-3" \
254 "2.3 2.2 2.1 1.3 1.2 1.1"
255 thread_apply "all -ascending" \
256 "1.1 1.2 1.3 2.1 2.2 2.3"
258 # Now test using GDB convenience variables.
260 gdb_test "p \$inf = 1" " = 1"
261 gdb_test "p \$thr_start = 2" " = 2"
262 gdb_test "p \$thr_end = 3" " = 3"
264 # Convenience variable for the inferior number, only.
265 thr_apply_info_thr "\$inf.2" \
267 thr_apply_info_thr "\$inf.2-3" \
270 # Convenience variables for thread numbers as well.
271 foreach prefix {"" "1." "\$inf."} {
272 thr_apply_info_thr "${prefix}\$thr_start" \
274 thr_apply_info_thr "${prefix}\$thr_start-\$thr_end" \
276 thr_apply_info_thr "${prefix}2-\$thr_end" \
278 thr_apply_info_thr "${prefix}\$thr_start-3" \
281 # Undefined convenience variable.
282 set prefix_re [string_to_regexp $prefix]
283 thr_apply_info_thr_error "${prefix}\$conv123" \
285 "Convenience variable must have integer value\." \
286 "Invalid thread ID: ${prefix_re}\\\$conv123"]
289 # Convenience variables pointing at an inexisting thread and/or
291 gdb_test "p \$inf = 30" " = 30"
292 gdb_test "p \$thr = 20" " = 20"
293 # Try both the convenience variable and the literal number.
294 foreach thr {"\$thr" "20" "1.20" "\$inf.1" "30.1" } {
295 set expected [string_to_regexp $thr]
296 gdb_test "info threads $thr" "No threads match '${expected}'."
297 # "info threads" works like a filter. If there's any other
298 # valid thread in the list, there's no error.
299 info_threads "$thr 1.1" "1.1"
300 info_threads "1.1 $thr" "1.1"
303 gdb_test "thread apply \$thr p 1234" \
304 "warning: Unknown thread 1.20" \
307 gdb_test "thread apply \$inf.1 p 1234" \
308 "warning: Unknown thread 30.1" \
309 "thread apply \$inf.1"
313 thr_apply_info_thr "1.*" \
316 thr_apply_info_thr "*" \
319 thr_apply_info_thr "1.* 2.1" \
322 thr_apply_info_thr "2.1 1.*" \
326 thr_apply_info_thr "1.* 2.*" \
327 "1.1 1.2 1.3 2.1 2.2 2.3"
329 thr_apply_info_thr "2.* 1.*" \
330 "1.1 1.2 1.3 2.1 2.2 2.3" \
331 "2.1 2.2 2.3 1.1 1.2 1.3"
333 # There's no inferior 3, but "info threads" treats the thread list
334 # as a filter, so it's OK. "thread apply" complains about the
335 # unknown inferior through.
336 info_threads "1.1 3.*" \
338 gdb_test "thread apply 1.1 3.* p 1" \
339 "Thread 1.1.*warning: Unknown inferior 3"
341 # Now test a set of invalid thread IDs/ranges.
343 thr_apply_info_thr_invalid "1." \
346 thr_apply_info_thr_invalid "1-3 1." \
349 thr_apply_info_thr_invalid "1.1.1" \
352 thr_apply_info_thr_invalid "2 1.1.1" \
355 thr_apply_info_thr_invalid "1.1.1 2" \
358 thr_apply_info_thr_invalid "1-2.1" \
361 gdb_test "p \$zero = 0" " = 0"
362 gdb_test "p \$one = 1" " = 1"
363 gdb_test "p \$minus_one = -11" " = -11"
364 foreach prefix {"" "1." "$one."} {
365 set prefix_re [string_to_regexp $prefix]
367 thr_apply_info_thr_invalid "${prefix}foo"
368 thr_apply_info_thr_invalid "${prefix}1foo"
369 thr_apply_info_thr_invalid "${prefix}foo1"
371 thr_apply_info_thr_error "${prefix}1-0" "inverted range"
372 thr_apply_info_thr_error "${prefix}1-\$zero" "inverted range"
373 thr_apply_info_thr_error "${prefix}\$one-0" "inverted range"
374 thr_apply_info_thr_error "${prefix}\$one-\$zero" "inverted range"
375 thr_apply_info_thr_error "${prefix}1-" "inverted range"
376 thr_apply_info_thr_error "${prefix}2-1" "inverted range"
377 thr_apply_info_thr_error "${prefix}2-\$one" "inverted range"
379 thr_apply_info_thr_error "${prefix}-1" "Invalid thread ID: -1" \
380 "Unrecognized option at: -1"
381 thr_apply_info_thr_error "${prefix}-\$one" \
382 "Invalid thread ID: -\\\$one" "Unrecognized option at: -\\\$one"
384 thr_apply_info_thr_error "${prefix}-1" "negative value"
385 thr_apply_info_thr_error "${prefix}-\$one" "negative value"
387 thr_apply_info_thr_error "${prefix}\$minus_one" \
388 "negative value: ${prefix_re}\\\$minus_one"
390 thr_apply_info_thr_error "${prefix}1-*" "inverted range"
391 thr_apply_info_thr_invalid "${prefix}*1"
392 thr_apply_info_thr_invalid "${prefix}*foo"
393 thr_apply_info_thr_invalid "${prefix}foo*"
396 # Check that a valid thread ID list with a missing command errors
398 with_test_prefix "missing command" {
399 set output "Please specify a command following the thread ID list"
400 gdb_test "thread apply 1" $output
401 gdb_test "thread apply 1.1" $output
402 gdb_test "thread apply 1.1 1.2" $output
403 gdb_test "thread apply 1-2" $output
404 gdb_test "thread apply 1.1-2" $output
405 gdb_test "thread apply $thr" $output
406 gdb_test "thread apply 1.*" $output
409 # Check that thread ID list parsing stops at the non-number token
410 # "foo" in a corner case where the "foo" is followed by hyphens.
411 # In this corner case, GDB used to skip past "foo", and then parse
412 # "--1" as a tid range for the current inferior.
413 gdb_test "thread apply 1 foo --1" \
414 "Undefined command: \"foo\". Try \"help\"\\."
416 # Check that we do parse the inferior number and don't confuse it.
417 gdb_test "info threads 3.1" \
418 "No threads match '3.1'\."
421 if { ![skip_python_tests] } {
422 with_test_prefix "python" {
423 # Check that InferiorThread.num and InferiorThread.global_num
424 # return the expected numbers.
425 gdb_py_test_silent_cmd "python t0 = gdb.selected_thread ()" \
426 "test gdb.selected_thread" 1
427 gdb_test "python print ('result = %s' % t0.num)" " = 3" \
428 "test InferiorThread.num"
429 gdb_test "python print ('result = %s' % t0.global_num)" " = 6" \
430 "test InferiorThread.global_num"
432 # Breakpoint.thread expects global IDs. Confirm that that
435 gdb_breakpoint "thread_function1"
437 gdb_py_test_silent_cmd "python bp = gdb.breakpoints()\[0\]" \
438 "get python breakpoint" 0
439 gdb_test "python bp.thread = 6" "thread = 6" \
440 "make breakpoint thread-specific with python"
441 # Check that the inferior-qualified ID is correct.
442 gdb_test "info breakpoint" \
443 "stop only in thread 1.3\r\n.*" \
444 "thread specific breakpoint right thread"
448 # Remove the second inferior and confirm that GDB goes back to showing
449 # single-number thread IDs.
450 with_test_prefix "back to one inferior" {
451 gdb_test "kill inferior 2" "" "kill inferior 2"
452 gdb_test "thread 1.1" "Switching to thread 1\.1 .*"
453 gdb_test "remove-inferior 2" ".*" "remove inferior 2"
455 # "info threads" while there's only inferior 1 should show
456 # single-number thread IDs.
457 info_threads "" "1 2 3"
459 gdb_test "thread" "Current thread is 1 .*"
462 # Add another inferior and remove inferior 1. Since even though
463 # there's a single inferior, its number is not 1, GDB should show
464 # inferior-qualified thread IDs.
465 with_test_prefix "single-inferior but not initial" {
466 # Add another inferior.
467 gdb_test "add-inferior" "Added inferior 3.*" "add empty inferior"
469 # Now that we'd added another inferior, thread IDs should show the
471 info_threads "" "1.1 1.2 1.3" \
472 "info threads with multiple inferiors"
474 gdb_test "thread" "Current thread is 1\.1 .*"
476 gdb_test "inferior 3" "Switching to inferior 3 .*" "switch to inferior 3"
477 gdb_test "file ${binfile}" ".*" "load file in inferior 3"
481 gdb_test "remove-inferior 1" ".*" "remove inferior 1"
483 # Even though we have a single inferior, its number is > 1, so
484 # thread IDs should include the inferior number.
485 info_threads "" "3.1" \
486 "info threads with single inferior"
488 gdb_test "thread" "Current thread is 3\.1 .*" "thread again"