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