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