xref: /netbsd-src/external/gpl3/gdb/dist/gdb/testsuite/gdb.python/py-thrhandle.exp (revision 7d62b00eb9ad855ffcd7da46b41e23feb5476fac)
1# Copyright (C) 2017-2020 Free Software Foundation, Inc.
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# Please email any bugs, comments, and/or additions to this file to:
17# bug-gdb@gnu.org
18
19# This file verifies that methods Inferior.thread_from_handle
20# and InferiorThread.handle work as expected.
21
22load_lib gdb-python.exp
23
24standard_testfile
25
26if {[gdb_compile_pthreads "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable debug] != "" } {
27    return -1
28}
29
30clean_restart ${binfile}
31
32# Skip all tests if Python scripting is not enabled.
33if { [skip_python_tests] } { continue }
34
35runto_main
36
37gdb_test "break after_mc_barrier" \
38    "Breakpoint 2 at .*: file .*${srcfile}, line .*" \
39         "breakpoint on after_mc_barrier"
40
41gdb_test "break do_something" \
42    "Breakpoint 3 at .*: file .*${srcfile}, line .*" \
43         "breakpoint on do_something"
44
45gdb_test "continue" \
46	"Breakpoint 2, after_mc_barrier .*" \
47	"run to after_mc_barrier"
48
49gdb_test_no_output "del 2" "delete after_mc_barrier breakpoint"
50
51gdb_test "continue" \
52	"Breakpoint 3, do_something .*" \
53	"run to do_something"
54
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.
62#
63# Note that \1 in the RE below is a backreference to the thread id
64# reported in the "Id" column.
65
66gdb_test "info threads"  \
67	{.*[\r\n]+\* +([0-9]+) +Thread[^\r\n]* do_something \(n=\1\) at.*}
68
69# Check for expected results when passing a valid thread handle to
70# thread_from_handle().
71
72gdb_test "python print(gdb.selected_inferior().thread_from_handle(gdb.parse_and_eval('thrs\[0\]')).num)" \
73	"1" "print thread id for thrs\[0\]"
74
75gdb_test "python print(gdb.selected_inferior().thread_from_handle(gdb.parse_and_eval('thrs\[1\]')).num)" \
76	"2" "print thread id for thrs\[1\]"
77
78gdb_test "python print(gdb.selected_inferior().thread_from_handle(gdb.parse_and_eval('thrs\[2\]')).num)" \
79	"3" "print thread id for thrs\[2\]"
80
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.
84
85gdb_test "python print(gdb.selected_inferior().thread_from_handle(gdb.parse_and_eval('thrs\[3\]')))" \
86	"None" "print thread for bogus handle thrs\[3\]"
87
88gdb_test "python print(gdb.selected_inferior().thread_from_handle(gdb.parse_and_eval('thrs\[4\]')))" \
89	"None" "print thread for bogus handle thrs\[4\]"
90
91# We should see an exception when passing an object of the wrong type.
92
93gdb_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"
96
97# We should see an exception when passing too large of an object.
98
99gdb_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"
102
103# We should see an exception when passing too small of an object.
104
105gdb_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"
108
109# Test the thread_handle method
110
111gdb_py_test_silent_cmd "python tp=gdb.lookup_type('pthread_t')" \
112		       "Get pthread_t type" 0
113gdb_py_test_silent_cmd "python inf=gdb.selected_inferior()" "Get inferior" 0
114
115foreach thrN {0 1 2} {
116    with_test_prefix "thread $thrN" {
117
118	gdb_py_test_silent_cmd \
119	    "python hand = gdb.parse_and_eval('thrs\[$thrN\]')" \
120	    "fetch thread handle from inferior" \
121	    1
122
123	gdb_py_test_silent_cmd \
124	    "python hand_bytes = inf.thread_from_handle(hand).handle()" \
125	    "fetch thread handle from thread" \
126	    1
127
128
129	# It'd be nice to be able to use this comparison expression:
130	#
131	#    hand == hand_bytes
132	#
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
136	# its constructor.
137
138        gdb_test "python print(gdb.Value(hand_bytes, tp) == hand)" \
139	         "True" \
140	         "verify that handles are the same"
141    }
142}
143