3 Copyright (C) 2008-2017 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/>. */
21 #include "arch-utils.h"
26 #include "cli/cli-utils.h"
29 /* Copied from bfd_put_bits. */
32 put_bits (bfd_uint64_t data, gdb_byte *buf, int bits, bfd_boolean big_p)
37 gdb_assert (bits % 8 == 0);
40 for (i = 0; i < bytes; i++)
42 int index = big_p ? bytes - i - 1 : i;
44 buf[index] = data & 0xff;
49 /* Subroutine of find_command to simplify it.
50 Parse the arguments of the "find" command. */
53 parse_find_args (char *args, ULONGEST *max_countp,
54 gdb_byte **pattern_bufp, ULONGEST *pattern_lenp,
55 CORE_ADDR *start_addrp, ULONGEST *search_space_lenp,
58 /* Default to using the specified type. */
60 ULONGEST max_count = ~(ULONGEST) 0;
61 /* Buffer to hold the search pattern. */
62 gdb_byte *pattern_buf;
63 /* Current size of search pattern buffer.
64 We realloc space as needed. */
65 #define INITIAL_PATTERN_BUF_SIZE 100
66 ULONGEST pattern_buf_size = INITIAL_PATTERN_BUF_SIZE;
67 /* Pointer to one past the last in-use part of pattern_buf. */
68 gdb_byte *pattern_buf_end;
71 ULONGEST search_space_len;
73 struct cleanup *old_cleanups;
77 error (_("Missing search parameters."));
79 pattern_buf = (gdb_byte *) xmalloc (pattern_buf_size);
80 pattern_buf_end = pattern_buf;
81 old_cleanups = make_cleanup (free_current_contents, &pattern_buf);
83 /* Get search granularity and/or max count if specified.
84 They may be specified in either order, together or separately. */
90 while (*s != '\0' && *s != '/' && !isspace (*s))
109 error (_("Invalid size granularity."));
113 s = skip_spaces_const (s);
116 /* Get the search range. */
118 v = parse_to_comma_and_eval (&s);
119 start_addr = value_as_address (v);
123 s = skip_spaces_const (s);
130 v = parse_to_comma_and_eval (&s);
131 len = value_as_long (v);
134 do_cleanups (old_cleanups);
135 printf_filtered (_("Empty search range.\n"));
139 error (_("Invalid length."));
140 /* Watch for overflows. */
141 if (len > CORE_ADDR_MAX
142 || (start_addr + len - 1) < start_addr)
143 error (_("Search space too large."));
144 search_space_len = len;
150 v = parse_to_comma_and_eval (&s);
151 end_addr = value_as_address (v);
152 if (start_addr > end_addr)
153 error (_("Invalid search space, end precedes start."));
154 search_space_len = end_addr - start_addr + 1;
155 /* We don't support searching all of memory
156 (i.e. start=0, end = 0xff..ff).
157 Bail to avoid overflows later on. */
158 if (search_space_len == 0)
159 error (_("Overflow in address range "
160 "computation, choose smaller range."));
166 /* Fetch the search string. */
172 ULONGEST pattern_buf_size_need;
174 s = skip_spaces_const (s);
176 v = parse_to_comma_and_eval (&s);
179 /* Keep it simple and assume size == 'g' when watching for when we
180 need to grow the pattern buf. */
181 pattern_buf_size_need = (pattern_buf_end - pattern_buf
182 + std::max (TYPE_LENGTH (t),
183 (unsigned) sizeof (int64_t)));
184 if (pattern_buf_size_need > pattern_buf_size)
186 size_t current_offset = pattern_buf_end - pattern_buf;
188 pattern_buf_size = pattern_buf_size_need * 2;
189 pattern_buf = (gdb_byte *) xrealloc (pattern_buf, pattern_buf_size);
190 pattern_buf_end = pattern_buf + current_offset;
195 x = value_as_long (v);
199 *pattern_buf_end++ = x;
202 put_bits (x, pattern_buf_end, 16, big_p);
203 pattern_buf_end += sizeof (int16_t);
206 put_bits (x, pattern_buf_end, 32, big_p);
207 pattern_buf_end += sizeof (int32_t);
210 put_bits (x, pattern_buf_end, 64, big_p);
211 pattern_buf_end += sizeof (int64_t);
217 memcpy (pattern_buf_end, value_contents (v), TYPE_LENGTH (t));
218 pattern_buf_end += TYPE_LENGTH (t);
223 s = skip_spaces_const (s);
226 if (pattern_buf_end == pattern_buf)
227 error (_("Missing search pattern."));
229 pattern_len = pattern_buf_end - pattern_buf;
231 if (search_space_len < pattern_len)
232 error (_("Search space too small to contain pattern."));
234 *max_countp = max_count;
235 *pattern_bufp = pattern_buf;
236 *pattern_lenp = pattern_len;
237 *start_addrp = start_addr;
238 *search_space_lenp = search_space_len;
240 /* We successfully parsed the arguments, leave the freeing of PATTERN_BUF
241 to the caller now. */
242 discard_cleanups (old_cleanups);
246 find_command (char *args, int from_tty)
248 struct gdbarch *gdbarch = get_current_arch ();
249 bfd_boolean big_p = gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG;
250 /* Command line parameters.
251 These are initialized to avoid uninitialized warnings from -Wall. */
252 ULONGEST max_count = 0;
253 gdb_byte *pattern_buf = 0;
254 ULONGEST pattern_len = 0;
255 CORE_ADDR start_addr = 0;
256 ULONGEST search_space_len = 0;
257 /* End of command line parameters. */
258 unsigned int found_count;
259 CORE_ADDR last_found_addr;
260 struct cleanup *old_cleanups;
262 parse_find_args (args, &max_count, &pattern_buf, &pattern_len,
263 &start_addr, &search_space_len, big_p);
265 old_cleanups = make_cleanup (free_current_contents, &pattern_buf);
267 /* Perform the search. */
272 while (search_space_len >= pattern_len
273 && found_count < max_count)
275 /* Offset from start of this iteration to the next iteration. */
276 ULONGEST next_iter_incr;
277 CORE_ADDR found_addr;
278 int found = target_search_memory (start_addr, search_space_len,
279 pattern_buf, pattern_len, &found_addr);
284 print_address (gdbarch, found_addr, gdb_stdout);
285 printf_filtered ("\n");
287 last_found_addr = found_addr;
289 /* Begin next iteration at one byte past this match. */
290 next_iter_incr = (found_addr - start_addr) + 1;
292 /* For robustness, we don't let search_space_len go -ve here. */
293 if (search_space_len >= next_iter_incr)
294 search_space_len -= next_iter_incr;
296 search_space_len = 0;
297 start_addr += next_iter_incr;
300 /* Record and print the results. */
302 set_internalvar_integer (lookup_internalvar ("numfound"), found_count);
305 struct type *ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
307 set_internalvar (lookup_internalvar ("_"),
308 value_from_pointer (ptr_type, last_found_addr));
311 if (found_count == 0)
312 printf_filtered ("Pattern not found.\n");
314 printf_filtered ("%d pattern%s found.\n", found_count,
315 found_count > 1 ? "s" : "");
317 do_cleanups (old_cleanups);
320 /* Provide a prototype to silence -Wmissing-prototypes. */
321 extern initialize_file_ftype _initialize_mem_search;
324 _initialize_mem_search (void)
326 add_cmd ("find", class_vars, find_command, _("\
327 Search memory for a sequence of bytes.\n\
329 [/size-char] [/max-count] start-address, end-address, expr1 [, expr2 ...]\n\
330 find [/size-char] [/max-count] start-address, +length, expr1 [, expr2 ...]\n\
331 size-char is one of b,h,w,g for 8,16,32,64 bit values respectively,\n\
332 and if not specified the size is taken from the type of the expression\n\
333 in the current language.\n\
334 Note that this means for example that in the case of C-like languages\n\
335 a search for an untyped 0x42 will search for \"(int) 0x42\"\n\
336 which is typically four bytes.\n\
338 The address of the last match is stored as the value of \"$_\".\n\
339 Convenience variable \"$numfound\" is set to the number of matches."),