1# Copyright 2008, 2009, 2010, 2011 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 tests the find command.
17
18if $tracelevel then {
19    strace $tracelevel
20}
21
22set testfile "find"
23set srcfile ${testfile}.c
24set binfile ${objdir}/${subdir}/${testfile}
25
26if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug nowarnings}] != "" } {
27    untested find.exp
28    return -1
29}
30
31gdb_exit
32gdb_start
33gdb_reinitialize_dir $srcdir/$subdir
34gdb_load ${binfile}
35
36gdb_test "break $srcfile:stop_here" \
37    "Breakpoint.*at.* file .*$srcfile, line.*" \
38    "breakpoint function in file"
39
40gdb_run_cmd
41gdb_expect {
42    -re "Breakpoint \[0-9\]+,.*stop_here.* at .*$srcfile:.*$gdb_prompt $" {
43	pass "run until function breakpoint"
44    }
45    -re "$gdb_prompt $" {
46	fail "run until function breakpoint"
47    }
48    timeout {
49	fail "run until function breakpoint (timeout)"
50    }
51}
52
53# We've now got the target program in a state where we can test "find".
54
55set hex_number {0x[0-9a-fA-F][0-9a-fA-F]*}
56set history_prefix {[$][0-9]* = }
57set newline "\[\r\n\]*"
58set pattern_not_found "${newline}Pattern not found\[.\]"
59set one_pattern_found "${newline}1 pattern found\[.\]"
60set two_patterns_found "${newline}2 patterns found\[.\]"
61
62# Test string pattern.
63
64gdb_test_no_output "set *(int32_t*) &int8_search_buf\[10\] = 0x61616161" ""
65
66gdb_test "find &int8_search_buf\[0\], +sizeof(int8_search_buf), 'a', 'a', 'a'" \
67    "${hex_number}.*<int8_search_buf\\+10>${newline}${hex_number}.*<int8_search_buf\\+11>${two_patterns_found}" \
68    "find string pattern"
69
70# Test not finding pattern because search range too small, with
71# potential find at the edge of the range.
72
73gdb_test "find &int8_search_buf\[0\], +10+3, \"aaaa\"" \
74    "${pattern_not_found}" \
75    "pattern not found at end of range"
76
77# Increase the search range by 1 and we should find the pattern.
78
79gdb_test "find &int8_search_buf\[0\], +10+3+1, 'a', 'a', 'a', 'a'" \
80    "${hex_number}.*<int8_search_buf\\+10>${one_pattern_found}" \
81    "pattern found at end of range"
82
83# Test max-count, $_ and $numfound.
84
85gdb_test "find /1 &int8_search_buf\[0\], +sizeof(int8_search_buf), 'a', 'a', 'a'" \
86    "${hex_number}.*<int8_search_buf\\+10>${one_pattern_found}" \
87    "max-count"
88
89gdb_test "print \$_" \
90    "${history_prefix}.*${hex_number}" \
91    "\$_"
92
93gdb_test "print \$numfound" \
94    "${history_prefix}1" \
95    "\$numfound"
96
97# Test max-count with size-char.
98# They can be specified in either order.
99
100gdb_test "find /1b &int8_search_buf\[0\], +sizeof(int8_search_buf), 0x61, 0x61, 0x61" \
101    "${hex_number}.*<int8_search_buf\\+10>${one_pattern_found}" \
102    "size,max-count, /1b"
103
104gdb_test "find /b1 &int8_search_buf\[0\], +sizeof(int8_search_buf), 0x61, 0x61, 0x61" \
105    "${hex_number}.*<int8_search_buf\\+10>${one_pattern_found}" \
106    "size,max-count, /b1"
107
108gdb_test "find /b /1 &int8_search_buf\[0\], +sizeof(int8_search_buf), 0x61, 0x61, 0x61" \
109    "${hex_number}.*<int8_search_buf\\+10>${one_pattern_found}" \
110    "size,max-count, /b/1"
111
112gdb_test "find /1 /b &int8_search_buf\[0\], +sizeof(int8_search_buf), 0x61, 0x61, 0x61" \
113    "${hex_number}.*<int8_search_buf\\+10>${one_pattern_found}" \
114    "size,max-count, /1/b"
115
116# Test specifying end address.
117
118gdb_test "find /b &int8_search_buf\[0\], &int8_search_buf\[0\]+sizeof(int8_search_buf), 0x61, 0x61, 0x61, 0x61" \
119    "${hex_number}.*<int8_search_buf\\+10>${one_pattern_found}" \
120    "find byte pattern with end address"
121
122# Test 16-bit pattern.
123
124gdb_test_no_output "set int16_search_buf\[10\] = 0x1234" ""
125
126gdb_test "find /h &int16_search_buf\[0\], +sizeof(int16_search_buf), 0x1234" \
127    "${hex_number}.*<int16_search_buf\\+20>${one_pattern_found}" \
128    "find 16-bit pattern"
129
130gdb_test "find &int16_search_buf\[0\], +sizeof(int16_search_buf), (int16_t) 0x1234" \
131    "${hex_number}.*<int16_search_buf\\+20>${one_pattern_found}" \
132    "find 16-bit pattern"
133
134# Test 32-bit pattern.
135
136gdb_test_no_output "set int32_search_buf\[10\] = 0x12345678" ""
137
138gdb_test "find &int32_search_buf\[0\], +sizeof(int32_search_buf), (int32_t) 0x12345678" \
139    "${hex_number}.*<int32_search_buf\\+40>${one_pattern_found}" \
140    "find 32-bit pattern"
141
142gdb_test "find /w &int32_search_buf\[0\], +sizeof(int32_search_buf), 0x12345678" \
143    "${hex_number}.*<int32_search_buf\\+40>${one_pattern_found}" \
144    "find 32-bit pattern"
145
146# Test 64-bit pattern.
147
148gdb_test_no_output "set int64_search_buf\[10\] = 0xfedcba9876543210LL" ""
149
150gdb_test "find &int64_search_buf\[0\], +sizeof(int64_search_buf), (int64_t) 0xfedcba9876543210LL" \
151    "${hex_number}.*<int64_search_buf\\+80>${one_pattern_found}" \
152    "find 64-bit pattern"
153
154gdb_test "find /g &int64_search_buf\[0\], +sizeof(int64_search_buf), 0xfedcba9876543210LL" \
155    "${hex_number}.*<int64_search_buf\\+80>${one_pattern_found}" \
156    "find 64-bit pattern"
157
158# Test mixed-sized patterns.
159
160gdb_test_no_output "set *(int8_t*) &search_buf\[10\] = 0x62" ""
161gdb_test_no_output "set *(int16_t*) &search_buf\[11\] = 0x6363" ""
162gdb_test_no_output "set *(int32_t*) &search_buf\[13\] = 0x64646464" ""
163
164gdb_test "find &search_buf\[0\], +100, (int8_t) 0x62, (int16_t) 0x6363, (int32_t) 0x64646464" \
165    "${hex_number}${one_pattern_found}" \
166    "find mixed-sized pattern"
167
168# Test search spanning a large range, in the particular case of native
169# targets, test the search spanning multiple chunks.
170# Remote targets may implement the search differently.
171
172set CHUNK_SIZE 16000 ;# see findcmd.c
173
174gdb_test_no_output "set *(int32_t*) &search_buf\[0*${CHUNK_SIZE}+100\] = 0x12345678" ""
175gdb_test_no_output "set *(int32_t*) &search_buf\[1*${CHUNK_SIZE}+100\] = 0x12345678" ""
176
177gdb_test "find /w search_buf, +search_buf_size, 0x12345678" \
178    "${hex_number}${newline}${hex_number}${two_patterns_found}" \
179    "search spanning large range"
180
181# For native targets, test a pattern straddling a chunk boundary.
182
183if [isnative] {
184    gdb_test_no_output "set *(int32_t*) &search_buf\[${CHUNK_SIZE}-1\] = 0xfdb97531" ""
185    gdb_test "find /w search_buf, +search_buf_size, 0xfdb97531" \
186    "${hex_number}${one_pattern_found}" \
187    "find pattern straddling chunk boundary"
188}
189