Commit | Line | Data |
---|---|---|
88b9d363 | 1 | # Copyright (C) 2009-2022 Free Software Foundation, Inc. |
595939de PM |
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 | # This file is part of the GDB testsuite. It tests the mechanism | |
17 | # exposing inferiors to Python. | |
18 | ||
a2c09bd0 | 19 | load_lib gdb-python.exp |
595939de | 20 | |
b4a58790 TT |
21 | standard_testfile |
22 | ||
f66713d2 | 23 | if { [gdb_compile_pthreads ${srcdir}/${subdir}/${srcfile} ${binfile} executable {debug}] != "" } { |
595939de PM |
24 | return -1 |
25 | } | |
26 | ||
27 | # Start with a fresh gdb. | |
28 | clean_restart ${testfile} | |
29 | ||
30 | # Skip all tests if Python scripting is not enabled. | |
31 | if { [skip_python_tests] } { continue } | |
32 | ||
805acca0 AA |
33 | switch [get_endianness] { |
34 | little { set python_pack_char "<" } | |
35 | big { set python_pack_char ">" } | |
ae9d7ce4 NF |
36 | } |
37 | ||
595939de PM |
38 | # The following tests require execution. |
39 | ||
40 | if ![runto_main] then { | |
bc6c7af4 | 41 | fail "can't run to main" |
595939de PM |
42 | return 0 |
43 | } | |
44 | ||
595939de PM |
45 | # Test basic gdb.Inferior attributes and methods. |
46 | ||
47 | gdb_py_test_silent_cmd "python inferiors = gdb.inferiors ()" "get inferiors list" 1 | |
1256af7d SM |
48 | gdb_test "python print (inferiors)" \ |
49 | "\\(<gdb.Inferior num=1, pid=$decimal>,\\)" "verify inferiors list" | |
595939de PM |
50 | gdb_py_test_silent_cmd "python i0 = inferiors\[0\]" "get first inferior" 0 |
51 | ||
9325cb04 PK |
52 | gdb_test "python print ('result = %s' % (i0 == inferiors\[0\]))" " = True" "test equality comparison (true)" |
53 | gdb_test "python print ('result = %s' % i0.num)" " = \[0-9\]+" "test Inferior.num" | |
55789354 | 54 | gdb_test "python print ('result = %s' % i0.connection_num)" " = \[0-9\]+" "test Inferior.connection_num" |
9325cb04 PK |
55 | gdb_test "python print ('result = %s' % i0.pid)" " = \[0-9\]+" "test Inferior.pid" |
56 | gdb_test "python print ('result = %s' % i0.was_attached)" " = False" "test Inferior.was_attached" | |
57 | gdb_test "python print (i0.threads ())" "\\(<gdb.InferiorThread object at 0x\[\[:xdigit:\]\]+>,\\)" "test Inferior.threads" | |
595939de | 58 | |
a40bf0c2 SM |
59 | gdb_test "python print (i0.progspace)" "<gdb.Progspace object at $hex>" |
60 | gdb_test "python print (i0.progspace == gdb.progspaces()\[0\])" "True" | |
61 | ||
f66713d2 JK |
62 | # Test the number of inferior threads. |
63 | ||
64 | gdb_breakpoint check_threads | |
65 | gdb_continue_to_breakpoint "cont to check_threads" ".*pthread_barrier_wait.*" | |
9325cb04 | 66 | gdb_test "python print (len (i0.threads ()))" "\r\n9" "test Inferior.threads 2" |
f66713d2 JK |
67 | |
68 | # Proceed to the next test. | |
69 | ||
70 | gdb_breakpoint [gdb_get_line_number "Break here."] | |
71 | gdb_continue_to_breakpoint "cont to Break here." ".*Break here\..*" | |
72 | ||
595939de PM |
73 | # Test memory read and write operations. |
74 | ||
75 | gdb_py_test_silent_cmd "python addr = gdb.selected_frame ().read_var ('str')" \ | |
76 | "read str address" 0 | |
9325cb04 | 77 | gdb_py_test_silent_cmd "python str = gdb.inferiors()\[0\].read_memory (addr, 5); print(str)" \ |
595939de | 78 | "read str contents" 1 |
9325cb04 PK |
79 | if { $gdb_py_is_py3k == 0 } { |
80 | gdb_py_test_silent_cmd "python a = 'a'" "" 0 | |
81 | } else { | |
82 | gdb_py_test_silent_cmd "python a = bytes('a', 'ascii')" "" 0 | |
83 | } | |
84 | gdb_py_test_silent_cmd "python str\[1\] = a" "change str" 0 | |
595939de PM |
85 | gdb_py_test_silent_cmd "python gdb.inferiors()\[0\].write_memory (addr, str)" \ |
86 | "write str" 1 | |
9325cb04 | 87 | gdb_test "print (str)" " = \"hallo, testsuite\"" \ |
595939de PM |
88 | "ensure str was changed in the inferior" |
89 | ||
90 | # Test memory search. | |
91 | ||
92 | set hex_number {0x[0-9a-fA-F][0-9a-fA-F]*} | |
93 | set dec_number {[0-9]+} | |
94 | set history_prefix {[$][0-9]* = } | |
95 | set newline {[\r\n]+} | |
96 | set pattern_not_found "${newline}.None" | |
97 | set one_pattern_found "${newline}.${dec_number}" | |
98 | ||
99 | # Test string pattern. | |
100 | ||
2cc57ad8 PA |
101 | with_test_prefix "string" { |
102 | gdb_test "set *(int32_t*) &int8_search_buf\[10\] = 0x61616161" | |
103 | gdb_test "py search_buf = gdb.selected_frame ().read_var ('int8_search_buf')" | |
104 | gdb_test_no_output "py start_addr = search_buf.address" | |
105 | gdb_test_no_output "py length = search_buf.type.sizeof" | |
106 | ||
107 | gdb_test "py print (gdb.inferiors()\[0\].search_memory (start_addr, length, 'aaa'))" \ | |
108 | "${one_pattern_found}" "find string pattern" | |
109 | ||
110 | # Test not finding pattern because search range too small, with | |
111 | # potential find at the edge of the range. | |
112 | gdb_test "py print (gdb.inferiors()\[0\].search_memory (start_addr, 10+3, 'aaaa'))" \ | |
113 | "${pattern_not_found}" "pattern not found at end of range" | |
114 | ||
115 | # Increase the search range by 1 and we should find the pattern. | |
116 | gdb_test "py print (gdb.inferiors()\[0\].search_memory (start_addr, 10+3+1, 'aaa'))" \ | |
117 | "${one_pattern_found}" "pattern found at end of range" | |
118 | } | |
595939de PM |
119 | |
120 | # Import struct to pack the following patterns. | |
9a5193cb | 121 | gdb_test_no_output "py from struct import *" |
595939de PM |
122 | |
123 | # Test 16-bit pattern. | |
124 | ||
2cc57ad8 PA |
125 | with_test_prefix "16-bit" { |
126 | gdb_test_no_output "set int16_search_buf\[10\] = 0x1234" | |
127 | gdb_test_no_output "py search_buf = gdb.selected_frame ().read_var ('int16_search_buf')" | |
128 | gdb_test_no_output "py start_addr = search_buf.address" | |
129 | gdb_test_no_output "py length = search_buf.type.sizeof" | |
130 | gdb_test_no_output "py pattern = pack('${python_pack_char}H',0x1234)" | |
131 | gdb_test "py print (gdb.inferiors()\[0\].search_memory (start_addr, length, pattern))" \ | |
132 | "${one_pattern_found}" "find 16-bit pattern, with value pattern" | |
133 | } | |
595939de PM |
134 | |
135 | # Test 32-bit pattern. | |
136 | ||
2cc57ad8 PA |
137 | with_test_prefix "32-bit" { |
138 | gdb_test_no_output "set int32_search_buf\[10\] = 0x12345678" | |
139 | gdb_test_no_output "py search_buf = gdb.selected_frame ().read_var ('int32_search_buf')" | |
140 | gdb_test_no_output "py start_addr = search_buf.address" | |
141 | gdb_test_no_output "py length = search_buf.type.sizeof" | |
142 | gdb_test_no_output "py pattern = pack('${python_pack_char}I',0x12345678)" | |
143 | gdb_test "py print (gdb.inferiors()\[0\].search_memory (start_addr, length, pattern))" \ | |
144 | "${one_pattern_found}" "find 32-bit pattern, with python pattern" | |
145 | } | |
595939de PM |
146 | |
147 | # Test 64-bit pattern. | |
148 | ||
2cc57ad8 PA |
149 | with_test_prefix "64-bit" { |
150 | gdb_test_no_output "set int64_search_buf\[10\] = 0xfedcba9876543210LL" | |
151 | gdb_test_no_output "py search_buf = gdb.selected_frame ().read_var ('int64_search_buf')" | |
152 | gdb_test_no_output "py start_addr = search_buf.address" | |
153 | gdb_test_no_output "py length = search_buf.type.sizeof" | |
154 | gdb_test_no_output "py pattern = pack('${python_pack_char}Q', 0xfedcba9876543210)" | |
155 | gdb_test "py print (gdb.inferiors()\[0\].search_memory (start_addr, length, pattern))" \ | |
156 | "${one_pattern_found}" "find 64-bit pattern, with value pattern" | |
157 | } | |
595939de PM |
158 | |
159 | # Test mixed-sized patterns. | |
160 | ||
2cc57ad8 PA |
161 | with_test_prefix "mixed-sized" { |
162 | gdb_test_no_output "set *(int8_t*) &search_buf\[10\] = 0x62" | |
163 | gdb_test_no_output "set *(int16_t*) &search_buf\[11\] = 0x6363" | |
164 | gdb_test_no_output "set *(int32_t*) &search_buf\[13\] = 0x64646464" | |
165 | gdb_test_no_output "py search_buf = gdb.selected_frame ().read_var ('search_buf')" | |
166 | gdb_test_no_output "py start_addr = search_buf\[0\].address" | |
167 | gdb_test_no_output "py pattern1 = pack('B', 0x62)" | |
168 | gdb_test_no_output "py pattern2 = pack('${python_pack_char}H', 0x6363)" | |
169 | gdb_test_no_output "py pattern3 = pack('${python_pack_char}I', 0x64646464)" | |
170 | ||
171 | gdb_test "py print (gdb.inferiors()\[0\].search_memory (start_addr, 100, pattern1))" \ | |
172 | "${one_pattern_found}" "find mixed-sized pattern 1" | |
173 | gdb_test "py print (gdb.inferiors()\[0\].search_memory (start_addr, 100, pattern2))" \ | |
174 | "${one_pattern_found}" "find mixed-sized pattern 2" | |
175 | gdb_test "py print (gdb.inferiors()\[0\].search_memory (start_addr, 100, pattern3))" \ | |
176 | "${one_pattern_found}" "find mixed-sized pattern 3" | |
177 | } | |
595939de PM |
178 | |
179 | # Test search spanning a large range, in the particular case of native | |
180 | # targets, test the search spanning multiple chunks. | |
181 | # Remote targets may implement the search differently. | |
182 | ||
4ec70201 | 183 | set CHUNK_SIZE 16000 |
2cc57ad8 PA |
184 | with_test_prefix "large range" { |
185 | gdb_test_no_output "set *(int32_t*) &search_buf\[0*${CHUNK_SIZE}+100\] = 0x12345678" | |
186 | gdb_test_no_output "set *(int32_t*) &search_buf\[1*${CHUNK_SIZE}+100\] = 0x12345678" | |
187 | gdb_test_no_output "py start_addr = gdb.selected_frame ().read_var ('search_buf')" | |
188 | gdb_test_no_output "py end_addr = start_addr + gdb.selected_frame ().read_var ('search_buf_size')" | |
189 | gdb_test_no_output "py pattern = pack('${python_pack_char}I', 0x12345678)" | |
190 | ||
191 | gdb_test_no_output "py first = gdb.inferiors()\[0\].search_memory (start_addr,end_addr - start_addr, pattern)" | |
192 | gdb_test "py print (first)" "${one_pattern_found}" "search spanning large range 1st result" | |
193 | gdb_test_no_output "py start_addr = first + 1" | |
194 | gdb_test_no_output "py second = gdb.inferiors()\[0\].search_memory (start_addr, end_addr - start_addr, pattern)" | |
195 | gdb_test "py print (second)" "${one_pattern_found}" "search spanning large range 2nd result" | |
196 | gdb_test_no_output "py start_addr = second + 1" | |
197 | gdb_test_no_output "py third = gdb.inferiors()\[0\].search_memory (start_addr, end_addr - start_addr, pattern)" | |
198 | gdb_test "py print (third)" "${pattern_not_found}" "search spanning large range 3rd result" | |
199 | } | |
595939de PM |
200 | |
201 | # For native targets, test a pattern straddling a chunk boundary. | |
202 | ||
203 | if [isnative] { | |
2cc57ad8 PA |
204 | with_test_prefix "straddling" { |
205 | gdb_test_no_output "set *(int32_t*) &search_buf\[${CHUNK_SIZE}-1\] = 0xfdb97531" | |
206 | gdb_test_no_output "py pattern = pack('${python_pack_char}I', 0xfdb97531)" | |
207 | gdb_test_no_output "py start_addr = gdb.selected_frame ().read_var ('search_buf')" | |
208 | gdb_test "py print (gdb.inferiors()\[0\].search_memory (start_addr, end_addr - start_addr, pattern))" \ | |
209 | "${one_pattern_found}" "find pattern straddling chunk boundary" | |
210 | } | |
595939de | 211 | } |
29703da4 PM |
212 | |
213 | # Test Inferior is_valid. This must always be the last test in | |
214 | # this testcase as it kills the inferior. | |
215 | ||
2cc57ad8 PA |
216 | with_test_prefix "is_valid" { |
217 | gdb_py_test_silent_cmd "python inf_list = gdb.inferiors()" "get initial list" 1 | |
218 | gdb_test "python print (len(inf_list))" "1" "get inferior list length 1" | |
219 | gdb_test "python print (inf_list\[0\].is_valid())" "True" \ | |
220 | "check inferior validity 1" | |
221 | ||
2a17c803 | 222 | gdb_test_multiline "install new inferior event handler" \ |
7c96f8c1 TT |
223 | "python" "" \ |
224 | "my_inferior_count = 1" "" \ | |
225 | "def new_inf_handler(evt):" "" \ | |
226 | " global my_inferior_count" "" \ | |
227 | " if evt.inferior is not None:" "" \ | |
228 | " my_inferior_count = my_inferior_count + 1" "" \ | |
229 | "gdb.events.new_inferior.connect(new_inf_handler)" "" \ | |
230 | "end" "" | |
2a17c803 | 231 | gdb_test_multiline "install inferior deleted event handler" \ |
7c96f8c1 TT |
232 | "python" "" \ |
233 | "def del_inf_handler(evt):" "" \ | |
234 | " global my_inferior_count" "" \ | |
235 | " if evt.inferior is not None:" "" \ | |
236 | " my_inferior_count = my_inferior_count - 1" "" \ | |
237 | "gdb.events.inferior_deleted.connect(del_inf_handler)" "" \ | |
238 | "end" "" | |
239 | ||
2cc57ad8 PA |
240 | gdb_test "add-inferior" "Added inferior 2.*" "add empty inferior 2" |
241 | gdb_py_test_silent_cmd "python inf_list = gdb.inferiors()" "get new list" 1 | |
242 | gdb_test "python print (len(inf_list))" "2" "get inferior list length 2" | |
243 | gdb_test "python print (inf_list\[0\].is_valid())" "True" \ | |
244 | "check inferior validity 2" | |
245 | ||
7c96f8c1 TT |
246 | gdb_test "python print (my_inferior_count)" "2" \ |
247 | "test new-inferior event handler" | |
248 | ||
2cc57ad8 PA |
249 | gdb_test "python print (inf_list\[1\].is_valid())" "True" \ |
250 | "check inferior validity 3" | |
251 | ||
252 | gdb_test_no_output "remove-inferiors 2" "remove-inferiors 3" | |
b05b1202 | 253 | gdb_test "python print (inf_list\[0\].is_valid())" "True" \ |
2cc57ad8 PA |
254 | "check inferior validity 4" |
255 | ||
b05b1202 | 256 | gdb_test "python print (inf_list\[1\].is_valid())" "False" \ |
2cc57ad8 | 257 | "check inferior validity 5" |
7c96f8c1 TT |
258 | |
259 | gdb_test "python print (my_inferior_count)" "1" \ | |
260 | "test inferior-deleted event handler" | |
4aa8e6c2 SM |
261 | |
262 | # Test that other properties and methods handle the removed inferior | |
263 | # correctly. | |
264 | gdb_test "python print (inf_list\[1\].num)" \ | |
265 | "RuntimeError: Inferior no longer exists.*" | |
55789354 TBA |
266 | gdb_test "python print (inf_list\[1\].connection_num)" \ |
267 | "RuntimeError: Inferior no longer exists.*" | |
4aa8e6c2 SM |
268 | gdb_test "python print (inf_list\[1\].pid)" \ |
269 | "RuntimeError: Inferior no longer exists.*" | |
270 | gdb_test "python print (inf_list\[1\].was_attached)" \ | |
271 | "RuntimeError: Inferior no longer exists.*" | |
a40bf0c2 SM |
272 | gdb_test "python print (inf_list\[1\].progspace)" \ |
273 | "RuntimeError: Inferior no longer exists.*" | |
4aa8e6c2 SM |
274 | gdb_test "python print (inf_list\[1\].threads ())" \ |
275 | "RuntimeError: Inferior no longer exists.*" | |
276 | gdb_test "python print (inf_list\[1\].thread_from_thread_handle (1))" \ | |
277 | "RuntimeError: Inferior no longer exists.*" | |
2cc57ad8 | 278 | } |
2aa48337 KP |
279 | |
280 | # Test gdb.selected_inferior() | |
2cc57ad8 PA |
281 | with_test_prefix "selected_inferior" { |
282 | gdb_test "inferior 1" ".*" "switch to first inferior" | |
283 | gdb_test "py print (gdb.selected_inferior().num)" "1" "first inferior selected" | |
55789354 TBA |
284 | gdb_test "py print (gdb.selected_inferior().connection_num)" "1" \ |
285 | "first inferior's connection" | |
286 | # Figure out if inf 1 has a native target. | |
287 | set inf_1_is_native [gdb_is_target_native] | |
2cc57ad8 | 288 | |
55789354 | 289 | gdb_test "add-inferior -no-connection" "Added inferior 3" "create new inferior" |
2cc57ad8 PA |
290 | gdb_test "inferior 3" ".*" "switch to third inferior" |
291 | gdb_test "py print (gdb.selected_inferior().num)" "3" "third inferior selected" | |
55789354 TBA |
292 | gdb_test "py print (gdb.selected_inferior().connection_num)" "None" \ |
293 | "third inferior's None connection" | |
294 | gdb_test "target native" "Done. Use the \"run\" command to start a process." \ | |
295 | "target for the third inferior" | |
296 | ||
297 | # If inf 1 has a native target, inf 3's target is shared with 1's. | |
298 | # Otherwise, it must have created a new target with a new number. | |
299 | if {$inf_1_is_native} { | |
300 | set expected_connection_num 1 | |
301 | } else { | |
302 | set expected_connection_num 2 | |
303 | } | |
304 | gdb_test "py print (gdb.selected_inferior().connection_num)" \ | |
305 | "$expected_connection_num" \ | |
306 | "third inferior's native connection" | |
307 | ||
2cc57ad8 PA |
308 | gdb_test "inferior 1" ".*" "switch back to first inferior" |
309 | gdb_test_no_output "remove-inferiors 3" "remove second inferior" | |
310 | } | |
1256af7d SM |
311 | |
312 | # Test repr()/str() | |
313 | with_test_prefix "__repr__" { | |
121b3efd | 314 | gdb_test "add-inferior" "Added inferior 4 on connection .*" "add inferior 4" |
1256af7d SM |
315 | gdb_py_test_silent_cmd "python infs = gdb.inferiors()" "get inferior list" 1 |
316 | gdb_test "python print (infs\[0\])" "<gdb.Inferior num=1, pid=$decimal>" | |
317 | gdb_test "python print (infs)" \ | |
318 | "\\\(<gdb.Inferior num=1, pid=$decimal>, <gdb.Inferior num=4, pid=$decimal>\\\)" \ | |
319 | "print all inferiors 1" | |
320 | gdb_test_no_output "remove-inferiors 4" | |
321 | gdb_test "python print (infs)" \ | |
322 | "\\\(<gdb.Inferior num=1, pid=$decimal>, <gdb.Inferior \\\(invalid\\\)>\\\)" \ | |
323 | "print all inferiors 2" | |
324 | } | |
add5ded5 TT |
325 | |
326 | # Test architecture. | |
327 | with_test_prefix "architecture" { | |
328 | gdb_test "inferior 1" ".*" "switch to first inferior" | |
329 | gdb_test "python print(gdb.selected_frame().architecture() is gdb.selected_inferior().architecture())" \ | |
330 | "True" \ | |
331 | "inferior architecture matches frame architecture" | |
332 | } |