Commit | Line | Data |
---|---|---|
7b6bb8da | 1 | # Copyright (C) 2009, 2010, 2011 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 | ||
19 | if $tracelevel then { | |
20 | strace $tracelevel | |
21 | } | |
22 | ||
a2c09bd0 | 23 | load_lib gdb-python.exp |
595939de PM |
24 | |
25 | set testfile "py-inferior" | |
26 | set srcfile ${testfile}.c | |
27 | if { [prepare_for_testing ${testfile}.exp ${testfile} ${srcfile}] } { | |
28 | return -1 | |
29 | } | |
30 | ||
31 | # Start with a fresh gdb. | |
32 | clean_restart ${testfile} | |
33 | ||
34 | # Skip all tests if Python scripting is not enabled. | |
35 | if { [skip_python_tests] } { continue } | |
36 | ||
ae9d7ce4 NF |
37 | gdb_test_multiple "show endian" "getting target endian" { |
38 | -re ".*little endian.*$gdb_prompt $" { | |
39 | set python_pack_char "<" | |
40 | # pass silently | |
41 | } | |
42 | -re ".*big endian.*$gdb_prompt $" { | |
43 | set python_pack_char ">" | |
44 | # pass silently | |
45 | } | |
46 | } | |
47 | ||
595939de PM |
48 | # The following tests require execution. |
49 | ||
50 | if ![runto_main] then { | |
51 | fail "Can't run to main" | |
52 | return 0 | |
53 | } | |
54 | ||
55 | runto [gdb_get_line_number "Break here."] | |
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" "\\(<gdb.Inferior object at 0x\[\[:xdigit:\]\]+>,\\)" "verify inferiors list" | |
61 | gdb_py_test_silent_cmd "python i0 = inferiors\[0\]" "get first inferior" 0 | |
62 | ||
63 | gdb_test "python print 'result =', i0 == inferiors\[0\]" " = True" "test equality comparison (true)" | |
64 | gdb_test "python print 'result =', i0.num" " = \[0-9\]+" "test Inferior.num" | |
65 | gdb_test "python print 'result =', i0.pid" " = \[0-9\]+" "test Inferior.pid" | |
66 | gdb_test "python print 'result =', i0.was_attached" " = False" "test Inferior.was_attached" | |
67 | gdb_test "python print i0.threads ()" "\\(<gdb.InferiorThread object at 0x\[\[:xdigit:\]\]+>,\\)" "test Inferior.threads" | |
68 | ||
69 | # Test memory read and write operations. | |
70 | ||
71 | gdb_py_test_silent_cmd "python addr = gdb.selected_frame ().read_var ('str')" \ | |
72 | "read str address" 0 | |
73 | gdb_py_test_silent_cmd "python str = gdb.inferiors()\[0\].read_memory (addr, 5)" \ | |
74 | "read str contents" 1 | |
75 | gdb_py_test_silent_cmd "python str\[1\] = 'a'" "change str" 0 | |
76 | gdb_py_test_silent_cmd "python gdb.inferiors()\[0\].write_memory (addr, str)" \ | |
77 | "write str" 1 | |
78 | gdb_test "print str" " = 0x\[\[:xdigit:\]\]+ \"hallo, testsuite\"" \ | |
79 | "ensure str was changed in the inferior" | |
80 | ||
81 | # Test memory search. | |
82 | ||
83 | set hex_number {0x[0-9a-fA-F][0-9a-fA-F]*} | |
84 | set dec_number {[0-9]+} | |
85 | set history_prefix {[$][0-9]* = } | |
86 | set newline {[\r\n]+} | |
87 | set pattern_not_found "${newline}.None" | |
88 | set one_pattern_found "${newline}.${dec_number}" | |
89 | ||
90 | # Test string pattern. | |
91 | ||
92 | gdb_test "set *(int32_t*) &int8_search_buf\[10\] = 0x61616161" "" "" | |
93 | gdb_test "py search_buf = gdb.selected_frame ().read_var ('int8_search_buf')" "" "" | |
9a5193cb JB |
94 | gdb_test_no_output "py start_addr = search_buf.address" |
95 | gdb_test_no_output "py length = search_buf.type.sizeof" | |
595939de PM |
96 | |
97 | gdb_test "py print gdb.inferiors()\[0\].search_memory (start_addr, length, 'aaa')" \ | |
98 | "${one_pattern_found}" "find string pattern" | |
99 | ||
100 | # Test not finding pattern because search range too small, with | |
101 | # potential find at the edge of the range. | |
102 | ||
103 | gdb_test "py print gdb.inferiors()\[0\].search_memory (start_addr, 10+3, 'aaaa')" \ | |
104 | "${pattern_not_found}" "pattern not found at end of range" | |
105 | ||
106 | # Increase the search range by 1 and we should find the pattern. | |
107 | ||
108 | gdb_test "py print gdb.inferiors()\[0\].search_memory (start_addr, 10+3+1, 'aaa')" \ | |
109 | "${one_pattern_found}" "pattern found at end of range" | |
110 | ||
111 | # Import struct to pack the following patterns. | |
9a5193cb | 112 | gdb_test_no_output "py from struct import *" |
595939de PM |
113 | |
114 | # Test 16-bit pattern. | |
115 | ||
9a5193cb JB |
116 | gdb_test_no_output "set int16_search_buf\[10\] = 0x1234" |
117 | gdb_test_no_output "py search_buf = gdb.selected_frame ().read_var ('int16_search_buf')" | |
118 | gdb_test_no_output "py start_addr = search_buf.address" | |
119 | gdb_test_no_output "py length = search_buf.type.sizeof" | |
120 | gdb_test_no_output "py pattern = pack('${python_pack_char}H',0x1234)" | |
595939de PM |
121 | |
122 | gdb_test "py print gdb.inferiors()\[0\].search_memory (start_addr, length, pattern)" \ | |
123 | "${one_pattern_found}" "find 16-bit pattern, with value pattern" | |
124 | ||
125 | # Test 32-bit pattern. | |
126 | ||
9a5193cb JB |
127 | gdb_test_no_output "set int32_search_buf\[10\] = 0x12345678" |
128 | gdb_test_no_output "py search_buf = gdb.selected_frame ().read_var ('int32_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}I',0x12345678)" | |
595939de PM |
132 | |
133 | gdb_test "py print gdb.inferiors()\[0\].search_memory (start_addr, length, pattern)" \ | |
134 | "${one_pattern_found}" "find 32-bit pattern, with python pattern" | |
135 | ||
136 | # Test 64-bit pattern. | |
137 | ||
9a5193cb JB |
138 | gdb_test_no_output "set int64_search_buf\[10\] = 0xfedcba9876543210LL" |
139 | gdb_test_no_output "py search_buf = gdb.selected_frame ().read_var ('int64_search_buf')" | |
140 | gdb_test_no_output "py start_addr = search_buf.address" | |
141 | gdb_test_no_output "py length = search_buf.type.sizeof" | |
142 | gdb_test_no_output "py pattern = pack('${python_pack_char}Q', 0xfedcba9876543210)" | |
595939de PM |
143 | |
144 | gdb_test "py print gdb.inferiors()\[0\].search_memory (start_addr, length, pattern)" \ | |
145 | "${one_pattern_found}" "find 64-bit pattern, with value pattern" | |
146 | ||
147 | # Test mixed-sized patterns. | |
148 | ||
9a5193cb JB |
149 | gdb_test_no_output "set *(int8_t*) &search_buf\[10\] = 0x62" |
150 | gdb_test_no_output "set *(int16_t*) &search_buf\[11\] = 0x6363" | |
151 | gdb_test_no_output "set *(int32_t*) &search_buf\[13\] = 0x64646464" | |
152 | gdb_test_no_output "py search_buf = gdb.selected_frame ().read_var ('search_buf')" | |
153 | gdb_test_no_output "py start_addr = search_buf\[0\].address" | |
154 | gdb_test_no_output "py pattern1 = pack('B', 0x62)" | |
155 | gdb_test_no_output "py pattern2 = pack('${python_pack_char}H', 0x6363)" | |
156 | gdb_test_no_output "py pattern3 = pack('${python_pack_char}I', 0x64646464)" | |
595939de PM |
157 | |
158 | gdb_test "py print gdb.inferiors()\[0\].search_memory (start_addr, 100, pattern1)" \ | |
159 | "${one_pattern_found}" "find mixed-sized pattern" | |
160 | gdb_test "py print gdb.inferiors()\[0\].search_memory (start_addr, 100, pattern2)" \ | |
161 | "${one_pattern_found}" "find mixed-sized pattern" | |
162 | gdb_test "py print gdb.inferiors()\[0\].search_memory (start_addr, 100, pattern3)" \ | |
163 | "${one_pattern_found}" "find mixed-sized pattern" | |
164 | ||
165 | # Test search spanning a large range, in the particular case of native | |
166 | # targets, test the search spanning multiple chunks. | |
167 | # Remote targets may implement the search differently. | |
168 | ||
169 | set CHUNK_SIZE 16000 ; | |
170 | ||
9a5193cb JB |
171 | gdb_test_no_output "set *(int32_t*) &search_buf\[0*${CHUNK_SIZE}+100\] = 0x12345678" |
172 | gdb_test_no_output "set *(int32_t*) &search_buf\[1*${CHUNK_SIZE}+100\] = 0x12345678" | |
173 | gdb_test_no_output "py start_addr = gdb.selected_frame ().read_var ('search_buf')" | |
c171f854 | 174 | gdb_test_no_output "py end_addr = start_addr + gdb.selected_frame ().read_var ('search_buf_size')" |
9a5193cb | 175 | gdb_test_no_output "py pattern = pack('${python_pack_char}I', 0x12345678)" |
c171f854 | 176 | gdb_test_no_output "py first = gdb.inferiors()\[0\].search_memory (start_addr,end_addr - start_addr, pattern)" |
595939de | 177 | gdb_test "py print first" "${one_pattern_found}" "search spanning large range 1st result" |
9a5193cb | 178 | gdb_test_no_output "py start_addr = first + 1" |
c171f854 | 179 | gdb_test_no_output "py second = gdb.inferiors()\[0\].search_memory (start_addr, end_addr - start_addr, pattern)" |
595939de | 180 | gdb_test "py print second" "${one_pattern_found}" "search spanning large range 2nd result" |
9a5193cb | 181 | gdb_test_no_output "py start_addr = second + 1" |
c171f854 | 182 | gdb_test_no_output "py third = gdb.inferiors()\[0\].search_memory (start_addr, end_addr - start_addr, pattern)" |
595939de PM |
183 | gdb_test "py print third" "${pattern_not_found}" "search spanning large range 3rd result" |
184 | ||
185 | # For native targets, test a pattern straddling a chunk boundary. | |
186 | ||
187 | if [isnative] { | |
9a5193cb JB |
188 | gdb_test_no_output "set *(int32_t*) &search_buf\[${CHUNK_SIZE}-1\] = 0xfdb97531" |
189 | gdb_test_no_output "py pattern = pack('${python_pack_char}I', 0xfdb97531)" | |
190 | gdb_test_no_output "py start_addr = gdb.selected_frame ().read_var ('search_buf')" | |
c171f854 | 191 | gdb_test "py print gdb.inferiors()\[0\].search_memory (start_addr, end_addr - start_addr, pattern)" \ |
595939de PM |
192 | "${one_pattern_found}" "find pattern straddling chunk boundary" |
193 | } |