Update copyright year range in all GDB files
[deliverable/binutils-gdb.git] / gdb / testsuite / gdb.python / py-inferior.exp
1 # Copyright (C) 2009-2018 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 standard_testfile
22
23 if { [gdb_compile_pthreads ${srcdir}/${subdir}/${srcfile} ${binfile} executable {debug}] != "" } {
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
33 switch [get_endianness] {
34 little { set python_pack_char "<" }
35 big { set python_pack_char ">" }
36 }
37
38 # The following tests require execution.
39
40 if ![runto_main] then {
41 fail "can't run to main"
42 return 0
43 }
44
45 # Test basic gdb.Inferior attributes and methods.
46
47 gdb_py_test_silent_cmd "python inferiors = gdb.inferiors ()" "get inferiors list" 1
48 gdb_test "python print (inferiors)" "\\(<gdb.Inferior object at 0x\[\[:xdigit:\]\]+>,\\)" "verify inferiors list"
49 gdb_py_test_silent_cmd "python i0 = inferiors\[0\]" "get first inferior" 0
50
51 gdb_test "python print ('result = %s' % (i0 == inferiors\[0\]))" " = True" "test equality comparison (true)"
52 gdb_test "python print ('result = %s' % i0.num)" " = \[0-9\]+" "test Inferior.num"
53 gdb_test "python print ('result = %s' % i0.pid)" " = \[0-9\]+" "test Inferior.pid"
54 gdb_test "python print ('result = %s' % i0.was_attached)" " = False" "test Inferior.was_attached"
55 gdb_test "python print (i0.threads ())" "\\(<gdb.InferiorThread object at 0x\[\[:xdigit:\]\]+>,\\)" "test Inferior.threads"
56
57 # Test the number of inferior threads.
58
59 gdb_breakpoint check_threads
60 gdb_continue_to_breakpoint "cont to check_threads" ".*pthread_barrier_wait.*"
61 gdb_test "python print (len (i0.threads ()))" "\r\n9" "test Inferior.threads 2"
62
63 # Proceed to the next test.
64
65 gdb_breakpoint [gdb_get_line_number "Break here."]
66 gdb_continue_to_breakpoint "cont to Break here." ".*Break here\..*"
67
68 # Test memory read and write operations.
69
70 gdb_py_test_silent_cmd "python addr = gdb.selected_frame ().read_var ('str')" \
71 "read str address" 0
72 gdb_py_test_silent_cmd "python str = gdb.inferiors()\[0\].read_memory (addr, 5); print(str)" \
73 "read str contents" 1
74 if { $gdb_py_is_py3k == 0 } {
75 gdb_py_test_silent_cmd "python a = 'a'" "" 0
76 } else {
77 gdb_py_test_silent_cmd "python a = bytes('a', 'ascii')" "" 0
78 }
79 gdb_py_test_silent_cmd "python str\[1\] = a" "change str" 0
80 gdb_py_test_silent_cmd "python gdb.inferiors()\[0\].write_memory (addr, str)" \
81 "write str" 1
82 gdb_test "print (str)" " = \"hallo, testsuite\"" \
83 "ensure str was changed in the inferior"
84
85 # Test memory search.
86
87 set hex_number {0x[0-9a-fA-F][0-9a-fA-F]*}
88 set dec_number {[0-9]+}
89 set history_prefix {[$][0-9]* = }
90 set newline {[\r\n]+}
91 set pattern_not_found "${newline}.None"
92 set one_pattern_found "${newline}.${dec_number}"
93
94 # Test string pattern.
95
96 with_test_prefix "string" {
97 gdb_test "set *(int32_t*) &int8_search_buf\[10\] = 0x61616161"
98 gdb_test "py search_buf = gdb.selected_frame ().read_var ('int8_search_buf')"
99 gdb_test_no_output "py start_addr = search_buf.address"
100 gdb_test_no_output "py length = search_buf.type.sizeof"
101
102 gdb_test "py print (gdb.inferiors()\[0\].search_memory (start_addr, length, 'aaa'))" \
103 "${one_pattern_found}" "find string pattern"
104
105 # Test not finding pattern because search range too small, with
106 # potential find at the edge of the range.
107 gdb_test "py print (gdb.inferiors()\[0\].search_memory (start_addr, 10+3, 'aaaa'))" \
108 "${pattern_not_found}" "pattern not found at end of range"
109
110 # Increase the search range by 1 and we should find the pattern.
111 gdb_test "py print (gdb.inferiors()\[0\].search_memory (start_addr, 10+3+1, 'aaa'))" \
112 "${one_pattern_found}" "pattern found at end of range"
113 }
114
115 # Import struct to pack the following patterns.
116 gdb_test_no_output "py from struct import *"
117
118 # Test 16-bit pattern.
119
120 with_test_prefix "16-bit" {
121 gdb_test_no_output "set int16_search_buf\[10\] = 0x1234"
122 gdb_test_no_output "py search_buf = gdb.selected_frame ().read_var ('int16_search_buf')"
123 gdb_test_no_output "py start_addr = search_buf.address"
124 gdb_test_no_output "py length = search_buf.type.sizeof"
125 gdb_test_no_output "py pattern = pack('${python_pack_char}H',0x1234)"
126 gdb_test "py print (gdb.inferiors()\[0\].search_memory (start_addr, length, pattern))" \
127 "${one_pattern_found}" "find 16-bit pattern, with value pattern"
128 }
129
130 # Test 32-bit pattern.
131
132 with_test_prefix "32-bit" {
133 gdb_test_no_output "set int32_search_buf\[10\] = 0x12345678"
134 gdb_test_no_output "py search_buf = gdb.selected_frame ().read_var ('int32_search_buf')"
135 gdb_test_no_output "py start_addr = search_buf.address"
136 gdb_test_no_output "py length = search_buf.type.sizeof"
137 gdb_test_no_output "py pattern = pack('${python_pack_char}I',0x12345678)"
138 gdb_test "py print (gdb.inferiors()\[0\].search_memory (start_addr, length, pattern))" \
139 "${one_pattern_found}" "find 32-bit pattern, with python pattern"
140 }
141
142 # Test 64-bit pattern.
143
144 with_test_prefix "64-bit" {
145 gdb_test_no_output "set int64_search_buf\[10\] = 0xfedcba9876543210LL"
146 gdb_test_no_output "py search_buf = gdb.selected_frame ().read_var ('int64_search_buf')"
147 gdb_test_no_output "py start_addr = search_buf.address"
148 gdb_test_no_output "py length = search_buf.type.sizeof"
149 gdb_test_no_output "py pattern = pack('${python_pack_char}Q', 0xfedcba9876543210)"
150 gdb_test "py print (gdb.inferiors()\[0\].search_memory (start_addr, length, pattern))" \
151 "${one_pattern_found}" "find 64-bit pattern, with value pattern"
152 }
153
154 # Test mixed-sized patterns.
155
156 with_test_prefix "mixed-sized" {
157 gdb_test_no_output "set *(int8_t*) &search_buf\[10\] = 0x62"
158 gdb_test_no_output "set *(int16_t*) &search_buf\[11\] = 0x6363"
159 gdb_test_no_output "set *(int32_t*) &search_buf\[13\] = 0x64646464"
160 gdb_test_no_output "py search_buf = gdb.selected_frame ().read_var ('search_buf')"
161 gdb_test_no_output "py start_addr = search_buf\[0\].address"
162 gdb_test_no_output "py pattern1 = pack('B', 0x62)"
163 gdb_test_no_output "py pattern2 = pack('${python_pack_char}H', 0x6363)"
164 gdb_test_no_output "py pattern3 = pack('${python_pack_char}I', 0x64646464)"
165
166 gdb_test "py print (gdb.inferiors()\[0\].search_memory (start_addr, 100, pattern1))" \
167 "${one_pattern_found}" "find mixed-sized pattern 1"
168 gdb_test "py print (gdb.inferiors()\[0\].search_memory (start_addr, 100, pattern2))" \
169 "${one_pattern_found}" "find mixed-sized pattern 2"
170 gdb_test "py print (gdb.inferiors()\[0\].search_memory (start_addr, 100, pattern3))" \
171 "${one_pattern_found}" "find mixed-sized pattern 3"
172 }
173
174 # Test search spanning a large range, in the particular case of native
175 # targets, test the search spanning multiple chunks.
176 # Remote targets may implement the search differently.
177
178 set CHUNK_SIZE 16000
179 with_test_prefix "large range" {
180 gdb_test_no_output "set *(int32_t*) &search_buf\[0*${CHUNK_SIZE}+100\] = 0x12345678"
181 gdb_test_no_output "set *(int32_t*) &search_buf\[1*${CHUNK_SIZE}+100\] = 0x12345678"
182 gdb_test_no_output "py start_addr = gdb.selected_frame ().read_var ('search_buf')"
183 gdb_test_no_output "py end_addr = start_addr + gdb.selected_frame ().read_var ('search_buf_size')"
184 gdb_test_no_output "py pattern = pack('${python_pack_char}I', 0x12345678)"
185
186 gdb_test_no_output "py first = gdb.inferiors()\[0\].search_memory (start_addr,end_addr - start_addr, pattern)"
187 gdb_test "py print (first)" "${one_pattern_found}" "search spanning large range 1st result"
188 gdb_test_no_output "py start_addr = first + 1"
189 gdb_test_no_output "py second = gdb.inferiors()\[0\].search_memory (start_addr, end_addr - start_addr, pattern)"
190 gdb_test "py print (second)" "${one_pattern_found}" "search spanning large range 2nd result"
191 gdb_test_no_output "py start_addr = second + 1"
192 gdb_test_no_output "py third = gdb.inferiors()\[0\].search_memory (start_addr, end_addr - start_addr, pattern)"
193 gdb_test "py print (third)" "${pattern_not_found}" "search spanning large range 3rd result"
194 }
195
196 # For native targets, test a pattern straddling a chunk boundary.
197
198 if [isnative] {
199 with_test_prefix "straddling" {
200 gdb_test_no_output "set *(int32_t*) &search_buf\[${CHUNK_SIZE}-1\] = 0xfdb97531"
201 gdb_test_no_output "py pattern = pack('${python_pack_char}I', 0xfdb97531)"
202 gdb_test_no_output "py start_addr = gdb.selected_frame ().read_var ('search_buf')"
203 gdb_test "py print (gdb.inferiors()\[0\].search_memory (start_addr, end_addr - start_addr, pattern))" \
204 "${one_pattern_found}" "find pattern straddling chunk boundary"
205 }
206 }
207
208 # Test Inferior is_valid. This must always be the last test in
209 # this testcase as it kills the inferior.
210
211 with_test_prefix "is_valid" {
212 gdb_py_test_silent_cmd "python inf_list = gdb.inferiors()" "get initial list" 1
213 gdb_test "python print (len(inf_list))" "1" "get inferior list length 1"
214 gdb_test "python print (inf_list\[0\].is_valid())" "True" \
215 "check inferior validity 1"
216
217 gdb_py_test_multiple "install new inferior event handler" \
218 "python" "" \
219 "my_inferior_count = 1" "" \
220 "def new_inf_handler(evt):" "" \
221 " global my_inferior_count" "" \
222 " if evt.inferior is not None:" "" \
223 " my_inferior_count = my_inferior_count + 1" "" \
224 "gdb.events.new_inferior.connect(new_inf_handler)" "" \
225 "end" ""
226 gdb_py_test_multiple "install inferior deleted event handler" \
227 "python" "" \
228 "def del_inf_handler(evt):" "" \
229 " global my_inferior_count" "" \
230 " if evt.inferior is not None:" "" \
231 " my_inferior_count = my_inferior_count - 1" "" \
232 "gdb.events.inferior_deleted.connect(del_inf_handler)" "" \
233 "end" ""
234
235 gdb_test "add-inferior" "Added inferior 2.*" "add empty inferior 2"
236 gdb_py_test_silent_cmd "python inf_list = gdb.inferiors()" "get new list" 1
237 gdb_test "python print (len(inf_list))" "2" "get inferior list length 2"
238 gdb_test "python print (inf_list\[0\].is_valid())" "True" \
239 "check inferior validity 2"
240
241 gdb_test "python print (my_inferior_count)" "2" \
242 "test new-inferior event handler"
243
244 gdb_test "python print (inf_list\[1\].is_valid())" "True" \
245 "check inferior validity 3"
246
247 gdb_test_no_output "remove-inferiors 2" "remove-inferiors 3"
248 gdb_test "python print (inf_list\[0\].is_valid())" "True" \
249 "check inferior validity 4"
250
251 gdb_test "python print (inf_list\[1\].is_valid())" "False" \
252 "check inferior validity 5"
253
254 gdb_test "python print (my_inferior_count)" "1" \
255 "test inferior-deleted event handler"
256 }
257
258 # Test gdb.selected_inferior()
259 with_test_prefix "selected_inferior" {
260 gdb_test "inferior 1" ".*" "switch to first inferior"
261 gdb_test "py print (gdb.selected_inferior().num)" "1" "first inferior selected"
262
263 gdb_test "add-inferior" "Added inferior 3" "create new inferior"
264 gdb_test "inferior 3" ".*" "switch to third inferior"
265 gdb_test "py print (gdb.selected_inferior().num)" "3" "third inferior selected"
266 gdb_test "inferior 1" ".*" "switch back to first inferior"
267 gdb_test_no_output "remove-inferiors 3" "remove second inferior"
268 }
This page took 0.040826 seconds and 5 git commands to generate.