Fix test for sections with different VMA<->LMA relationships so that it only applies...
[binutils-gdb.git] / gdb / findcmd.c
blob2915c706733493c47d1b9fc85f9913c262c170ca
1 /* The find command.
3 Copyright (C) 2008-2024 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #include "arch-utils.h"
21 #include <ctype.h>
22 #include "gdbcmd.h"
23 #include "value.h"
24 #include "target.h"
25 #include "cli/cli-utils.h"
26 #include <algorithm>
27 #include "gdbsupport/byte-vector.h"
29 /* Copied from bfd_put_bits. */
31 static void
32 put_bits (uint64_t data, gdb::byte_vector &buf, int bits, bfd_boolean big_p)
34 int i;
35 int bytes;
37 gdb_assert (bits % 8 == 0);
39 bytes = bits / 8;
40 size_t last = buf.size ();
41 buf.resize (last + bytes);
42 for (i = 0; i < bytes; i++)
44 int index = big_p ? bytes - i - 1 : i;
46 buf[last + index] = data & 0xff;
47 data >>= 8;
51 /* Subroutine of find_command to simplify it.
52 Parse the arguments of the "find" command. */
54 static gdb::byte_vector
55 parse_find_args (const char *args, ULONGEST *max_countp,
56 CORE_ADDR *start_addrp, ULONGEST *search_space_lenp,
57 bfd_boolean big_p)
59 /* Default to using the specified type. */
60 char size = '\0';
61 ULONGEST max_count = ~(ULONGEST) 0;
62 /* Buffer to hold the search pattern. */
63 gdb::byte_vector pattern_buf;
64 CORE_ADDR start_addr;
65 ULONGEST search_space_len;
66 const char *s = args;
67 struct value *v;
69 if (args == NULL)
70 error (_("Missing search parameters."));
72 /* Get search granularity and/or max count if specified.
73 They may be specified in either order, together or separately. */
75 while (*s == '/')
77 ++s;
79 while (*s != '\0' && *s != '/' && !isspace (*s))
81 if (isdigit (*s))
83 max_count = atoi (s);
84 while (isdigit (*s))
85 ++s;
86 continue;
89 switch (*s)
91 case 'b':
92 case 'h':
93 case 'w':
94 case 'g':
95 size = *s++;
96 break;
97 default:
98 error (_("Invalid size granularity."));
102 s = skip_spaces (s);
105 /* Get the search range. */
107 v = parse_to_comma_and_eval (&s);
108 start_addr = value_as_address (v);
110 if (*s == ',')
111 ++s;
112 s = skip_spaces (s);
114 if (*s == '+')
116 LONGEST len;
118 ++s;
119 v = parse_to_comma_and_eval (&s);
120 len = value_as_long (v);
121 if (len == 0)
123 gdb_printf (_("Empty search range.\n"));
124 return pattern_buf;
126 if (len < 0)
127 error (_("Invalid length."));
128 /* Watch for overflows. */
129 if (len > CORE_ADDR_MAX
130 || (start_addr + len - 1) < start_addr)
131 error (_("Search space too large."));
132 search_space_len = len;
134 else
136 CORE_ADDR end_addr;
138 v = parse_to_comma_and_eval (&s);
139 end_addr = value_as_address (v);
140 if (start_addr > end_addr)
141 error (_("Invalid search space, end precedes start."));
142 search_space_len = end_addr - start_addr + 1;
143 /* We don't support searching all of memory
144 (i.e. start=0, end = 0xff..ff).
145 Bail to avoid overflows later on. */
146 if (search_space_len == 0)
147 error (_("Overflow in address range "
148 "computation, choose smaller range."));
151 if (*s == ',')
152 ++s;
154 /* Fetch the search string. */
156 while (*s != '\0')
158 LONGEST x;
159 struct type *t;
161 s = skip_spaces (s);
163 v = parse_to_comma_and_eval (&s);
164 t = v->type ();
166 if (size != '\0')
168 x = value_as_long (v);
169 switch (size)
171 case 'b':
172 pattern_buf.push_back (x);
173 break;
174 case 'h':
175 put_bits (x, pattern_buf, 16, big_p);
176 break;
177 case 'w':
178 put_bits (x, pattern_buf, 32, big_p);
179 break;
180 case 'g':
181 put_bits (x, pattern_buf, 64, big_p);
182 break;
185 else
187 const gdb_byte *contents = v->contents ().data ();
188 pattern_buf.insert (pattern_buf.end (), contents,
189 contents + t->length ());
192 if (*s == ',')
193 ++s;
194 s = skip_spaces (s);
197 if (pattern_buf.empty ())
198 error (_("Missing search pattern."));
200 if (search_space_len < pattern_buf.size ())
201 error (_("Search space too small to contain pattern."));
203 *max_countp = max_count;
204 *start_addrp = start_addr;
205 *search_space_lenp = search_space_len;
207 return pattern_buf;
210 static void
211 find_command (const char *args, int from_tty)
213 struct gdbarch *gdbarch = get_current_arch ();
214 bfd_boolean big_p = gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG;
215 /* Command line parameters.
216 These are initialized to avoid uninitialized warnings from -Wall. */
217 ULONGEST max_count = 0;
218 CORE_ADDR start_addr = 0;
219 ULONGEST search_space_len = 0;
220 /* End of command line parameters. */
221 unsigned int found_count;
222 CORE_ADDR last_found_addr;
224 gdb::byte_vector pattern_buf = parse_find_args (args, &max_count,
225 &start_addr,
226 &search_space_len,
227 big_p);
229 /* Perform the search. */
231 found_count = 0;
232 last_found_addr = 0;
234 while (search_space_len >= pattern_buf.size ()
235 && found_count < max_count)
237 /* Offset from start of this iteration to the next iteration. */
238 ULONGEST next_iter_incr;
239 CORE_ADDR found_addr;
240 int found = target_search_memory (start_addr, search_space_len,
241 pattern_buf.data (),
242 pattern_buf.size (),
243 &found_addr);
245 if (found <= 0)
246 break;
248 print_address (gdbarch, found_addr, gdb_stdout);
249 gdb_printf ("\n");
250 ++found_count;
251 last_found_addr = found_addr;
253 /* Begin next iteration at one byte past this match. */
254 next_iter_incr = (found_addr - start_addr) + 1;
256 /* For robustness, we don't let search_space_len go -ve here. */
257 if (search_space_len >= next_iter_incr)
258 search_space_len -= next_iter_incr;
259 else
260 search_space_len = 0;
261 start_addr += next_iter_incr;
264 /* Record and print the results. */
266 set_internalvar_integer (lookup_internalvar ("numfound"), found_count);
267 if (found_count > 0)
269 struct type *ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
271 set_internalvar (lookup_internalvar ("_"),
272 value_from_pointer (ptr_type, last_found_addr));
275 if (found_count == 0)
276 gdb_printf ("Pattern not found.\n");
277 else
278 gdb_printf ("%d pattern%s found.\n", found_count,
279 found_count > 1 ? "s" : "");
282 void _initialize_mem_search ();
283 void
284 _initialize_mem_search ()
286 add_cmd ("find", class_vars, find_command, _("\
287 Search memory for a sequence of bytes.\n\
288 Usage:\nfind \
289 [/SIZE-CHAR] [/MAX-COUNT] START-ADDRESS, END-ADDRESS, EXPR1 [, EXPR2 ...]\n\
290 find [/SIZE-CHAR] [/MAX-COUNT] START-ADDRESS, +LENGTH, EXPR1 [, EXPR2 ...]\n\
291 SIZE-CHAR is one of b,h,w,g for 8,16,32,64 bit values respectively,\n\
292 and if not specified the size is taken from the type of the expression\n\
293 in the current language.\n\
294 The two-address form specifies an inclusive range.\n\
295 Note that this means for example that in the case of C-like languages\n\
296 a search for an untyped 0x42 will search for \"(int) 0x42\"\n\
297 which is typically four bytes, and a search for a string \"hello\" will\n\
298 include the trailing '\\0'. The null terminator can be removed from\n\
299 searching by using casts, e.g.: {char[5]}\"hello\".\n\
301 The address of the last match is stored as the value of \"$_\".\n\
302 Convenience variable \"$numfound\" is set to the number of matches."),
303 &cmdlist);