1 # Copyright 2015-2019 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}] } {
34 clean_restart ${testfile}
36 if { ![runto_main] } then {
40 # Issue "thread apply TID_LIST p 1234" and expect EXP_TID_LIST (a list
41 # of thread ids) to be displayed.
42 proc thread_apply {tid_list exp_tid_list {message ""}} {
45 set expected [string_to_regexp $exp_tid_list]
48 foreach tid $expected {
50 append r "Thread $tid $any:\r\n"
51 append r "\\$$decimal = 1234"
54 set cmd "thread apply $tid_list"
58 gdb_test "$cmd p 1234" $r $message
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"
72 gdb_test $cmd $r $message
75 # Issue "info threads TID_LIST" and expect INFO_THR output. Then
76 # issue "thread apply TID_LIST" and expect THR_APPLY output. If
77 # THR_APPLY is omitted, INFO_THR is expected instead.
78 proc thr_apply_info_thr {tid_list info_thr {thr_apply ""}} {
79 if {$thr_apply == ""} {
80 set thr_apply $info_thr
83 info_threads $tid_list $info_thr
84 thread_apply $tid_list $thr_apply
87 # Issue both "thread apply TID_LIST" and "info threads TID_LIST" and
88 # expect commands to error out with EXP_ERROR_APPLY and EXP_ERROR_INFO.
89 # If EXP_ERROR_INFO is missing, default to EXP_ERROR_APPLY.
90 proc thr_apply_info_thr_error {tid_list exp_error_apply {exp_error_info ""}} {
91 if { "$exp_error_info" == "" } {
92 set exp_error_info "$exp_error_apply"
95 gdb_test "info threads $tid_list" \
98 gdb_test "thread apply $tid_list" \
100 "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 .*"
128 # "info threads" while there are multiple inferiors should show
129 # qualified thread IDs.
130 with_test_prefix "two inferiors" {
131 # Add another inferior.
132 gdb_test "add-inferior" "Added inferior 2.*" "add empty inferior 2"
134 # Now that we've added another inferior, thread IDs now show the
136 info_threads "" "1.1"
138 gdb_test "thread" "Current thread is 1\.1 .*"
140 gdb_test "inferior 2" "Switching to inferior 2 .*" "switch to inferior 2"
141 gdb_test "file ${binfile}" ".*" "load file in inferior 2"
145 # Now that we've added another inferior, thread IDs now show the
147 info_threads "" "1.1 2.1" \
148 "info threads show inferior numbers"
150 gdb_test "thread" "Current thread is 2\.1 .*" \
151 "switch to thread using extended thread ID"
153 gdb_breakpoint "thread_function1"
155 gdb_continue_to_breakpoint "once"
156 gdb_test "inferior 1" "Switching to inferior 1 .*"
157 gdb_continue_to_breakpoint "twice"
159 info_threads "" "1.1 1.2 2.1 2.2" \
162 # Same, but show the global ID.
163 gdb_test "info threads -gid" \
166 "\\* 1\.2 +4 +.* thread_function1 .* at .*$srcfile:.*" \
168 " 2\.2 +3 +.* thread_function1 .* at .*$srcfile:.*"]
170 # Confirm the convenience variables show the expected numbers.
171 gdb_test "p \$_thread == 2" " = 1"
172 gdb_test "p \$_gthread == 4" " = 1"
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\\."
179 # Test thread ID list parsing. Test qualified and unqualified
180 # IDs; qualified and unqualified ranges; invalid IDs and invalid
183 # First spawn a couple more threads so ranges includes more than
185 with_test_prefix "more threads" {
186 gdb_breakpoint "thread_function2"
188 gdb_test "inferior 2" "Switching to inferior 2 .*"
189 gdb_continue_to_breakpoint "once"
191 gdb_test "inferior 1" "Switching to inferior 1 .*"
192 gdb_continue_to_breakpoint "twice"
195 thr_apply_info_thr "1" \
198 thr_apply_info_thr "1.1" \
201 thr_apply_info_thr "1 2 3" \
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"
208 # Test a thread number range.
209 thr_apply_info_thr "1-3" \
212 # Same, but using a qualified range.
213 thr_apply_info_thr "1.1-3" \
216 # A mix of qualified and unqualified thread IDs/ranges.
217 thr_apply_info_thr "1.1 2-3" \
220 thr_apply_info_thr "1 1.2-3" \
223 # Likewise, but mix inferiors too.
224 thr_apply_info_thr "2.1 2-3" \
228 # Multiple ranges with mixed explicit inferiors.
229 thr_apply_info_thr "1.1-2 2.2-3" \
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"
238 # Now test using GDB convenience variables.
240 gdb_test "p \$inf = 1" " = 1"
241 gdb_test "p \$thr_start = 2" " = 2"
242 gdb_test "p \$thr_end = 3" " = 3"
244 # Convenience variable for the inferior number, only.
245 thr_apply_info_thr "\$inf.2" \
247 thr_apply_info_thr "\$inf.2-3" \
250 # Convenience variables for thread numbers as well.
251 foreach prefix {"" "1." "\$inf."} {
252 thr_apply_info_thr "${prefix}\$thr_start" \
254 thr_apply_info_thr "${prefix}\$thr_start-\$thr_end" \
256 thr_apply_info_thr "${prefix}2-\$thr_end" \
258 thr_apply_info_thr "${prefix}\$thr_start-3" \
261 # Undefined convenience variable.
262 set prefix_re [string_to_regexp $prefix]
263 thr_apply_info_thr_error "${prefix}\$conv123" \
265 "Convenience variable must have integer value\." \
266 "Invalid thread ID: ${prefix_re}\\\$conv123"]
269 # Convenience variables pointing at an inexisting thread and/or
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"
283 gdb_test "thread apply \$thr p 1234" \
284 "warning: Unknown thread 1.20" \
287 gdb_test "thread apply \$inf.1 p 1234" \
288 "warning: Unknown thread 30.1" \
289 "thread apply \$inf.1"
293 thr_apply_info_thr "1.*" \
296 thr_apply_info_thr "*" \
299 thr_apply_info_thr "1.* 2.1" \
302 thr_apply_info_thr "2.1 1.*" \
306 thr_apply_info_thr "1.* 2.*" \
307 "1.1 1.2 1.3 2.1 2.2 2.3"
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"
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.*" \
318 gdb_test "thread apply 1.1 3.* p 1" \
319 "Thread 1.1.*warning: Unknown inferior 3"
321 # Now test a set of invalid thread IDs/ranges.
323 thr_apply_info_thr_invalid "1." \
326 thr_apply_info_thr_invalid "1-3 1." \
329 thr_apply_info_thr_invalid "1.1.1" \
332 thr_apply_info_thr_invalid "2 1.1.1" \
335 thr_apply_info_thr_invalid "1.1.1 2" \
338 thr_apply_info_thr_invalid "1-2.1" \
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]
347 thr_apply_info_thr_invalid "${prefix}foo"
348 thr_apply_info_thr_invalid "${prefix}1foo"
349 thr_apply_info_thr_invalid "${prefix}foo1"
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"
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"
364 thr_apply_info_thr_error "${prefix}-1" "negative value"
365 thr_apply_info_thr_error "${prefix}-\$one" "negative value"
367 thr_apply_info_thr_error "${prefix}\$minus_one" \
368 "negative value: ${prefix_re}\\\$minus_one"
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*"
376 # Check that a valid thread ID list with a missing command errors
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
386 gdb_test "thread apply 1.*" $output
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\"\\."
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'\."
401 if { ![skip_python_tests] } {
402 with_test_prefix "python" {
403 # Check that InferiorThread.num and InferiorThread.global_num
404 # return the expected numbers.
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"
409 gdb_test "python print ('result = %s' % t0.global_num)" " = 6" \
410 "test InferiorThread.global_num"
412 # Breakpoint.thread expects global IDs. Confirm that that
415 gdb_breakpoint "thread_function1"
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"
428 # Remove the second inferior and confirm that GDB goes back to showing
429 # single-number thread IDs.
430 with_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"
435 # "info threads" while there's only inferior 1 should show
436 # single-number thread IDs.
437 info_threads "" "1 2 3"
439 gdb_test "thread" "Current thread is 1 .*"
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.
445 with_test_prefix "single-inferior but not initial" {
446 # Add another inferior.
447 gdb_test "add-inferior" "Added inferior 3.*" "add empty inferior"
449 # Now that we'd added another inferior, thread IDs should show the
451 info_threads "" "1.1 1.2 1.3" \
452 "info threads with multiple inferiors"
454 gdb_test "thread" "Current thread is 1\.1 .*"
456 gdb_test "inferior 3" "Switching to inferior 3 .*" "switch to inferior 3"
457 gdb_test "file ${binfile}" ".*" "load file in inferior 3"
461 gdb_test "remove-inferior 1" ".*" "remove inferior 1"
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"
468 gdb_test "thread" "Current thread is 3\.1 .*" "thread again"