1 # Copyright (C) 2009, 2010 Free Software Foundation, Inc.
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.
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.
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/>.
16 # This file is part of the GDB testsuite. It tests the mechanism
17 # exposing inferiors to Python.
23 load_lib gdb-python.exp
25 set testfile "py-inferior"
26 set srcfile ${testfile}.c
27 if { [prepare_for_testing ${testfile}.exp ${testfile} ${srcfile}] } {
31 # Start with a fresh gdb.
32 clean_restart ${testfile}
34 # Skip all tests if Python scripting is not enabled.
35 if { [skip_python_tests] } { continue }
37 # The following tests require execution.
39 if ![runto_main] then {
40 fail "Can't run to main"
44 runto [gdb_get_line_number "Break here."]
46 # Test basic gdb.Inferior attributes and methods.
48 gdb_py_test_silent_cmd "python inferiors = gdb.inferiors ()" "get inferiors list" 1
49 gdb_test "python print inferiors" "\\(<gdb.Inferior object at 0x\[\[:xdigit:\]\]+>,\\)" "verify inferiors list"
50 gdb_py_test_silent_cmd "python i0 = inferiors\[0\]" "get first inferior" 0
52 gdb_test "python print 'result =', i0 == inferiors\[0\]" " = True" "test equality comparison (true)"
53 gdb_test "python print 'result =', i0.num" " = \[0-9\]+" "test Inferior.num"
54 gdb_test "python print 'result =', i0.pid" " = \[0-9\]+" "test Inferior.pid"
55 gdb_test "python print 'result =', i0.was_attached" " = False" "test Inferior.was_attached"
56 gdb_test "python print i0.threads ()" "\\(<gdb.InferiorThread object at 0x\[\[:xdigit:\]\]+>,\\)" "test Inferior.threads"
58 # Test memory read and write operations.
60 gdb_py_test_silent_cmd "python addr = gdb.selected_frame ().read_var ('str')" \
62 gdb_py_test_silent_cmd "python str = gdb.inferiors()\[0\].read_memory (addr, 5)" \
64 gdb_py_test_silent_cmd "python str\[1\] = 'a'" "change str" 0
65 gdb_py_test_silent_cmd "python gdb.inferiors()\[0\].write_memory (addr, str)" \
67 gdb_test "print str" " = 0x\[\[:xdigit:\]\]+ \"hallo, testsuite\"" \
68 "ensure str was changed in the inferior"
72 set hex_number {0x[0-9a-fA-F][0-9a-fA-F]*}
73 set dec_number {[0-9]+}
74 set history_prefix {[$][0-9]* = }
76 set pattern_not_found "${newline}.None"
77 set one_pattern_found "${newline}.${dec_number}"
79 # Test string pattern.
81 gdb_test "set *(int32_t*) &int8_search_buf\[10\] = 0x61616161" "" ""
82 gdb_test "py search_buf = gdb.selected_frame ().read_var ('int8_search_buf')" "" ""
83 gdb_test "py start_addr = search_buf.address" "" ""
84 gdb_test "py length = search_buf.type.sizeof" "" ""
86 gdb_test "py print gdb.inferiors()\[0\].search_memory (start_addr, length, 'aaa')" \
87 "${one_pattern_found}" "find string pattern"
89 # Test not finding pattern because search range too small, with
90 # potential find at the edge of the range.
92 gdb_test "py print gdb.inferiors()\[0\].search_memory (start_addr, 10+3, 'aaaa')" \
93 "${pattern_not_found}" "pattern not found at end of range"
95 # Increase the search range by 1 and we should find the pattern.
97 gdb_test "py print gdb.inferiors()\[0\].search_memory (start_addr, 10+3+1, 'aaa')" \
98 "${one_pattern_found}" "pattern found at end of range"
100 # Import struct to pack the following patterns.
101 gdb_test "py from struct import *" "" ""
103 # Test 16-bit pattern.
105 gdb_test "set int16_search_buf\[10\] = 0x1234" "" ""
106 gdb_test "py search_buf = gdb.selected_frame ().read_var ('int16_search_buf')" "" ""
107 gdb_test "py start_addr = search_buf.address" "" ""
108 gdb_test "py length = search_buf.type.sizeof" "" ""
109 gdb_test "py pattern = pack('H',0x1234)" "" \
111 gdb_test "py print gdb.inferiors()\[0\].search_memory (start_addr, length, pattern)" \
112 "${one_pattern_found}" "find 16-bit pattern, with value pattern"
114 # Test 32-bit pattern.
116 gdb_test "set int32_search_buf\[10\] = 0x12345678" "" ""
117 gdb_test "py search_buf = gdb.selected_frame ().read_var ('int32_search_buf')" "" ""
118 gdb_test "py start_addr = search_buf.address" "" ""
119 gdb_test "py length = search_buf.type.sizeof" "" ""
120 gdb_test "py pattern = pack('I',0x12345678)" "" \
122 gdb_test "py print gdb.inferiors()\[0\].search_memory (start_addr, length, pattern)" \
123 "${one_pattern_found}" "find 32-bit pattern, with python pattern"
125 # Test 64-bit pattern.
127 gdb_test "set int64_search_buf\[10\] = 0xfedcba9876543210LL" "" ""
128 gdb_test "py search_buf = gdb.selected_frame ().read_var ('int64_search_buf')" "" ""
129 gdb_test "py start_addr = search_buf.address" "" ""
130 gdb_test "py length = search_buf.type.sizeof" "" ""
131 gdb_test "py pattern = pack('Q', 0xfedcba9876543210)" "" ""
133 gdb_test "py print gdb.inferiors()\[0\].search_memory (start_addr, length, pattern)" \
134 "${one_pattern_found}" "find 64-bit pattern, with value pattern"
136 # Test mixed-sized patterns.
138 gdb_test "set *(int8_t*) &search_buf\[10\] = 0x62" "" ""
139 gdb_test "set *(int16_t*) &search_buf\[11\] = 0x6363" "" ""
140 gdb_test "set *(int32_t*) &search_buf\[13\] = 0x64646464" "" ""
141 gdb_test "py search_buf = gdb.selected_frame ().read_var ('search_buf')" "" ""
142 gdb_test "py start_addr = search_buf\[0\].address" "" ""
143 gdb_test "py pattern1 = pack('B', 0x62)" "" ""
144 gdb_test "py pattern2 = pack('H', 0x6363)" "" ""
145 gdb_test "py pattern3 = pack('I', 0x64646464)" "" ""
147 gdb_test "py print gdb.inferiors()\[0\].search_memory (start_addr, 100, pattern1)" \
148 "${one_pattern_found}" "find mixed-sized pattern"
149 gdb_test "py print gdb.inferiors()\[0\].search_memory (start_addr, 100, pattern2)" \
150 "${one_pattern_found}" "find mixed-sized pattern"
151 gdb_test "py print gdb.inferiors()\[0\].search_memory (start_addr, 100, pattern3)" \
152 "${one_pattern_found}" "find mixed-sized pattern"
154 # Test search spanning a large range, in the particular case of native
155 # targets, test the search spanning multiple chunks.
156 # Remote targets may implement the search differently.
158 set CHUNK_SIZE 16000 ;
160 gdb_test "set *(int32_t*) &search_buf\[0*${CHUNK_SIZE}+100\] = 0x12345678" "" ""
161 gdb_test "set *(int32_t*) &search_buf\[1*${CHUNK_SIZE}+100\] = 0x12345678" "" ""
162 gdb_test "py start_addr = gdb.selected_frame ().read_var ('search_buf')" "" ""
163 gdb_test "py length = gdb.selected_frame ().read_var ('search_buf_size')" "" ""
164 gdb_test "py pattern = pack('I', 0x12345678)" "" ""
165 gdb_test "py first = gdb.inferiors()\[0\].search_memory (start_addr,length, pattern)" "" ""
166 gdb_test "py print first" "${one_pattern_found}" "search spanning large range 1st result"
167 gdb_test "py start_addr = first + 1"
168 gdb_test "py second = gdb.inferiors()\[0\].search_memory (start_addr, length, pattern)" "" ""
169 gdb_test "py print second" "${one_pattern_found}" "search spanning large range 2nd result"
170 gdb_test "py start_addr = second + 1"
171 gdb_test "py third = gdb.inferiors()\[0\].search_memory (start_addr, length, pattern)" "" ""
172 gdb_test "py print third" "${pattern_not_found}" "search spanning large range 3rd result"
174 # For native targets, test a pattern straddling a chunk boundary.
177 gdb_test "set *(int32_t*) &search_buf\[${CHUNK_SIZE}-1\] = 0xfdb97531" "" ""
178 gdb_test "py pattern = pack('I', 0xfdb97531)" "" ""
179 gdb_test "py start_addr = gdb.selected_frame ().read_var ('search_buf')" "" ""
180 gdb_test "py print gdb.inferiors()\[0\].search_memory (start_addr, length, pattern)" \
181 "${one_pattern_found}" "find pattern straddling chunk boundary"