Use correct inferior in Inferior.read_memory et al
[binutils-gdb.git] / gdb / testsuite / gdb.python / py-inferior.exp
1 # Copyright (C) 2009-2023 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 # This file is part of the GDB testsuite. It tests the mechanism
17 # exposing inferiors to Python.
18
19 load_lib gdb-python.exp
20
21 require allow_python_tests
22
23 standard_testfile
24
25 if { [gdb_compile_pthreads ${srcdir}/${subdir}/${srcfile} ${binfile} executable {debug}] != "" } {
26 return -1
27 }
28
29 # Start with a fresh gdb.
30 clean_restart ${testfile}
31
32 switch [get_endianness] {
33 little { set python_pack_char "<" }
34 big { set python_pack_char ">" }
35 }
36
37 # The following tests require execution.
38
39 if {![runto_main]} {
40 return 0
41 }
42
43 # The most recently added inferior.
44 set most_recent_inf 1
45
46 # A helper function that adds a new inferior. It returns the expected
47 # number of the new inferior. ARG is a string to pass to
48 # add-inferior.
49 proc add_inferior {{arg ""}} {
50 global most_recent_inf
51 incr most_recent_inf
52 gdb_test "add-inferior $arg" "Added inferior $most_recent_inf.*" \
53 "add inferior $most_recent_inf"
54 return $most_recent_inf
55 }
56
57 # Test basic gdb.Inferior attributes and methods.
58
59 gdb_py_test_silent_cmd "python inferiors = gdb.inferiors ()" "get inferiors list" 1
60 gdb_test "python print (inferiors)" \
61 "\\(<gdb.Inferior num=1, pid=$decimal>,\\)" "verify inferiors list"
62 gdb_py_test_silent_cmd "python i0 = inferiors\[0\]" "get first inferior" 0
63
64 gdb_test "python print ('result = %s' % (i0 == inferiors\[0\]))" " = True" "test equality comparison (true)"
65 gdb_test "python print ('result = %s' % i0.num)" " = \[0-9\]+" "test Inferior.num"
66 gdb_test "python print ('result = %s' % i0.connection_num)" " = \[0-9\]+" "test Inferior.connection_num"
67 gdb_test "python print ('result = %s' % (i0.connection_num == i0.connection.num))" " = True" \
68 "Inferior.connection_num equals Inferior.connection.num"
69 gdb_test "python print ('result = %s' % i0.pid)" " = \[0-9\]+" "test Inferior.pid"
70 gdb_test "python print ('result = %s' % i0.was_attached)" " = False" "test Inferior.was_attached"
71 gdb_test "python print (i0.threads ())" "\\(<gdb.InferiorThread object at 0x\[\[:xdigit:\]\]+>,\\)" "test Inferior.threads"
72
73 gdb_test "python print (i0.progspace)" "<gdb.Progspace object at $hex>"
74 gdb_test "python print (i0.progspace == gdb.progspaces()\[0\])" "True"
75
76 # Test the number of inferior threads.
77
78 gdb_breakpoint check_threads
79 gdb_continue_to_breakpoint "cont to check_threads" ".*pthread_barrier_wait.*"
80 gdb_test "python print (len (i0.threads ()))" "\r\n9" "test Inferior.threads 2"
81
82 # Proceed to the next test.
83
84 gdb_breakpoint [gdb_get_line_number "Break here."]
85 gdb_continue_to_breakpoint "cont to Break here." ".*Break here\..*"
86
87 # Test memory read and write operations.
88
89 gdb_py_test_silent_cmd "python addr = gdb.selected_frame ().read_var ('str')" \
90 "read str address" 0
91 gdb_test "python astr = gdb.inferiors()\[0\].read_memory (addr, 5); print(astr)" \
92 "<memory at $hex>" \
93 "read str contents"
94 gdb_test "python print(astr\[0\])" "b'h'"
95 gdb_py_test_silent_cmd "python a = bytes('a', 'ascii')" "" 0
96 gdb_py_test_silent_cmd "python astr\[1\] = a" "change str" 0
97 gdb_py_test_silent_cmd "python gdb.inferiors()\[0\].write_memory (addr, astr)" \
98 "write str" 1
99 gdb_test "print str" " = \"hallo, testsuite\"" \
100 "ensure str was changed in the inferior"
101
102 # Add a new inferior here, so we can test that operations work on the
103 # correct inferior.
104 set num [add_inferior]
105
106 # Test memory search.
107
108 set hex_number {0x[0-9a-fA-F][0-9a-fA-F]*}
109 set dec_number {[0-9]+}
110 set history_prefix {[$][0-9]* = }
111 set newline {[\r\n]+}
112 set pattern_not_found "${newline}.None"
113 set one_pattern_found "${newline}.${dec_number}"
114
115 # Test string pattern.
116
117 with_test_prefix "string" {
118 gdb_test "set *(int32_t*) &int8_search_buf\[10\] = 0x61616161"
119 gdb_test "py search_buf = gdb.selected_frame ().read_var ('int8_search_buf')"
120 gdb_test_no_output "py start_addr = search_buf.address"
121 gdb_test_no_output "py length = search_buf.type.sizeof"
122
123 # Switch to the new inferior before testing.
124 gdb_test "inferior $num" "Switching to inferior $num.*" \
125 "switch to inferior $num"
126
127 gdb_test "py print (gdb.inferiors()\[0\].search_memory (start_addr, length, 'aaa'))" \
128 "${one_pattern_found}" "find string pattern"
129
130 # Test not finding pattern because search range too small, with
131 # potential find at the edge of the range.
132 gdb_test "py print (gdb.inferiors()\[0\].search_memory (start_addr, 10+3, 'aaaa'))" \
133 "${pattern_not_found}" "pattern not found at end of range"
134
135 # Increase the search range by 1 and we should find the pattern.
136 gdb_test "py print (gdb.inferiors()\[0\].search_memory (start_addr, 10+3+1, 'aaa'))" \
137 "${one_pattern_found}" "pattern found at end of range"
138 }
139
140 # While still in the new inferior, test reading and writing memory
141 # again.
142 gdb_test "python astr = gdb.inferiors()\[0\].read_memory (addr, 5); print(astr)" \
143 "<memory at $hex>" \
144 "read str while other inferior selected"
145 gdb_test "python print(astr\[1\])" "b'a'" \
146 "print a character from the string"
147 gdb_py_test_silent_cmd "python astr\[1\] = b'X'" "change str again" 0
148 gdb_py_test_silent_cmd "python gdb.inferiors()\[0\].write_memory (addr, astr)" \
149 "write str while other inferior selected" 1
150
151 gdb_test "inferior 1" "Switching to inferior 1.*" "switch back to inferior 1"
152
153 gdb_test "print str" " = \"hXllo, testsuite\"" \
154 "ensure str was changed while other inferior selected"
155
156 gdb_test_no_output "remove-inferiors $num" "remove-inferiors $num"
157
158 # Import struct to pack the following patterns.
159 gdb_test_no_output "py from struct import *"
160
161 # Test 16-bit pattern.
162
163 with_test_prefix "16-bit" {
164 gdb_test_no_output "set int16_search_buf\[10\] = 0x1234"
165 gdb_test_no_output "py search_buf = gdb.selected_frame ().read_var ('int16_search_buf')"
166 gdb_test_no_output "py start_addr = search_buf.address"
167 gdb_test_no_output "py length = search_buf.type.sizeof"
168 gdb_test_no_output "py pattern = pack('${python_pack_char}H',0x1234)"
169 gdb_test "py print (gdb.inferiors()\[0\].search_memory (start_addr, length, pattern))" \
170 "${one_pattern_found}" "find 16-bit pattern, with value pattern"
171 }
172
173 # Test 32-bit pattern.
174
175 with_test_prefix "32-bit" {
176 gdb_test_no_output "set int32_search_buf\[10\] = 0x12345678"
177 gdb_test_no_output "py search_buf = gdb.selected_frame ().read_var ('int32_search_buf')"
178 gdb_test_no_output "py start_addr = search_buf.address"
179 gdb_test_no_output "py length = search_buf.type.sizeof"
180 gdb_test_no_output "py pattern = pack('${python_pack_char}I',0x12345678)"
181 gdb_test "py print (gdb.inferiors()\[0\].search_memory (start_addr, length, pattern))" \
182 "${one_pattern_found}" "find 32-bit pattern, with python pattern"
183 }
184
185 # Test 64-bit pattern.
186
187 with_test_prefix "64-bit" {
188 gdb_test_no_output "set int64_search_buf\[10\] = 0xfedcba9876543210LL"
189 gdb_test_no_output "py search_buf = gdb.selected_frame ().read_var ('int64_search_buf')"
190 gdb_test_no_output "py start_addr = search_buf.address"
191 gdb_test_no_output "py length = search_buf.type.sizeof"
192 gdb_test_no_output "py pattern = pack('${python_pack_char}Q', 0xfedcba9876543210)"
193 gdb_test "py print (gdb.inferiors()\[0\].search_memory (start_addr, length, pattern))" \
194 "${one_pattern_found}" "find 64-bit pattern, with value pattern"
195 }
196
197 # Test mixed-sized patterns.
198
199 with_test_prefix "mixed-sized" {
200 gdb_test_no_output "set *(int8_t*) &search_buf\[10\] = 0x62"
201 gdb_test_no_output "set *(int16_t*) &search_buf\[11\] = 0x6363"
202 gdb_test_no_output "set *(int32_t*) &search_buf\[13\] = 0x64646464"
203 gdb_test_no_output "py search_buf = gdb.selected_frame ().read_var ('search_buf')"
204 gdb_test_no_output "py start_addr = search_buf\[0\].address"
205 gdb_test_no_output "py pattern1 = pack('B', 0x62)"
206 gdb_test_no_output "py pattern2 = pack('${python_pack_char}H', 0x6363)"
207 gdb_test_no_output "py pattern3 = pack('${python_pack_char}I', 0x64646464)"
208
209 gdb_test "py print (gdb.inferiors()\[0\].search_memory (start_addr, 100, pattern1))" \
210 "${one_pattern_found}" "find mixed-sized pattern 1"
211 gdb_test "py print (gdb.inferiors()\[0\].search_memory (start_addr, 100, pattern2))" \
212 "${one_pattern_found}" "find mixed-sized pattern 2"
213 gdb_test "py print (gdb.inferiors()\[0\].search_memory (start_addr, 100, pattern3))" \
214 "${one_pattern_found}" "find mixed-sized pattern 3"
215 }
216
217 # Test search spanning a large range, in the particular case of native
218 # targets, test the search spanning multiple chunks.
219 # Remote targets may implement the search differently.
220
221 set CHUNK_SIZE 16000
222 with_test_prefix "large range" {
223 gdb_test_no_output "set *(int32_t*) &search_buf\[0*${CHUNK_SIZE}+100\] = 0x12345678"
224 gdb_test_no_output "set *(int32_t*) &search_buf\[1*${CHUNK_SIZE}+100\] = 0x12345678"
225 gdb_test_no_output "py start_addr = gdb.selected_frame ().read_var ('search_buf')"
226 gdb_test_no_output "py end_addr = start_addr + gdb.selected_frame ().read_var ('search_buf_size')"
227 gdb_test_no_output "py pattern = pack('${python_pack_char}I', 0x12345678)"
228
229 gdb_test_no_output "py first = gdb.inferiors()\[0\].search_memory (start_addr,end_addr - start_addr, pattern)"
230 gdb_test "py print (first)" "${one_pattern_found}" "search spanning large range 1st result"
231 gdb_test_no_output "py start_addr = first + 1"
232 gdb_test_no_output "py second = gdb.inferiors()\[0\].search_memory (start_addr, end_addr - start_addr, pattern)"
233 gdb_test "py print (second)" "${one_pattern_found}" "search spanning large range 2nd result"
234 gdb_test_no_output "py start_addr = second + 1"
235 gdb_test_no_output "py third = gdb.inferiors()\[0\].search_memory (start_addr, end_addr - start_addr, pattern)"
236 gdb_test "py print (third)" "${pattern_not_found}" "search spanning large range 3rd result"
237 }
238
239 # For native targets, test a pattern straddling a chunk boundary.
240
241 if [isnative] {
242 with_test_prefix "straddling" {
243 gdb_test_no_output "set *(int32_t*) &search_buf\[${CHUNK_SIZE}-1\] = 0xfdb97531"
244 gdb_test_no_output "py pattern = pack('${python_pack_char}I', 0xfdb97531)"
245 gdb_test_no_output "py start_addr = gdb.selected_frame ().read_var ('search_buf')"
246 gdb_test "py print (gdb.inferiors()\[0\].search_memory (start_addr, end_addr - start_addr, pattern))" \
247 "${one_pattern_found}" "find pattern straddling chunk boundary"
248 }
249 }
250
251 # Test Inferior is_valid.
252
253 with_test_prefix "is_valid" {
254 gdb_py_test_silent_cmd "python inf_list = gdb.inferiors()" "get initial list" 1
255 gdb_test "python print (len(inf_list))" "1" "get inferior list length 1"
256 gdb_test "python print (inf_list\[0\].is_valid())" "True" \
257 "check inferior validity 1"
258
259 # The "dummy" line below used to cause a gdb crash.
260 gdb_test_multiline "install new inferior event handler" \
261 "python" "" \
262 "my_inferior_count = 1" "" \
263 "def new_inf_handler(evt):" "" \
264 " global my_inferior_count" "" \
265 " if evt.inferior is not None:" "" \
266 " my_inferior_count = my_inferior_count + 1" "" \
267 " dummy = gdb.Value(True)" "" \
268 "gdb.events.new_inferior.connect(new_inf_handler)" "" \
269 "end" ""
270 gdb_test_multiline "install inferior deleted event handler" \
271 "python" "" \
272 "def del_inf_handler(evt):" "" \
273 " global my_inferior_count" "" \
274 " if evt.inferior is not None:" "" \
275 " my_inferior_count = my_inferior_count - 1" "" \
276 "gdb.events.inferior_deleted.connect(del_inf_handler)" "" \
277 "end" ""
278
279 set num [add_inferior]
280 gdb_py_test_silent_cmd "python inf_list = gdb.inferiors()" "get new list" 1
281 gdb_test "python print (len(inf_list))" "2" "get inferior list length 2"
282 gdb_test "python print (inf_list\[0\].is_valid())" "True" \
283 "check inferior validity 2"
284
285 gdb_test "python print (my_inferior_count)" "2" \
286 "test new-inferior event handler"
287
288 gdb_test "python print (inf_list\[1\].is_valid())" "True" \
289 "check inferior validity 3"
290
291 gdb_test_no_output "remove-inferiors $num"
292 gdb_test "python print (inf_list\[0\].is_valid())" "True" \
293 "check inferior validity 4"
294
295 gdb_test "python print (inf_list\[1\].is_valid())" "False" \
296 "check inferior validity 5"
297
298 gdb_test "python print (my_inferior_count)" "1" \
299 "test inferior-deleted event handler"
300
301 # Test that other properties and methods handle the removed inferior
302 # correctly.
303 gdb_test "python print (inf_list\[1\].num)" \
304 "RuntimeError: Inferior no longer exists.*"
305 gdb_test "python print (inf_list\[1\].connection_num)" \
306 "RuntimeError: Inferior no longer exists.*"
307 gdb_test "python print (inf_list\[1\].connection)" \
308 "RuntimeError: Inferior no longer exists.*"
309 gdb_test "python print (inf_list\[1\].pid)" \
310 "RuntimeError: Inferior no longer exists.*"
311 gdb_test "python print (inf_list\[1\].was_attached)" \
312 "RuntimeError: Inferior no longer exists.*"
313 gdb_test "python print (inf_list\[1\].progspace)" \
314 "RuntimeError: Inferior no longer exists.*"
315 gdb_test "python print (inf_list\[1\].threads ())" \
316 "RuntimeError: Inferior no longer exists.*"
317 gdb_test "python print (inf_list\[1\].thread_from_thread_handle (1))" \
318 "RuntimeError: Inferior no longer exists.*"
319 }
320
321 # Test gdb.selected_inferior()
322 with_test_prefix "selected_inferior" {
323 gdb_test "inferior 1" ".*" "switch to first inferior"
324 gdb_test "py print (gdb.selected_inferior().num)" "1" "first inferior selected"
325 gdb_test "py print (gdb.selected_inferior().connection_num)" "1" \
326 "first inferior's connection number"
327 gdb_test "py print (gdb.selected_inferior().connection.num)" "1" \
328 "first inferior's connection number, though connection object"
329 # Figure out if inf 1 has a native target.
330 set inf_1_is_native [gdb_is_target_native]
331
332 set num [add_inferior "-no-connection"]
333 gdb_test "inferior $num" ".*" "switch to inferior $num"
334 gdb_test "py print (gdb.selected_inferior().num)" "$num" \
335 "inferior $num selected"
336 gdb_test "py print (gdb.selected_inferior().connection_num)" "None" \
337 "inferior $num's None connection number"
338 gdb_test "py print (gdb.selected_inferior().connection)" "None" \
339 "inferior $num's None connection"
340 gdb_test "target native" "Done. Use the \"run\" command to start a process." \
341 "target for inferior $num"
342
343 # If inf 1 has a native target, inf 3's target is shared with 1's.
344 # Otherwise, it must have created a new target with a new number.
345 if {$inf_1_is_native} {
346 set expected_connection_num 1
347 } else {
348 set expected_connection_num 2
349 }
350 gdb_test "py print (gdb.selected_inferior().connection_num)" \
351 "$expected_connection_num" \
352 "inferior $num's native connection number"
353 gdb_test "py print (gdb.selected_inferior().connection.num)" \
354 "$expected_connection_num" \
355 "inferior $num's native connection number, though connection object"
356
357 # Test printing of gdb.TargetConnection object.
358 gdb_test "py print (gdb.selected_inferior().connection)" \
359 "<gdb.TargetConnection num=${expected_connection_num}, what=\"\[^\"\]+\">" \
360 "print a connection object"
361
362 gdb_test "inferior 1" ".*" "switch back to first inferior"
363 gdb_test_no_output "remove-inferiors $num"
364 }
365
366 # Test repr()/str()
367 with_test_prefix "__repr__" {
368 set num [add_inferior]
369 gdb_py_test_silent_cmd "python infs = gdb.inferiors()" "get inferior list" 1
370 gdb_test "python print (infs\[0\])" "<gdb.Inferior num=1, pid=$decimal>"
371 gdb_test "python print (infs)" \
372 "\\\(<gdb.Inferior num=1, pid=$decimal>, <gdb.Inferior num=$num, pid=$decimal>\\\)" \
373 "print all inferiors 1"
374 gdb_test_no_output "remove-inferiors $num"
375 gdb_test "python print (infs)" \
376 "\\\(<gdb.Inferior num=1, pid=$decimal>, <gdb.Inferior \\\(invalid\\\)>\\\)" \
377 "print all inferiors 2"
378 }
379
380 # Test architecture.
381 with_test_prefix "architecture" {
382 gdb_test "inferior 1" ".*" "switch to first inferior"
383 gdb_test "python print(gdb.selected_frame().architecture() is gdb.selected_inferior().architecture())" \
384 "True" \
385 "inferior architecture matches frame architecture"
386 }
387
388 gdb_test "python print(gdb.selected_inferior().main_name)" \
389 "main" \
390 "print main name"
391
392 gdb_test_no_output "set args x y z"
393 gdb_test "python print(gdb.selected_inferior().arguments)" \
394 "x y z" \
395 "print arguments"
396
397 gdb_test_no_output "python gdb.selected_inferior().arguments = 'a b c'" \
398 "set arguments from string"
399 gdb_test "show args" \
400 [string_to_regexp "Argument list to give program being debugged when it is started is \"a b c\"."] \
401 "show args from string"
402
403 gdb_test_no_output "python gdb.selected_inferior().arguments = \['a', 'b c'\]" \
404 "set arguments from list"
405 gdb_test "show args" \
406 [string_to_regexp "Argument list to give program being debugged when it is started is \"a b\\ c\"."] \
407 "show args from list"
408
409 gdb_test_no_output "python gdb.selected_inferior().clear_env()" \
410 "clear environment"
411 gdb_test_no_output "show environment"
412
413 gdb_test_no_output "python gdb.selected_inferior().set_env('DEI', 'value')" \
414 "set environment variable"
415 gdb_test "show environment" \
416 "DEI=value" \
417 "examine environment variable"
418
419 gdb_test_no_output "python gdb.selected_inferior().unset_env('DEI')" \
420 "unset environment variable"
421 gdb_test_no_output "show environment" \
422 "environment is empty again"