1 # Copyright 2008-2024 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}] != "" } {
21 untested "failed to compile"
25 clean_restart ${binfile}
27 gdb_test "break $srcfile:stop_here" \
28 "Breakpoint.*at.* file .*$srcfile, line.*" \
29 "breakpoint function in file"
32 gdb_test "" "Breakpoint \[0-9\]+,.*stop_here.* at .*$srcfile:.*" "run until function breakpoint"
34 # We've now got the target program in a state where we can test "find".
36 set hex_number {0x[0-9a-fA-F][0-9a-fA-F]*}
37 set history_prefix {[$][0-9]* = }
38 set newline "\[\r\n\]*"
39 set pattern_not_found "${newline}Pattern not found\[.\]"
40 set one_pattern_found "${newline}1 pattern found\[.\]"
41 set two_patterns_found "${newline}2 patterns found\[.\]"
43 # Test string pattern.
45 # For when the testcase is built in C++ mode.
46 gdb_test_no_output "set print asm-demangle on"
48 gdb_test_no_output "set *(int32_t*) &int8_search_buf\[10\] = 0x61616161" ""
50 gdb_test "find &int8_search_buf\[0\], +sizeof(int8_search_buf), 'a', 'a', 'a'" \
51 "${hex_number}.*<int8_search_buf\\+10>${newline}${hex_number}.*<int8_search_buf\\+11>${two_patterns_found}" \
54 # Test not finding pattern because search range too small, with
55 # potential find at the edge of the range.
57 gdb_test "find &int8_search_buf\[0\], +10+3, \"aaaa\"" \
58 "${pattern_not_found}" \
59 "pattern not found at end of range"
61 # Increase the search range by 1 and we should find the pattern.
63 gdb_test "find &int8_search_buf\[0\], +10+3+1, 'a', 'a', 'a', 'a'" \
64 "${hex_number}.*<int8_search_buf\\+10>${one_pattern_found}" \
65 "pattern found at end of range"
67 # Test max-count, $_ and $numfound.
69 gdb_test "find /1 &int8_search_buf\[0\], +sizeof(int8_search_buf), 'a', 'a', 'a'" \
70 "${hex_number}.*<int8_search_buf\\+10>${one_pattern_found}" \
73 gdb_test "print \$_" \
74 "${history_prefix}.*${hex_number} <int8_search_buf\\+10>" \
77 gdb_test "print \$numfound" \
78 "${history_prefix}1" \
81 # Test max-count with size-char.
82 # They can be specified in either order.
84 gdb_test "find /1b &int8_search_buf\[0\], +sizeof(int8_search_buf), 0x61, 0x61, 0x61" \
85 "${hex_number}.*<int8_search_buf\\+10>${one_pattern_found}" \
88 gdb_test "find /b1 &int8_search_buf\[0\], +sizeof(int8_search_buf), 0x61, 0x61, 0x61" \
89 "${hex_number}.*<int8_search_buf\\+10>${one_pattern_found}" \
92 gdb_test "find /b /1 &int8_search_buf\[0\], +sizeof(int8_search_buf), 0x61, 0x61, 0x61" \
93 "${hex_number}.*<int8_search_buf\\+10>${one_pattern_found}" \
94 "size,max-count, /b/1"
96 gdb_test "find /1 /b &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, /1/b"
100 # Test specifying end address.
102 gdb_test "find /b &int8_search_buf\[0\], &int8_search_buf\[0\]+sizeof(int8_search_buf), 0x61, 0x61, 0x61, 0x61" \
103 "${hex_number}.*<int8_search_buf\\+10>${one_pattern_found}" \
104 "find byte pattern with end address"
106 # Test 16-bit pattern.
108 gdb_test_no_output "set int16_search_buf\[10\] = 0x1234" ""
110 gdb_test "find /h &int16_search_buf\[0\], +sizeof(int16_search_buf), 0x1234" \
111 "${hex_number}.*<int16_search_buf\\+20>${one_pattern_found}" \
112 "find 16-bit pattern /h"
114 gdb_test "find &int16_search_buf\[0\], +sizeof(int16_search_buf), (int16_t) 0x1234" \
115 "${hex_number}.*<int16_search_buf\\+20>${one_pattern_found}" \
116 "find 16-bit pattern cast"
118 # Test 32-bit pattern.
120 gdb_test_no_output "set int32_search_buf\[10\] = 0x12345678" ""
122 gdb_test "find &int32_search_buf\[0\], +sizeof(int32_search_buf), (int32_t) 0x12345678" \
123 "${hex_number}.*<int32_search_buf\\+40>${one_pattern_found}" \
124 "find 32-bit pattern cast"
126 gdb_test "find /w &int32_search_buf\[0\], +sizeof(int32_search_buf), 0x12345678" \
127 "${hex_number}.*<int32_search_buf\\+40>${one_pattern_found}" \
128 "find 32-bit pattern /w"
130 # Test 64-bit pattern.
132 gdb_test_no_output "set int64_search_buf\[10\] = 0xfedcba9876543210LL" ""
134 gdb_test "find &int64_search_buf\[0\], +sizeof(int64_search_buf), (int64_t) 0xfedcba9876543210LL" \
135 "${hex_number}.*<int64_search_buf\\+80>${one_pattern_found}" \
136 "find 64-bit pattern cast"
138 gdb_test "find /g &int64_search_buf\[0\], +sizeof(int64_search_buf), 0xfedcba9876543210LL" \
139 "${hex_number}.*<int64_search_buf\\+80>${one_pattern_found}" \
140 "find 64-bit pattern /g"
142 # Test mixed-sized patterns.
144 gdb_test_no_output "set *(int8_t*) &search_buf\[10\] = 0x62" ""
145 gdb_test_no_output "set *(int16_t*) &search_buf\[11\] = 0x6363" ""
146 gdb_test_no_output "set *(int32_t*) &search_buf\[13\] = 0x64646464" ""
148 gdb_test "find &search_buf\[0\], +100, (int8_t) 0x62, (int16_t) 0x6363, (int32_t) 0x64646464" \
149 "${hex_number}${one_pattern_found}" \
150 "find mixed-sized pattern"
152 # Test search spanning a large range, in the particular case of native
153 # targets, test the search spanning multiple chunks.
154 # Remote targets may implement the search differently.
156 set CHUNK_SIZE 16000 ;# see findcmd.c
158 gdb_test_no_output "set *(int32_t*) &search_buf\[0*${CHUNK_SIZE}+100\] = 0x12345678" ""
159 gdb_test_no_output "set *(int32_t*) &search_buf\[1*${CHUNK_SIZE}+100\] = 0x12345678" ""
161 gdb_test "find /w search_buf, +search_buf_size, 0x12345678" \
162 "${hex_number}${newline}${hex_number}${two_patterns_found}" \
163 "search spanning large range"
165 # For native targets, test a pattern straddling a chunk boundary.
168 gdb_test_no_output "set *(int32_t*) &search_buf\[${CHUNK_SIZE}-1\] = 0xfdb97531" ""
169 gdb_test "find /w search_buf, +search_buf_size, 0xfdb97531" \
170 "${hex_number}${one_pattern_found}" \
171 "find pattern straddling chunk boundary"
174 # Check GDB buffer overflow.
175 gdb_test "find int64_search_buf, +64/8*100, int64_search_buf" " <int64_search_buf>\r\n1 pattern found\\."