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