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