1 # Copyright 2008-2013 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 tests the find command.
20 if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug nowarnings}] != "" } {
25 clean_restart ${binfile}
27 gdb_test "break $srcfile:stop_here" \
28 "Breakpoint.*at.* file .*$srcfile, line.*" \
29 "breakpoint function in file"
33 -re "Breakpoint \[0-9\]+,.*stop_here.* at .*$srcfile:.*$gdb_prompt $" {
34 pass "run until function breakpoint"
37 fail "run until function breakpoint"
40 fail "run until function breakpoint (timeout)"
44 # We've now got the target program in a state where we can test "find".
46 set hex_number {0x[0-9a-fA-F][0-9a-fA-F]*}
47 set history_prefix {[$][0-9]* = }
48 set newline "\[\r\n\]*"
49 set pattern_not_found "${newline}Pattern not found\[.\]"
50 set one_pattern_found "${newline}1 pattern found\[.\]"
51 set two_patterns_found "${newline}2 patterns found\[.\]"
53 # Test string pattern.
55 gdb_test_no_output "set *(int32_t*) &int8_search_buf\[10\] = 0x61616161" ""
57 gdb_test "find &int8_search_buf\[0\], +sizeof(int8_search_buf), 'a', 'a', 'a'" \
58 "${hex_number}.*<int8_search_buf\\+10>${newline}${hex_number}.*<int8_search_buf\\+11>${two_patterns_found}" \
61 # Test not finding pattern because search range too small, with
62 # potential find at the edge of the range.
64 gdb_test "find &int8_search_buf\[0\], +10+3, \"aaaa\"" \
65 "${pattern_not_found}" \
66 "pattern not found at end of range"
68 # Increase the search range by 1 and we should find the pattern.
70 gdb_test "find &int8_search_buf\[0\], +10+3+1, 'a', 'a', 'a', 'a'" \
71 "${hex_number}.*<int8_search_buf\\+10>${one_pattern_found}" \
72 "pattern found at end of range"
74 # Test max-count, $_ and $numfound.
76 gdb_test "find /1 &int8_search_buf\[0\], +sizeof(int8_search_buf), 'a', 'a', 'a'" \
77 "${hex_number}.*<int8_search_buf\\+10>${one_pattern_found}" \
80 gdb_test "print \$_" \
81 "${history_prefix}.*${hex_number} <int8_search_buf\\+10>" \
84 gdb_test "print \$numfound" \
85 "${history_prefix}1" \
88 # Test max-count with size-char.
89 # They can be specified in either order.
91 gdb_test "find /1b &int8_search_buf\[0\], +sizeof(int8_search_buf), 0x61, 0x61, 0x61" \
92 "${hex_number}.*<int8_search_buf\\+10>${one_pattern_found}" \
95 gdb_test "find /b1 &int8_search_buf\[0\], +sizeof(int8_search_buf), 0x61, 0x61, 0x61" \
96 "${hex_number}.*<int8_search_buf\\+10>${one_pattern_found}" \
99 gdb_test "find /b /1 &int8_search_buf\[0\], +sizeof(int8_search_buf), 0x61, 0x61, 0x61" \
100 "${hex_number}.*<int8_search_buf\\+10>${one_pattern_found}" \
101 "size,max-count, /b/1"
103 gdb_test "find /1 /b &int8_search_buf\[0\], +sizeof(int8_search_buf), 0x61, 0x61, 0x61" \
104 "${hex_number}.*<int8_search_buf\\+10>${one_pattern_found}" \
105 "size,max-count, /1/b"
107 # Test specifying end address.
109 gdb_test "find /b &int8_search_buf\[0\], &int8_search_buf\[0\]+sizeof(int8_search_buf), 0x61, 0x61, 0x61, 0x61" \
110 "${hex_number}.*<int8_search_buf\\+10>${one_pattern_found}" \
111 "find byte pattern with end address"
113 # Test 16-bit pattern.
115 gdb_test_no_output "set int16_search_buf\[10\] = 0x1234" ""
117 gdb_test "find /h &int16_search_buf\[0\], +sizeof(int16_search_buf), 0x1234" \
118 "${hex_number}.*<int16_search_buf\\+20>${one_pattern_found}" \
119 "find 16-bit pattern"
121 gdb_test "find &int16_search_buf\[0\], +sizeof(int16_search_buf), (int16_t) 0x1234" \
122 "${hex_number}.*<int16_search_buf\\+20>${one_pattern_found}" \
123 "find 16-bit pattern"
125 # Test 32-bit pattern.
127 gdb_test_no_output "set int32_search_buf\[10\] = 0x12345678" ""
129 gdb_test "find &int32_search_buf\[0\], +sizeof(int32_search_buf), (int32_t) 0x12345678" \
130 "${hex_number}.*<int32_search_buf\\+40>${one_pattern_found}" \
131 "find 32-bit pattern"
133 gdb_test "find /w &int32_search_buf\[0\], +sizeof(int32_search_buf), 0x12345678" \
134 "${hex_number}.*<int32_search_buf\\+40>${one_pattern_found}" \
135 "find 32-bit pattern"
137 # Test 64-bit pattern.
139 gdb_test_no_output "set int64_search_buf\[10\] = 0xfedcba9876543210LL" ""
141 gdb_test "find &int64_search_buf\[0\], +sizeof(int64_search_buf), (int64_t) 0xfedcba9876543210LL" \
142 "${hex_number}.*<int64_search_buf\\+80>${one_pattern_found}" \
143 "find 64-bit pattern"
145 gdb_test "find /g &int64_search_buf\[0\], +sizeof(int64_search_buf), 0xfedcba9876543210LL" \
146 "${hex_number}.*<int64_search_buf\\+80>${one_pattern_found}" \
147 "find 64-bit pattern"
149 # Test mixed-sized patterns.
151 gdb_test_no_output "set *(int8_t*) &search_buf\[10\] = 0x62" ""
152 gdb_test_no_output "set *(int16_t*) &search_buf\[11\] = 0x6363" ""
153 gdb_test_no_output "set *(int32_t*) &search_buf\[13\] = 0x64646464" ""
155 gdb_test "find &search_buf\[0\], +100, (int8_t) 0x62, (int16_t) 0x6363, (int32_t) 0x64646464" \
156 "${hex_number}${one_pattern_found}" \
157 "find mixed-sized pattern"
159 # Test search spanning a large range, in the particular case of native
160 # targets, test the search spanning multiple chunks.
161 # Remote targets may implement the search differently.
163 set CHUNK_SIZE 16000 ;# see findcmd.c
165 gdb_test_no_output "set *(int32_t*) &search_buf\[0*${CHUNK_SIZE}+100\] = 0x12345678" ""
166 gdb_test_no_output "set *(int32_t*) &search_buf\[1*${CHUNK_SIZE}+100\] = 0x12345678" ""
168 gdb_test "find /w search_buf, +search_buf_size, 0x12345678" \
169 "${hex_number}${newline}${hex_number}${two_patterns_found}" \
170 "search spanning large range"
172 # For native targets, test a pattern straddling a chunk boundary.
175 gdb_test_no_output "set *(int32_t*) &search_buf\[${CHUNK_SIZE}-1\] = 0xfdb97531" ""
176 gdb_test "find /w search_buf, +search_buf_size, 0xfdb97531" \
177 "${hex_number}${one_pattern_found}" \
178 "find pattern straddling chunk boundary"
181 # Check GDB buffer overflow.
182 gdb_test "find int64_search_buf, +64/8*100, int64_search_buf" " <int64_search_buf>\r\n1 pattern found\\."