3 Copyright (C) 2008, 2009 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/>. */
22 #include "gdb_string.h"
27 /* Copied from bfd_put_bits. */
30 put_bits (bfd_uint64_t data, char *buf, int bits, bfd_boolean big_p)
35 gdb_assert (bits % 8 == 0);
38 for (i = 0; i < bytes; i++)
40 int index = big_p ? bytes - i - 1 : i;
42 buf[index] = data & 0xff;
47 /* Subroutine of find_command to simplify it.
48 Parse the arguments of the "find" command. */
51 parse_find_args (char *args, ULONGEST *max_countp,
52 char **pattern_bufp, ULONGEST *pattern_lenp,
53 CORE_ADDR *start_addrp, ULONGEST *search_space_lenp)
55 /* Default to using the specified type. */
57 ULONGEST max_count = ~(ULONGEST) 0;
58 /* Buffer to hold the search pattern. */
60 /* Current size of search pattern buffer.
61 We realloc space as needed. */
62 #define INITIAL_PATTERN_BUF_SIZE 100
63 ULONGEST pattern_buf_size = INITIAL_PATTERN_BUF_SIZE;
64 /* Pointer to one past the last in-use part of pattern_buf. */
65 char *pattern_buf_end;
68 ULONGEST search_space_len;
70 bfd_boolean big_p = gdbarch_byte_order (current_gdbarch) == BFD_ENDIAN_BIG;
71 struct cleanup *old_cleanups;
75 error (_("Missing search parameters."));
77 pattern_buf = xmalloc (pattern_buf_size);
78 pattern_buf_end = pattern_buf;
79 old_cleanups = make_cleanup (free_current_contents, &pattern_buf);
81 /* Get search granularity and/or max count if specified.
82 They may be specified in either order, together or separately. */
88 while (*s != '\0' && *s != '/' && !isspace (*s))
107 error (_("Invalid size granularity."));
115 /* Get the search range. */
117 v = parse_to_comma_and_eval (&s);
118 start_addr = value_as_address (v);
129 v = parse_to_comma_and_eval (&s);
130 len = value_as_long (v);
133 printf_filtered (_("Empty search range.\n"));
137 error (_("Invalid length."));
138 /* Watch for overflows. */
139 if (len > CORE_ADDR_MAX
140 || (start_addr + len - 1) < start_addr)
141 error (_("Search space too large."));
142 search_space_len = len;
147 v = parse_to_comma_and_eval (&s);
148 end_addr = value_as_address (v);
149 if (start_addr > end_addr)
150 error (_("Invalid search space, end preceeds start."));
151 search_space_len = end_addr - start_addr + 1;
152 /* We don't support searching all of memory
153 (i.e. start=0, end = 0xff..ff).
154 Bail to avoid overflows later on. */
155 if (search_space_len == 0)
156 error (_("Overflow in address range computation, choose smaller range."));
162 /* Fetch the search string. */
172 v = parse_to_comma_and_eval (&s);
173 val_bytes = TYPE_LENGTH (value_type (v));
175 /* Keep it simple and assume size == 'g' when watching for when we
176 need to grow the pattern buf. */
177 if ((pattern_buf_end - pattern_buf + max (val_bytes, sizeof (int64_t)))
180 size_t current_offset = pattern_buf_end - pattern_buf;
181 pattern_buf_size *= 2;
182 pattern_buf = xrealloc (pattern_buf, pattern_buf_size);
183 pattern_buf_end = pattern_buf + current_offset;
188 x = value_as_long (v);
192 *pattern_buf_end++ = x;
195 put_bits (x, pattern_buf_end, 16, big_p);
196 pattern_buf_end += sizeof (int16_t);
199 put_bits (x, pattern_buf_end, 32, big_p);
200 pattern_buf_end += sizeof (int32_t);
203 put_bits (x, pattern_buf_end, 64, big_p);
204 pattern_buf_end += sizeof (int64_t);
210 memcpy (pattern_buf_end, value_contents_raw (v), val_bytes);
211 pattern_buf_end += val_bytes;
220 if (pattern_buf_end == pattern_buf)
221 error (_("Missing search pattern."));
223 pattern_len = pattern_buf_end - pattern_buf;
225 if (search_space_len < pattern_len)
226 error (_("Search space too small to contain pattern."));
228 *max_countp = max_count;
229 *pattern_bufp = pattern_buf;
230 *pattern_lenp = pattern_len;
231 *start_addrp = start_addr;
232 *search_space_lenp = search_space_len;
234 /* We successfully parsed the arguments, leave the freeing of PATTERN_BUF
235 to the caller now. */
236 discard_cleanups (old_cleanups);
240 find_command (char *args, int from_tty)
242 /* Command line parameters.
243 These are initialized to avoid uninitialized warnings from -Wall. */
244 ULONGEST max_count = 0;
245 char *pattern_buf = 0;
246 ULONGEST pattern_len = 0;
247 CORE_ADDR start_addr = 0;
248 ULONGEST search_space_len = 0;
249 /* End of command line parameters. */
250 unsigned int found_count;
251 CORE_ADDR last_found_addr;
252 struct cleanup *old_cleanups;
254 parse_find_args (args, &max_count, &pattern_buf, &pattern_len,
255 &start_addr, &search_space_len);
257 old_cleanups = make_cleanup (free_current_contents, &pattern_buf);
259 /* Perform the search. */
264 while (search_space_len >= pattern_len
265 && found_count < max_count)
267 /* Offset from start of this iteration to the next iteration. */
268 ULONGEST next_iter_incr;
269 CORE_ADDR found_addr;
270 int found = target_search_memory (start_addr, search_space_len,
271 pattern_buf, pattern_len, &found_addr);
276 print_address (found_addr, gdb_stdout);
277 printf_filtered ("\n");
279 last_found_addr = found_addr;
281 /* Begin next iteration at one byte past this match. */
282 next_iter_incr = (found_addr - start_addr) + 1;
284 /* For robustness, we don't let search_space_len go -ve here. */
285 if (search_space_len >= next_iter_incr)
286 search_space_len -= next_iter_incr;
288 search_space_len = 0;
289 start_addr += next_iter_incr;
292 /* Record and print the results. */
294 set_internalvar_integer (lookup_internalvar ("numfound"), found_count);
297 struct gdbarch *gdbarch = current_gdbarch;
298 struct type *ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
299 set_internalvar (lookup_internalvar ("_"),
300 value_from_pointer (ptr_type, last_found_addr));
303 if (found_count == 0)
304 printf_filtered ("Pattern not found.\n");
306 printf_filtered ("%d pattern%s found.\n", found_count,
307 found_count > 1 ? "s" : "");
309 do_cleanups (old_cleanups);
312 /* Provide a prototype to silence -Wmissing-prototypes. */
313 extern initialize_file_ftype _initialize_mem_search;
316 _initialize_mem_search (void)
318 add_cmd ("find", class_vars, find_command, _("\
319 Search memory for a sequence of bytes.\n\
321 find [/size-char] [/max-count] start-address, end-address, expr1 [, expr2 ...]\n\
322 find [/size-char] [/max-count] start-address, +length, expr1 [, expr2 ...]\n\
323 size-char is one of b,h,w,g for 8,16,32,64 bit values respectively,\n\
324 and if not specified the size is taken from the type of the expression\n\
325 in the current language.\n\
326 Note that this means for example that in the case of C-like languages\n\
327 a search for an untyped 0x42 will search for \"(int) 0x42\"\n\
328 which is typically four bytes.\n\
330 The address of the last match is stored as the value of \"$_\".\n\
331 Convenience variable \"$numfound\" is set to the number of matches."),