]>
Commit | Line | Data |
---|---|---|
1d506c26 | 1 | # Copyright (C) 2009-2024 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. | |
b0c431de TV |
30 | save_vars { env(ASAN_OPTIONS) } { |
31 | # The call to gdb.selected_inferior().read_memory (0, 0xffffffffffffffff) | |
32 | # triggers address sanitizer. Suppress the error, leaving us with just | |
33 | # this warning: | |
34 | # WARNING: AddressSanitizer failed to allocate 0xffffffffffffffff bytes | |
35 | set_sanitizer ASAN_OPTIONS allocator_may_return_null 1 | |
36 | ||
37 | clean_restart ${testfile} | |
38 | } | |
595939de | 39 | |
805acca0 AA |
40 | switch [get_endianness] { |
41 | little { set python_pack_char "<" } | |
42 | big { set python_pack_char ">" } | |
ae9d7ce4 NF |
43 | } |
44 | ||
06e967db TV |
45 | gdb_test \ |
46 | "python gdb.selected_inferior().read_memory (0, 0xffffffffffffffff)" \ | |
47 | [multi_line \ | |
48 | [string_to_regexp "Python Exception <class 'MemoryError'>: "] \ | |
49 | [string_to_regexp "Error occurred in Python."]] | |
50 | ||
6d30ada8 PA |
51 | # Test memory read operations without execution. |
52 | ||
53 | gdb_py_test_silent_cmd "python addr = gdb.lookup_global_symbol ('int8_global').value().address" \ | |
54 | "get global variable address" 0 | |
55 | gdb_test "python \ | |
56 | int8_global_mv = gdb.selected_inferior().read_memory (addr, 1); \ | |
57 | print(int.from_bytes(int8_global_mv\[0\], byteorder='little'))" \ | |
58 | "\r\n42" \ | |
59 | "read memory without execution" | |
60 | ||
595939de PM |
61 | # The following tests require execution. |
62 | ||
b0e16ca5 | 63 | if {![runto_main]} { |
595939de PM |
64 | return 0 |
65 | } | |
66 | ||
eef2e91d TT |
67 | # The most recently added inferior. |
68 | set most_recent_inf 1 | |
69 | ||
70 | # A helper function that adds a new inferior. It returns the expected | |
71 | # number of the new inferior. ARG is a string to pass to | |
72 | # add-inferior. | |
73 | proc add_inferior {{arg ""}} { | |
74 | global most_recent_inf | |
75 | incr most_recent_inf | |
76 | gdb_test "add-inferior $arg" "Added inferior $most_recent_inf.*" \ | |
77 | "add inferior $most_recent_inf" | |
78 | return $most_recent_inf | |
79 | } | |
80 | ||
595939de PM |
81 | # Test basic gdb.Inferior attributes and methods. |
82 | ||
83 | gdb_py_test_silent_cmd "python inferiors = gdb.inferiors ()" "get inferiors list" 1 | |
1256af7d SM |
84 | gdb_test "python print (inferiors)" \ |
85 | "\\(<gdb.Inferior num=1, pid=$decimal>,\\)" "verify inferiors list" | |
595939de PM |
86 | gdb_py_test_silent_cmd "python i0 = inferiors\[0\]" "get first inferior" 0 |
87 | ||
9325cb04 PK |
88 | gdb_test "python print ('result = %s' % (i0 == inferiors\[0\]))" " = True" "test equality comparison (true)" |
89 | gdb_test "python print ('result = %s' % i0.num)" " = \[0-9\]+" "test Inferior.num" | |
55789354 | 90 | gdb_test "python print ('result = %s' % i0.connection_num)" " = \[0-9\]+" "test Inferior.connection_num" |
0e3b7c25 AB |
91 | gdb_test "python print ('result = %s' % (i0.connection_num == i0.connection.num))" " = True" \ |
92 | "Inferior.connection_num equals Inferior.connection.num" | |
9325cb04 PK |
93 | gdb_test "python print ('result = %s' % i0.pid)" " = \[0-9\]+" "test Inferior.pid" |
94 | gdb_test "python print ('result = %s' % i0.was_attached)" " = False" "test Inferior.was_attached" | |
1925bba8 AB |
95 | gdb_test "python print (i0.threads ())" \ |
96 | "\\(<gdb.InferiorThread id=${decimal}\\.${decimal} target-id=\"\[^\r\n\]*\">,\\)" \ | |
97 | "test Inferior.threads" | |
595939de | 98 | |
a40bf0c2 SM |
99 | gdb_test "python print (i0.progspace)" "<gdb.Progspace object at $hex>" |
100 | gdb_test "python print (i0.progspace == gdb.progspaces()\[0\])" "True" | |
101 | ||
13cd9bce AB |
102 | # Add a user defined attribute to the inferior, and check the |
103 | # attribute can be read back. | |
104 | gdb_test_no_output "python i0._user_attr = 123" \ | |
105 | "add a user defined attribute to the inferior object" | |
106 | gdb_test "python print(i0._user_attr)" \ | |
107 | "123" "read back user defined attribute from i0" | |
108 | gdb_test "python print(gdb.inferiors()\[0\]._user_attr)" \ | |
109 | "123" "read back user defined attribute from gdb.inferiors" | |
110 | ||
98138c62 AB |
111 | # Record the main thread, and check its __repr__ while we're at it. |
112 | gdb_test_no_output "python main_thread = gdb.inferiors()\[0\].threads()\[0\]" | |
113 | gdb_test "python print(main_thread)" \ | |
114 | "<gdb.InferiorThread id=${decimal}\\.${decimal} target-id=\"\[^\r\n\]*\">" \ | |
115 | ||
f66713d2 JK |
116 | # Test the number of inferior threads. |
117 | ||
118 | gdb_breakpoint check_threads | |
119 | gdb_continue_to_breakpoint "cont to check_threads" ".*pthread_barrier_wait.*" | |
9325cb04 | 120 | gdb_test "python print (len (i0.threads ()))" "\r\n9" "test Inferior.threads 2" |
f66713d2 | 121 | |
98138c62 AB |
122 | # Grab a worker thread from the thread list. A worker thread is the |
123 | # first thread that is not the main thread. The worker thread object | |
124 | # will become invalid when the corresponding thread exits. | |
125 | gdb_test_no_output "python worker_thread = next(filter(lambda thr : thr != main_thread, i0.threads()))" | |
126 | gdb_test "python print(worker_thread)" \ | |
1925bba8 AB |
127 | "<gdb.InferiorThread id=${decimal}\\.${decimal} target-id=\"\[^\r\n\]*\">" \ |
128 | "test repr of a valid thread" | |
129 | ||
98138c62 AB |
130 | # Add a user defined attribute to the worker thread, check the |
131 | # attribute can be read back, and check the attribute is not present | |
132 | # on the main thread. | |
133 | gdb_test_no_output "python worker_thread._user_attribute = 123" \ | |
1d586eda | 134 | "add user defined attribute to InferiorThread object" |
98138c62 | 135 | gdb_test "python print(worker_thread._user_attribute)" "123" \ |
1d586eda | 136 | "read back user defined attribute" |
98138c62 | 137 | gdb_test "python print(main_thread._user_attribute)" \ |
1d586eda | 138 | [multi_line \ |
a207f6b3 TT |
139 | "AttributeError.*: 'gdb\\.InferiorThread' object has no attribute '_user_attribute'" \ |
140 | "Error occurred in Python.*"] \ | |
1d586eda AB |
141 | "attempt to read non-existent user defined attribute" |
142 | ||
f66713d2 JK |
143 | # Proceed to the next test. |
144 | ||
145 | gdb_breakpoint [gdb_get_line_number "Break here."] | |
146 | gdb_continue_to_breakpoint "cont to Break here." ".*Break here\..*" | |
147 | ||
1925bba8 | 148 | # Check the repr() for an invalid gdb.InferiorThread object. |
98138c62 | 149 | gdb_test "python print(worker_thread)" "<gdb.InferiorThread \\(invalid\\)>" \ |
1925bba8 AB |
150 | "test repr of an invalid thread" |
151 | ||
1d586eda | 152 | # Check the user defined attribute is still present on the invalid thread object. |
98138c62 | 153 | gdb_test "python print(worker_thread._user_attribute)" "123" \ |
1d586eda AB |
154 | "check user defined attribute on an invalid InferiorThread object" |
155 | ||
595939de PM |
156 | # Test memory read and write operations. |
157 | ||
158 | gdb_py_test_silent_cmd "python addr = gdb.selected_frame ().read_var ('str')" \ | |
159 | "read str address" 0 | |
817437c2 TT |
160 | gdb_test "python astr = gdb.inferiors()\[0\].read_memory (addr, 5); print(astr)" \ |
161 | "<memory at $hex>" \ | |
162 | "read str contents" | |
75ec0982 | 163 | gdb_test "python print(astr\[0\])" "b'h'" |
edae3fd6 | 164 | gdb_py_test_silent_cmd "python a = bytes('a', 'ascii')" "" 0 |
817437c2 TT |
165 | gdb_py_test_silent_cmd "python astr\[1\] = a" "change str" 0 |
166 | gdb_py_test_silent_cmd "python gdb.inferiors()\[0\].write_memory (addr, astr)" \ | |
595939de | 167 | "write str" 1 |
817437c2 | 168 | gdb_test "print str" " = \"hallo, testsuite\"" \ |
595939de PM |
169 | "ensure str was changed in the inferior" |
170 | ||
75ec0982 TT |
171 | # Add a new inferior here, so we can test that operations work on the |
172 | # correct inferior. | |
173 | set num [add_inferior] | |
174 | ||
13cd9bce AB |
175 | # Confirm the new inferior doesn't have the user defined attribute, |
176 | # but that the first inferior does still have the attribute. | |
177 | gdb_test "python print(gdb.inferiors()\[1\]._user_attr)" \ | |
178 | [multi_line \ | |
a207f6b3 TT |
179 | "AttributeError.*: 'gdb\\.Inferior' object has no attribute '_user_attr'" \ |
180 | "Error occurred in Python.*"] \ | |
13cd9bce AB |
181 | "check new inferior doesn't have user defined attribute" |
182 | gdb_test "python print(gdb.inferiors()\[0\]._user_attr)" \ | |
183 | "123" "read back user defined attribute again" | |
184 | ||
595939de PM |
185 | # Test memory search. |
186 | ||
187 | set hex_number {0x[0-9a-fA-F][0-9a-fA-F]*} | |
188 | set dec_number {[0-9]+} | |
189 | set history_prefix {[$][0-9]* = } | |
190 | set newline {[\r\n]+} | |
191 | set pattern_not_found "${newline}.None" | |
192 | set one_pattern_found "${newline}.${dec_number}" | |
193 | ||
194 | # Test string pattern. | |
195 | ||
2cc57ad8 PA |
196 | with_test_prefix "string" { |
197 | gdb_test "set *(int32_t*) &int8_search_buf\[10\] = 0x61616161" | |
198 | gdb_test "py search_buf = gdb.selected_frame ().read_var ('int8_search_buf')" | |
199 | gdb_test_no_output "py start_addr = search_buf.address" | |
200 | gdb_test_no_output "py length = search_buf.type.sizeof" | |
201 | ||
75ec0982 TT |
202 | # Switch to the new inferior before testing. |
203 | gdb_test "inferior $num" "Switching to inferior $num.*" \ | |
204 | "switch to inferior $num" | |
205 | ||
2cc57ad8 PA |
206 | gdb_test "py print (gdb.inferiors()\[0\].search_memory (start_addr, length, 'aaa'))" \ |
207 | "${one_pattern_found}" "find string pattern" | |
208 | ||
209 | # Test not finding pattern because search range too small, with | |
210 | # potential find at the edge of the range. | |
211 | gdb_test "py print (gdb.inferiors()\[0\].search_memory (start_addr, 10+3, 'aaaa'))" \ | |
212 | "${pattern_not_found}" "pattern not found at end of range" | |
213 | ||
214 | # Increase the search range by 1 and we should find the pattern. | |
215 | gdb_test "py print (gdb.inferiors()\[0\].search_memory (start_addr, 10+3+1, 'aaa'))" \ | |
216 | "${one_pattern_found}" "pattern found at end of range" | |
217 | } | |
595939de | 218 | |
75ec0982 TT |
219 | # While still in the new inferior, test reading and writing memory |
220 | # again. | |
221 | gdb_test "python astr = gdb.inferiors()\[0\].read_memory (addr, 5); print(astr)" \ | |
222 | "<memory at $hex>" \ | |
223 | "read str while other inferior selected" | |
224 | gdb_test "python print(astr\[1\])" "b'a'" \ | |
225 | "print a character from the string" | |
226 | gdb_py_test_silent_cmd "python astr\[1\] = b'X'" "change str again" 0 | |
227 | gdb_py_test_silent_cmd "python gdb.inferiors()\[0\].write_memory (addr, astr)" \ | |
228 | "write str while other inferior selected" 1 | |
229 | ||
230 | gdb_test "inferior 1" "Switching to inferior 1.*" "switch back to inferior 1" | |
231 | ||
232 | gdb_test "print str" " = \"hXllo, testsuite\"" \ | |
233 | "ensure str was changed while other inferior selected" | |
234 | ||
235 | gdb_test_no_output "remove-inferiors $num" "remove-inferiors $num" | |
236 | ||
595939de | 237 | # Import struct to pack the following patterns. |
9a5193cb | 238 | gdb_test_no_output "py from struct import *" |
595939de PM |
239 | |
240 | # Test 16-bit pattern. | |
241 | ||
2cc57ad8 PA |
242 | with_test_prefix "16-bit" { |
243 | gdb_test_no_output "set int16_search_buf\[10\] = 0x1234" | |
244 | gdb_test_no_output "py search_buf = gdb.selected_frame ().read_var ('int16_search_buf')" | |
245 | gdb_test_no_output "py start_addr = search_buf.address" | |
246 | gdb_test_no_output "py length = search_buf.type.sizeof" | |
247 | gdb_test_no_output "py pattern = pack('${python_pack_char}H',0x1234)" | |
248 | gdb_test "py print (gdb.inferiors()\[0\].search_memory (start_addr, length, pattern))" \ | |
249 | "${one_pattern_found}" "find 16-bit pattern, with value pattern" | |
250 | } | |
595939de PM |
251 | |
252 | # Test 32-bit pattern. | |
253 | ||
2cc57ad8 PA |
254 | with_test_prefix "32-bit" { |
255 | gdb_test_no_output "set int32_search_buf\[10\] = 0x12345678" | |
256 | gdb_test_no_output "py search_buf = gdb.selected_frame ().read_var ('int32_search_buf')" | |
257 | gdb_test_no_output "py start_addr = search_buf.address" | |
258 | gdb_test_no_output "py length = search_buf.type.sizeof" | |
259 | gdb_test_no_output "py pattern = pack('${python_pack_char}I',0x12345678)" | |
260 | gdb_test "py print (gdb.inferiors()\[0\].search_memory (start_addr, length, pattern))" \ | |
261 | "${one_pattern_found}" "find 32-bit pattern, with python pattern" | |
262 | } | |
595939de PM |
263 | |
264 | # Test 64-bit pattern. | |
265 | ||
2cc57ad8 PA |
266 | with_test_prefix "64-bit" { |
267 | gdb_test_no_output "set int64_search_buf\[10\] = 0xfedcba9876543210LL" | |
268 | gdb_test_no_output "py search_buf = gdb.selected_frame ().read_var ('int64_search_buf')" | |
269 | gdb_test_no_output "py start_addr = search_buf.address" | |
270 | gdb_test_no_output "py length = search_buf.type.sizeof" | |
271 | gdb_test_no_output "py pattern = pack('${python_pack_char}Q', 0xfedcba9876543210)" | |
272 | gdb_test "py print (gdb.inferiors()\[0\].search_memory (start_addr, length, pattern))" \ | |
273 | "${one_pattern_found}" "find 64-bit pattern, with value pattern" | |
274 | } | |
595939de PM |
275 | |
276 | # Test mixed-sized patterns. | |
277 | ||
2cc57ad8 PA |
278 | with_test_prefix "mixed-sized" { |
279 | gdb_test_no_output "set *(int8_t*) &search_buf\[10\] = 0x62" | |
280 | gdb_test_no_output "set *(int16_t*) &search_buf\[11\] = 0x6363" | |
281 | gdb_test_no_output "set *(int32_t*) &search_buf\[13\] = 0x64646464" | |
282 | gdb_test_no_output "py search_buf = gdb.selected_frame ().read_var ('search_buf')" | |
283 | gdb_test_no_output "py start_addr = search_buf\[0\].address" | |
284 | gdb_test_no_output "py pattern1 = pack('B', 0x62)" | |
285 | gdb_test_no_output "py pattern2 = pack('${python_pack_char}H', 0x6363)" | |
286 | gdb_test_no_output "py pattern3 = pack('${python_pack_char}I', 0x64646464)" | |
287 | ||
288 | gdb_test "py print (gdb.inferiors()\[0\].search_memory (start_addr, 100, pattern1))" \ | |
289 | "${one_pattern_found}" "find mixed-sized pattern 1" | |
290 | gdb_test "py print (gdb.inferiors()\[0\].search_memory (start_addr, 100, pattern2))" \ | |
291 | "${one_pattern_found}" "find mixed-sized pattern 2" | |
292 | gdb_test "py print (gdb.inferiors()\[0\].search_memory (start_addr, 100, pattern3))" \ | |
293 | "${one_pattern_found}" "find mixed-sized pattern 3" | |
294 | } | |
595939de PM |
295 | |
296 | # Test search spanning a large range, in the particular case of native | |
297 | # targets, test the search spanning multiple chunks. | |
298 | # Remote targets may implement the search differently. | |
299 | ||
4ec70201 | 300 | set CHUNK_SIZE 16000 |
2cc57ad8 PA |
301 | with_test_prefix "large range" { |
302 | gdb_test_no_output "set *(int32_t*) &search_buf\[0*${CHUNK_SIZE}+100\] = 0x12345678" | |
303 | gdb_test_no_output "set *(int32_t*) &search_buf\[1*${CHUNK_SIZE}+100\] = 0x12345678" | |
304 | gdb_test_no_output "py start_addr = gdb.selected_frame ().read_var ('search_buf')" | |
305 | gdb_test_no_output "py end_addr = start_addr + gdb.selected_frame ().read_var ('search_buf_size')" | |
306 | gdb_test_no_output "py pattern = pack('${python_pack_char}I', 0x12345678)" | |
307 | ||
308 | gdb_test_no_output "py first = gdb.inferiors()\[0\].search_memory (start_addr,end_addr - start_addr, pattern)" | |
309 | gdb_test "py print (first)" "${one_pattern_found}" "search spanning large range 1st result" | |
310 | gdb_test_no_output "py start_addr = first + 1" | |
311 | gdb_test_no_output "py second = gdb.inferiors()\[0\].search_memory (start_addr, end_addr - start_addr, pattern)" | |
312 | gdb_test "py print (second)" "${one_pattern_found}" "search spanning large range 2nd result" | |
313 | gdb_test_no_output "py start_addr = second + 1" | |
314 | gdb_test_no_output "py third = gdb.inferiors()\[0\].search_memory (start_addr, end_addr - start_addr, pattern)" | |
315 | gdb_test "py print (third)" "${pattern_not_found}" "search spanning large range 3rd result" | |
316 | } | |
595939de PM |
317 | |
318 | # For native targets, test a pattern straddling a chunk boundary. | |
319 | ||
320 | if [isnative] { | |
2cc57ad8 PA |
321 | with_test_prefix "straddling" { |
322 | gdb_test_no_output "set *(int32_t*) &search_buf\[${CHUNK_SIZE}-1\] = 0xfdb97531" | |
323 | gdb_test_no_output "py pattern = pack('${python_pack_char}I', 0xfdb97531)" | |
324 | gdb_test_no_output "py start_addr = gdb.selected_frame ().read_var ('search_buf')" | |
325 | gdb_test "py print (gdb.inferiors()\[0\].search_memory (start_addr, end_addr - start_addr, pattern))" \ | |
326 | "${one_pattern_found}" "find pattern straddling chunk boundary" | |
327 | } | |
595939de | 328 | } |
29703da4 | 329 | |
f5910419 | 330 | # Test Inferior is_valid. |
29703da4 | 331 | |
2cc57ad8 PA |
332 | with_test_prefix "is_valid" { |
333 | gdb_py_test_silent_cmd "python inf_list = gdb.inferiors()" "get initial list" 1 | |
334 | gdb_test "python print (len(inf_list))" "1" "get inferior list length 1" | |
335 | gdb_test "python print (inf_list\[0\].is_valid())" "True" \ | |
336 | "check inferior validity 1" | |
337 | ||
1da5d0e6 | 338 | # The "dummy" line below used to cause a gdb crash. |
2a17c803 | 339 | gdb_test_multiline "install new inferior event handler" \ |
7c96f8c1 TT |
340 | "python" "" \ |
341 | "my_inferior_count = 1" "" \ | |
342 | "def new_inf_handler(evt):" "" \ | |
343 | " global my_inferior_count" "" \ | |
344 | " if evt.inferior is not None:" "" \ | |
345 | " my_inferior_count = my_inferior_count + 1" "" \ | |
1da5d0e6 | 346 | " dummy = gdb.Value(True)" "" \ |
7c96f8c1 TT |
347 | "gdb.events.new_inferior.connect(new_inf_handler)" "" \ |
348 | "end" "" | |
2a17c803 | 349 | gdb_test_multiline "install inferior deleted event handler" \ |
7c96f8c1 TT |
350 | "python" "" \ |
351 | "def del_inf_handler(evt):" "" \ | |
352 | " global my_inferior_count" "" \ | |
353 | " if evt.inferior is not None:" "" \ | |
354 | " my_inferior_count = my_inferior_count - 1" "" \ | |
355 | "gdb.events.inferior_deleted.connect(del_inf_handler)" "" \ | |
356 | "end" "" | |
357 | ||
eef2e91d | 358 | set num [add_inferior] |
2cc57ad8 PA |
359 | gdb_py_test_silent_cmd "python inf_list = gdb.inferiors()" "get new list" 1 |
360 | gdb_test "python print (len(inf_list))" "2" "get inferior list length 2" | |
361 | gdb_test "python print (inf_list\[0\].is_valid())" "True" \ | |
362 | "check inferior validity 2" | |
363 | ||
7c96f8c1 TT |
364 | gdb_test "python print (my_inferior_count)" "2" \ |
365 | "test new-inferior event handler" | |
366 | ||
2cc57ad8 PA |
367 | gdb_test "python print (inf_list\[1\].is_valid())" "True" \ |
368 | "check inferior validity 3" | |
369 | ||
eef2e91d | 370 | gdb_test_no_output "remove-inferiors $num" |
b05b1202 | 371 | gdb_test "python print (inf_list\[0\].is_valid())" "True" \ |
2cc57ad8 PA |
372 | "check inferior validity 4" |
373 | ||
b05b1202 | 374 | gdb_test "python print (inf_list\[1\].is_valid())" "False" \ |
2cc57ad8 | 375 | "check inferior validity 5" |
7c96f8c1 TT |
376 | |
377 | gdb_test "python print (my_inferior_count)" "1" \ | |
378 | "test inferior-deleted event handler" | |
4aa8e6c2 SM |
379 | |
380 | # Test that other properties and methods handle the removed inferior | |
381 | # correctly. | |
382 | gdb_test "python print (inf_list\[1\].num)" \ | |
a207f6b3 | 383 | "RuntimeError.*: Inferior no longer exists.*" |
55789354 | 384 | gdb_test "python print (inf_list\[1\].connection_num)" \ |
a207f6b3 | 385 | "RuntimeError.*: Inferior no longer exists.*" |
0e3b7c25 | 386 | gdb_test "python print (inf_list\[1\].connection)" \ |
a207f6b3 | 387 | "RuntimeError.*: Inferior no longer exists.*" |
4aa8e6c2 | 388 | gdb_test "python print (inf_list\[1\].pid)" \ |
a207f6b3 | 389 | "RuntimeError.*: Inferior no longer exists.*" |
4aa8e6c2 | 390 | gdb_test "python print (inf_list\[1\].was_attached)" \ |
a207f6b3 | 391 | "RuntimeError.*: Inferior no longer exists.*" |
a40bf0c2 | 392 | gdb_test "python print (inf_list\[1\].progspace)" \ |
a207f6b3 | 393 | "RuntimeError.*: Inferior no longer exists.*" |
4aa8e6c2 | 394 | gdb_test "python print (inf_list\[1\].threads ())" \ |
a207f6b3 | 395 | "RuntimeError.*: Inferior no longer exists.*" |
4aa8e6c2 | 396 | gdb_test "python print (inf_list\[1\].thread_from_thread_handle (1))" \ |
a207f6b3 | 397 | "RuntimeError.*: Inferior no longer exists.*" |
2cc57ad8 | 398 | } |
2aa48337 KP |
399 | |
400 | # Test gdb.selected_inferior() | |
2cc57ad8 PA |
401 | with_test_prefix "selected_inferior" { |
402 | gdb_test "inferior 1" ".*" "switch to first inferior" | |
403 | gdb_test "py print (gdb.selected_inferior().num)" "1" "first inferior selected" | |
55789354 | 404 | gdb_test "py print (gdb.selected_inferior().connection_num)" "1" \ |
0e3b7c25 AB |
405 | "first inferior's connection number" |
406 | gdb_test "py print (gdb.selected_inferior().connection.num)" "1" \ | |
407 | "first inferior's connection number, though connection object" | |
55789354 | 408 | # Figure out if inf 1 has a native target. |
c7a2ee64 | 409 | set inf_1_is_native [gdb_protocol_is_native] |
2cc57ad8 | 410 | |
eef2e91d TT |
411 | set num [add_inferior "-no-connection"] |
412 | gdb_test "inferior $num" ".*" "switch to inferior $num" | |
413 | gdb_test "py print (gdb.selected_inferior().num)" "$num" \ | |
414 | "inferior $num selected" | |
55789354 | 415 | gdb_test "py print (gdb.selected_inferior().connection_num)" "None" \ |
eef2e91d | 416 | "inferior $num's None connection number" |
0e3b7c25 | 417 | gdb_test "py print (gdb.selected_inferior().connection)" "None" \ |
eef2e91d | 418 | "inferior $num's None connection" |
55789354 | 419 | gdb_test "target native" "Done. Use the \"run\" command to start a process." \ |
eef2e91d | 420 | "target for inferior $num" |
55789354 TBA |
421 | |
422 | # If inf 1 has a native target, inf 3's target is shared with 1's. | |
423 | # Otherwise, it must have created a new target with a new number. | |
424 | if {$inf_1_is_native} { | |
425 | set expected_connection_num 1 | |
426 | } else { | |
427 | set expected_connection_num 2 | |
428 | } | |
429 | gdb_test "py print (gdb.selected_inferior().connection_num)" \ | |
430 | "$expected_connection_num" \ | |
eef2e91d | 431 | "inferior $num's native connection number" |
0e3b7c25 AB |
432 | gdb_test "py print (gdb.selected_inferior().connection.num)" \ |
433 | "$expected_connection_num" \ | |
eef2e91d | 434 | "inferior $num's native connection number, though connection object" |
0e3b7c25 AB |
435 | |
436 | # Test printing of gdb.TargetConnection object. | |
437 | gdb_test "py print (gdb.selected_inferior().connection)" \ | |
438 | "<gdb.TargetConnection num=${expected_connection_num}, what=\"\[^\"\]+\">" \ | |
439 | "print a connection object" | |
55789354 | 440 | |
2cc57ad8 | 441 | gdb_test "inferior 1" ".*" "switch back to first inferior" |
eef2e91d | 442 | gdb_test_no_output "remove-inferiors $num" |
2cc57ad8 | 443 | } |
1256af7d SM |
444 | |
445 | # Test repr()/str() | |
446 | with_test_prefix "__repr__" { | |
eef2e91d | 447 | set num [add_inferior] |
1256af7d SM |
448 | gdb_py_test_silent_cmd "python infs = gdb.inferiors()" "get inferior list" 1 |
449 | gdb_test "python print (infs\[0\])" "<gdb.Inferior num=1, pid=$decimal>" | |
450 | gdb_test "python print (infs)" \ | |
eef2e91d | 451 | "\\\(<gdb.Inferior num=1, pid=$decimal>, <gdb.Inferior num=$num, pid=$decimal>\\\)" \ |
1256af7d | 452 | "print all inferiors 1" |
eef2e91d | 453 | gdb_test_no_output "remove-inferiors $num" |
1256af7d SM |
454 | gdb_test "python print (infs)" \ |
455 | "\\\(<gdb.Inferior num=1, pid=$decimal>, <gdb.Inferior \\\(invalid\\\)>\\\)" \ | |
456 | "print all inferiors 2" | |
457 | } | |
add5ded5 TT |
458 | |
459 | # Test architecture. | |
460 | with_test_prefix "architecture" { | |
461 | gdb_test "inferior 1" ".*" "switch to first inferior" | |
462 | gdb_test "python print(gdb.selected_frame().architecture() is gdb.selected_inferior().architecture())" \ | |
463 | "True" \ | |
464 | "inferior architecture matches frame architecture" | |
465 | } | |
31531132 TT |
466 | |
467 | gdb_test "python print(gdb.selected_inferior().main_name)" \ | |
468 | "main" \ | |
469 | "print main name" | |
470 | ||
471 | gdb_test_no_output "set args x y z" | |
472 | gdb_test "python print(gdb.selected_inferior().arguments)" \ | |
473 | "x y z" \ | |
474 | "print arguments" | |
475 | ||
476 | gdb_test_no_output "python gdb.selected_inferior().arguments = 'a b c'" \ | |
477 | "set arguments from string" | |
478 | gdb_test "show args" \ | |
479 | [string_to_regexp "Argument list to give program being debugged when it is started is \"a b c\"."] \ | |
480 | "show args from string" | |
481 | ||
482 | gdb_test_no_output "python gdb.selected_inferior().arguments = \['a', 'b c'\]" \ | |
483 | "set arguments from list" | |
484 | gdb_test "show args" \ | |
485 | [string_to_regexp "Argument list to give program being debugged when it is started is \"a b\\ c\"."] \ | |
486 | "show args from list" | |
487 | ||
488 | gdb_test_no_output "python gdb.selected_inferior().clear_env()" \ | |
489 | "clear environment" | |
490 | gdb_test_no_output "show environment" | |
491 | ||
492 | gdb_test_no_output "python gdb.selected_inferior().set_env('DEI', 'value')" \ | |
493 | "set environment variable" | |
494 | gdb_test "show environment" \ | |
495 | "DEI=value" \ | |
496 | "examine environment variable" | |
497 | ||
498 | gdb_test_no_output "python gdb.selected_inferior().unset_env('DEI')" \ | |
499 | "unset environment variable" | |
500 | gdb_test_no_output "show environment" \ | |
501 | "environment is empty again" |