1 # Copyright (C) 2017-2020 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 # Please email any bugs, comments, and/or additions to this file to:
19 # This file verifies that methods Inferior.thread_from_handle
20 # and InferiorThread.handle work as expected.
22 load_lib gdb-python.exp
26 if {[gdb_compile_pthreads "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable debug] != "" } {
30 clean_restart ${binfile}
32 # Skip all tests if Python scripting is not enabled.
33 if { [skip_python_tests] } { continue }
37 gdb_test "break after_mc_barrier" \
38 "Breakpoint 2 at .*: file .*${srcfile}, line .*" \
39 "breakpoint on after_mc_barrier"
41 gdb_test "break do_something" \
42 "Breakpoint 3 at .*: file .*${srcfile}, line .*" \
43 "breakpoint on do_something"
46 "Breakpoint 2, after_mc_barrier .*" \
47 "run to after_mc_barrier"
49 gdb_test_no_output "del 2" "delete after_mc_barrier breakpoint"
52 "Breakpoint 3, do_something .*" \
55 # The test case has been constructed so that the current thread,
56 # indicated by '*' in the "info threads" output, should be stopped in
57 # do_something() with a value of n which is the same as the number
58 # reported in the "Id" column. If it's not, then something went wrong
59 # with the start up sequence which should cause the main thread to be
60 # thread 1, the first child thread to be thread 2, and the second
61 # child thread to be thread 3.
63 # Note that \1 in the RE below is a backreference to the thread id
64 # reported in the "Id" column.
66 gdb_test "info threads" \
67 {.*[\r\n]+\* +([0-9]+) +Thread[^\r\n]* do_something \(n=\1\) at.*}
69 # Check for expected results when passing a valid thread handle to
70 # thread_from_handle().
72 gdb_test "python print(gdb.selected_inferior().thread_from_handle(gdb.parse_and_eval('thrs\[0\]')).num)" \
73 "1" "print thread id for thrs\[0\]"
75 gdb_test "python print(gdb.selected_inferior().thread_from_handle(gdb.parse_and_eval('thrs\[1\]')).num)" \
76 "2" "print thread id for thrs\[1\]"
78 gdb_test "python print(gdb.selected_inferior().thread_from_handle(gdb.parse_and_eval('thrs\[2\]')).num)" \
79 "3" "print thread id for thrs\[2\]"
81 # Objects which are of the correct size, but which are bogus thread
82 # handles should return None. For the first test (using thrs[3]), we
83 # use 0. For the second (thrs[4]), we use an unlikely bit pattern.
85 gdb_test "python print(gdb.selected_inferior().thread_from_handle(gdb.parse_and_eval('thrs\[3\]')))" \
86 "None" "print thread for bogus handle thrs\[3\]"
88 gdb_test "python print(gdb.selected_inferior().thread_from_handle(gdb.parse_and_eval('thrs\[4\]')))" \
89 "None" "print thread for bogus handle thrs\[4\]"
91 # We should see an exception when passing an object of the wrong type.
93 gdb_test "python print(gdb.selected_inferior().thread_from_handle(gdb.lookup_symbol('main')))" \
94 ".*TypeError: Argument 'handle' must be a thread handle object.*" \
95 "TypeError when passing a symbol object to thread_from_handle"
97 # We should see an exception when passing too large of an object.
99 gdb_test "python print(gdb.selected_inferior().thread_from_handle(gdb.parse_and_eval('thrs')))" \
100 ".*Thread handle size mismatch.*" \
101 "Pass overly large object to thread_from_handle"
103 # We should see an exception when passing too small of an object.
105 gdb_test "python print(gdb.selected_inferior().thread_from_handle(gdb.parse_and_eval('\"S\"')))" \
106 ".*Thread handle size mismatch.*" \
107 "Pass too small of an object to thread_from_handle"
109 # Test the thread_handle method
111 gdb_py_test_silent_cmd "python tp=gdb.lookup_type('pthread_t')" \
112 "Get pthread_t type" 0
113 gdb_py_test_silent_cmd "python inf=gdb.selected_inferior()" "Get inferior" 0
115 foreach thrN {0 1 2} {
116 with_test_prefix "thread $thrN" {
118 gdb_py_test_silent_cmd \
119 "python hand = gdb.parse_and_eval('thrs\[$thrN\]')" \
120 "fetch thread handle from inferior" \
123 gdb_py_test_silent_cmd \
124 "python hand_bytes = inf.thread_from_handle(hand).handle()" \
125 "fetch thread handle from thread" \
129 # It'd be nice to be able to use this comparison expression:
133 # But this won't work because hand is a gdb.Value and hand_bytes
134 # is a Python bytes object. Therefore, we convert the bytes
135 # object into a gdb.value by calling the two argument form of
138 gdb_test "python print(gdb.Value(hand_bytes, tp) == hand)" \
140 "verify that handles are the same"