2012-11-06 Tristan Gingold <gingold@adacore.com>
[binutils-gdb.git] / gdb / findcmd.c
blob116e0b0107beb65bbad39c73ec9d11fdc7e17be8
1 /* The find command.
3 Copyright (C) 2008-2012 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 "defs.h"
21 #include "arch-utils.h"
22 #include <ctype.h>
23 #include "gdb_string.h"
24 #include "gdbcmd.h"
25 #include "value.h"
26 #include "target.h"
28 /* Copied from bfd_put_bits. */
30 static void
31 put_bits (bfd_uint64_t data, char *buf, int bits, bfd_boolean big_p)
33 int i;
34 int bytes;
36 gdb_assert (bits % 8 == 0);
38 bytes = bits / 8;
39 for (i = 0; i < bytes; i++)
41 int index = big_p ? bytes - i - 1 : i;
43 buf[index] = data & 0xff;
44 data >>= 8;
48 /* Subroutine of find_command to simplify it.
49 Parse the arguments of the "find" command. */
51 static void
52 parse_find_args (char *args, ULONGEST *max_countp,
53 char **pattern_bufp, ULONGEST *pattern_lenp,
54 CORE_ADDR *start_addrp, ULONGEST *search_space_lenp,
55 bfd_boolean big_p)
57 /* Default to using the specified type. */
58 char size = '\0';
59 ULONGEST max_count = ~(ULONGEST) 0;
60 /* Buffer to hold the search pattern. */
61 char *pattern_buf;
62 /* Current size of search pattern buffer.
63 We realloc space as needed. */
64 #define INITIAL_PATTERN_BUF_SIZE 100
65 ULONGEST pattern_buf_size = INITIAL_PATTERN_BUF_SIZE;
66 /* Pointer to one past the last in-use part of pattern_buf. */
67 char *pattern_buf_end;
68 ULONGEST pattern_len;
69 CORE_ADDR start_addr;
70 ULONGEST search_space_len;
71 char *s = args;
72 struct cleanup *old_cleanups;
73 struct value *v;
75 if (args == NULL)
76 error (_("Missing search parameters."));
78 pattern_buf = xmalloc (pattern_buf_size);
79 pattern_buf_end = pattern_buf;
80 old_cleanups = make_cleanup (free_current_contents, &pattern_buf);
82 /* Get search granularity and/or max count if specified.
83 They may be specified in either order, together or separately. */
85 while (*s == '/')
87 ++s;
89 while (*s != '\0' && *s != '/' && !isspace (*s))
91 if (isdigit (*s))
93 max_count = atoi (s);
94 while (isdigit (*s))
95 ++s;
96 continue;
99 switch (*s)
101 case 'b':
102 case 'h':
103 case 'w':
104 case 'g':
105 size = *s++;
106 break;
107 default:
108 error (_("Invalid size granularity."));
112 while (isspace (*s))
113 ++s;
116 /* Get the search range. */
118 v = parse_to_comma_and_eval (&s);
119 start_addr = value_as_address (v);
121 if (*s == ',')
122 ++s;
123 while (isspace (*s))
124 ++s;
126 if (*s == '+')
128 LONGEST len;
130 ++s;
131 v = parse_to_comma_and_eval (&s);
132 len = value_as_long (v);
133 if (len == 0)
135 do_cleanups (old_cleanups);
136 printf_filtered (_("Empty search range.\n"));
137 return;
139 if (len < 0)
140 error (_("Invalid length."));
141 /* Watch for overflows. */
142 if (len > CORE_ADDR_MAX
143 || (start_addr + len - 1) < start_addr)
144 error (_("Search space too large."));
145 search_space_len = len;
147 else
149 CORE_ADDR end_addr;
151 v = parse_to_comma_and_eval (&s);
152 end_addr = value_as_address (v);
153 if (start_addr > end_addr)
154 error (_("Invalid search space, end precedes start."));
155 search_space_len = end_addr - start_addr + 1;
156 /* We don't support searching all of memory
157 (i.e. start=0, end = 0xff..ff).
158 Bail to avoid overflows later on. */
159 if (search_space_len == 0)
160 error (_("Overflow in address range "
161 "computation, choose smaller range."));
164 if (*s == ',')
165 ++s;
167 /* Fetch the search string. */
169 while (*s != '\0')
171 LONGEST x;
172 struct type *t;
173 ULONGEST pattern_buf_size_need;
175 while (isspace (*s))
176 ++s;
178 v = parse_to_comma_and_eval (&s);
179 t = value_type (v);
181 /* Keep it simple and assume size == 'g' when watching for when we
182 need to grow the pattern buf. */
183 pattern_buf_size_need = (pattern_buf_end - pattern_buf
184 + max (TYPE_LENGTH (t), sizeof (int64_t)));
185 if (pattern_buf_size_need > pattern_buf_size)
187 size_t current_offset = pattern_buf_end - pattern_buf;
189 pattern_buf_size = pattern_buf_size_need * 2;
190 pattern_buf = xrealloc (pattern_buf, pattern_buf_size);
191 pattern_buf_end = pattern_buf + current_offset;
194 if (size != '\0')
196 x = value_as_long (v);
197 switch (size)
199 case 'b':
200 *pattern_buf_end++ = x;
201 break;
202 case 'h':
203 put_bits (x, pattern_buf_end, 16, big_p);
204 pattern_buf_end += sizeof (int16_t);
205 break;
206 case 'w':
207 put_bits (x, pattern_buf_end, 32, big_p);
208 pattern_buf_end += sizeof (int32_t);
209 break;
210 case 'g':
211 put_bits (x, pattern_buf_end, 64, big_p);
212 pattern_buf_end += sizeof (int64_t);
213 break;
216 else
218 memcpy (pattern_buf_end, value_contents (v), TYPE_LENGTH (t));
219 pattern_buf_end += TYPE_LENGTH (t);
222 if (*s == ',')
223 ++s;
224 while (isspace (*s))
225 ++s;
228 if (pattern_buf_end == pattern_buf)
229 error (_("Missing search pattern."));
231 pattern_len = pattern_buf_end - pattern_buf;
233 if (search_space_len < pattern_len)
234 error (_("Search space too small to contain pattern."));
236 *max_countp = max_count;
237 *pattern_bufp = pattern_buf;
238 *pattern_lenp = pattern_len;
239 *start_addrp = start_addr;
240 *search_space_lenp = search_space_len;
242 /* We successfully parsed the arguments, leave the freeing of PATTERN_BUF
243 to the caller now. */
244 discard_cleanups (old_cleanups);
247 static void
248 find_command (char *args, int from_tty)
250 struct gdbarch *gdbarch = get_current_arch ();
251 bfd_boolean big_p = gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG;
252 /* Command line parameters.
253 These are initialized to avoid uninitialized warnings from -Wall. */
254 ULONGEST max_count = 0;
255 char *pattern_buf = 0;
256 ULONGEST pattern_len = 0;
257 CORE_ADDR start_addr = 0;
258 ULONGEST search_space_len = 0;
259 /* End of command line parameters. */
260 unsigned int found_count;
261 CORE_ADDR last_found_addr;
262 struct cleanup *old_cleanups;
264 parse_find_args (args, &max_count, &pattern_buf, &pattern_len,
265 &start_addr, &search_space_len, big_p);
267 old_cleanups = make_cleanup (free_current_contents, &pattern_buf);
269 /* Perform the search. */
271 found_count = 0;
272 last_found_addr = 0;
274 while (search_space_len >= pattern_len
275 && found_count < max_count)
277 /* Offset from start of this iteration to the next iteration. */
278 ULONGEST next_iter_incr;
279 CORE_ADDR found_addr;
280 int found = target_search_memory (start_addr, search_space_len,
281 pattern_buf, pattern_len, &found_addr);
283 if (found <= 0)
284 break;
286 print_address (gdbarch, found_addr, gdb_stdout);
287 printf_filtered ("\n");
288 ++found_count;
289 last_found_addr = found_addr;
291 /* Begin next iteration at one byte past this match. */
292 next_iter_incr = (found_addr - start_addr) + 1;
294 /* For robustness, we don't let search_space_len go -ve here. */
295 if (search_space_len >= next_iter_incr)
296 search_space_len -= next_iter_incr;
297 else
298 search_space_len = 0;
299 start_addr += next_iter_incr;
302 /* Record and print the results. */
304 set_internalvar_integer (lookup_internalvar ("numfound"), found_count);
305 if (found_count > 0)
307 struct type *ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
309 set_internalvar (lookup_internalvar ("_"),
310 value_from_pointer (ptr_type, last_found_addr));
313 if (found_count == 0)
314 printf_filtered ("Pattern not found.\n");
315 else
316 printf_filtered ("%d pattern%s found.\n", found_count,
317 found_count > 1 ? "s" : "");
319 do_cleanups (old_cleanups);
322 /* Provide a prototype to silence -Wmissing-prototypes. */
323 extern initialize_file_ftype _initialize_mem_search;
325 void
326 _initialize_mem_search (void)
328 add_cmd ("find", class_vars, find_command, _("\
329 Search memory for a sequence of bytes.\n\
330 Usage:\nfind \
331 [/size-char] [/max-count] start-address, end-address, expr1 [, expr2 ...]\n\
332 find [/size-char] [/max-count] start-address, +length, expr1 [, expr2 ...]\n\
333 size-char is one of b,h,w,g for 8,16,32,64 bit values respectively,\n\
334 and if not specified the size is taken from the type of the expression\n\
335 in the current language.\n\
336 Note that this means for example that in the case of C-like languages\n\
337 a search for an untyped 0x42 will search for \"(int) 0x42\"\n\
338 which is typically four bytes.\n\
340 The address of the last match is stored as the value of \"$_\".\n\
341 Convenience variable \"$numfound\" is set to the number of matches."),
342 &cmdlist);