Commit | Line | Data |
---|---|---|
88b9d363 | 1 | # Copyright (C) 2017-2022 Free Software Foundation, Inc. |
757bf54b KB |
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 | ||
2b0c8b01 | 19 | # This file verifies that methods Inferior.thread_from_handle |
947210e5 | 20 | # and InferiorThread.handle work as expected. |
757bf54b | 21 | |
947210e5 | 22 | load_lib gdb-python.exp |
757bf54b | 23 | |
947210e5 | 24 | standard_testfile |
757bf54b KB |
25 | |
26 | if {[gdb_compile_pthreads "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable debug] != "" } { | |
27 | return -1 | |
28 | } | |
29 | ||
30 | clean_restart ${binfile} | |
8e68731c YQ |
31 | |
32 | # Skip all tests if Python scripting is not enabled. | |
33 | if { [skip_python_tests] } { continue } | |
34 | ||
757bf54b KB |
35 | runto_main |
36 | ||
37 | gdb_test "break after_mc_barrier" \ | |
38 | "Breakpoint 2 at .*: file .*${srcfile}, line .*" \ | |
39 | "breakpoint on after_mc_barrier" | |
40 | ||
41 | gdb_test "break do_something" \ | |
42 | "Breakpoint 3 at .*: file .*${srcfile}, line .*" \ | |
43 | "breakpoint on do_something" | |
44 | ||
45 | gdb_test "continue" \ | |
46 | "Breakpoint 2, after_mc_barrier .*" \ | |
47 | "run to after_mc_barrier" | |
48 | ||
49 | gdb_test_no_output "del 2" "delete after_mc_barrier breakpoint" | |
50 | ||
51 | gdb_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 | ||
66 | gdb_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 | |
2b0c8b01 | 70 | # thread_from_handle(). |
757bf54b | 71 | |
2b0c8b01 | 72 | gdb_test "python print(gdb.selected_inferior().thread_from_handle(gdb.parse_and_eval('thrs\[0\]')).num)" \ |
757bf54b KB |
73 | "1" "print thread id for thrs\[0\]" |
74 | ||
2b0c8b01 | 75 | gdb_test "python print(gdb.selected_inferior().thread_from_handle(gdb.parse_and_eval('thrs\[1\]')).num)" \ |
757bf54b KB |
76 | "2" "print thread id for thrs\[1\]" |
77 | ||
2b0c8b01 | 78 | gdb_test "python print(gdb.selected_inferior().thread_from_handle(gdb.parse_and_eval('thrs\[2\]')).num)" \ |
757bf54b KB |
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 | ||
2b0c8b01 | 85 | gdb_test "python print(gdb.selected_inferior().thread_from_handle(gdb.parse_and_eval('thrs\[3\]')))" \ |
757bf54b KB |
86 | "None" "print thread for bogus handle thrs\[3\]" |
87 | ||
2b0c8b01 | 88 | gdb_test "python print(gdb.selected_inferior().thread_from_handle(gdb.parse_and_eval('thrs\[4\]')))" \ |
757bf54b KB |
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 | ||
2b0c8b01 KB |
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" | |
757bf54b KB |
96 | |
97 | # We should see an exception when passing too large of an object. | |
98 | ||
2b0c8b01 | 99 | gdb_test "python print(gdb.selected_inferior().thread_from_handle(gdb.parse_and_eval('thrs')))" \ |
757bf54b | 100 | ".*Thread handle size mismatch.*" \ |
2b0c8b01 | 101 | "Pass overly large object to thread_from_handle" |
757bf54b KB |
102 | |
103 | # We should see an exception when passing too small of an object. | |
104 | ||
2b0c8b01 | 105 | gdb_test "python print(gdb.selected_inferior().thread_from_handle(gdb.parse_and_eval('\"S\"')))" \ |
757bf54b | 106 | ".*Thread handle size mismatch.*" \ |
2b0c8b01 | 107 | "Pass too small of an object to thread_from_handle" |
947210e5 KB |
108 | |
109 | # Test the thread_handle method | |
110 | ||
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 | |
114 | ||
115 | foreach 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 \ | |
2b0c8b01 | 124 | "python hand_bytes = inf.thread_from_handle(hand).handle()" \ |
947210e5 KB |
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 | } |