xref: /dragonfly/contrib/gdb-7/gdb/findcmd.c (revision 9348a738)
1 /* The find command.
2 
3    Copyright (C) 2008-2013 Free Software Foundation, Inc.
4 
5    This file is part of GDB.
6 
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.
11 
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.
16 
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/>.  */
19 
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"
27 #include "cli/cli-utils.h"
28 
29 /* Copied from bfd_put_bits.  */
30 
31 static void
32 put_bits (bfd_uint64_t data, gdb_byte *buf, int bits, bfd_boolean big_p)
33 {
34   int i;
35   int bytes;
36 
37   gdb_assert (bits % 8 == 0);
38 
39   bytes = bits / 8;
40   for (i = 0; i < bytes; i++)
41     {
42       int index = big_p ? bytes - i - 1 : i;
43 
44       buf[index] = data & 0xff;
45       data >>= 8;
46     }
47 }
48 
49 /* Subroutine of find_command to simplify it.
50    Parse the arguments of the "find" command.  */
51 
52 static void
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,
56 		 bfd_boolean big_p)
57 {
58   /* Default to using the specified type.  */
59   char size = '\0';
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;
69   ULONGEST pattern_len;
70   CORE_ADDR start_addr;
71   ULONGEST search_space_len;
72   const char *s = args;
73   struct cleanup *old_cleanups;
74   struct value *v;
75 
76   if (args == NULL)
77     error (_("Missing search parameters."));
78 
79   pattern_buf = xmalloc (pattern_buf_size);
80   pattern_buf_end = pattern_buf;
81   old_cleanups = make_cleanup (free_current_contents, &pattern_buf);
82 
83   /* Get search granularity and/or max count if specified.
84      They may be specified in either order, together or separately.  */
85 
86   while (*s == '/')
87     {
88       ++s;
89 
90       while (*s != '\0' && *s != '/' && !isspace (*s))
91 	{
92 	  if (isdigit (*s))
93 	    {
94 	      max_count = atoi (s);
95 	      while (isdigit (*s))
96 		++s;
97 	      continue;
98 	    }
99 
100 	  switch (*s)
101 	    {
102 	    case 'b':
103 	    case 'h':
104 	    case 'w':
105 	    case 'g':
106 	      size = *s++;
107 	      break;
108 	    default:
109 	      error (_("Invalid size granularity."));
110 	    }
111 	}
112 
113       s = skip_spaces_const (s);
114     }
115 
116   /* Get the search range.  */
117 
118   v = parse_to_comma_and_eval (&s);
119   start_addr = value_as_address (v);
120 
121   if (*s == ',')
122     ++s;
123   s = skip_spaces_const (s);
124 
125   if (*s == '+')
126     {
127       LONGEST len;
128 
129       ++s;
130       v = parse_to_comma_and_eval (&s);
131       len = value_as_long (v);
132       if (len == 0)
133 	{
134 	  do_cleanups (old_cleanups);
135 	  printf_filtered (_("Empty search range.\n"));
136 	  return;
137 	}
138       if (len < 0)
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;
145     }
146   else
147     {
148       CORE_ADDR end_addr;
149 
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."));
161     }
162 
163   if (*s == ',')
164     ++s;
165 
166   /* Fetch the search string.  */
167 
168   while (*s != '\0')
169     {
170       LONGEST x;
171       struct type *t;
172       ULONGEST pattern_buf_size_need;
173 
174       s = skip_spaces_const (s);
175 
176       v = parse_to_comma_and_eval (&s);
177       t = value_type (v);
178 
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 			       + max (TYPE_LENGTH (t), sizeof (int64_t)));
183       if (pattern_buf_size_need > pattern_buf_size)
184 	{
185 	  size_t current_offset = pattern_buf_end - pattern_buf;
186 
187 	  pattern_buf_size = pattern_buf_size_need * 2;
188 	  pattern_buf = xrealloc (pattern_buf, pattern_buf_size);
189 	  pattern_buf_end = pattern_buf + current_offset;
190 	}
191 
192       if (size != '\0')
193 	{
194 	  x = value_as_long (v);
195 	  switch (size)
196 	    {
197 	    case 'b':
198 	      *pattern_buf_end++ = x;
199 	      break;
200 	    case 'h':
201 	      put_bits (x, pattern_buf_end, 16, big_p);
202 	      pattern_buf_end += sizeof (int16_t);
203 	      break;
204 	    case 'w':
205 	      put_bits (x, pattern_buf_end, 32, big_p);
206 	      pattern_buf_end += sizeof (int32_t);
207 	      break;
208 	    case 'g':
209 	      put_bits (x, pattern_buf_end, 64, big_p);
210 	      pattern_buf_end += sizeof (int64_t);
211 	      break;
212 	    }
213 	}
214       else
215 	{
216 	  memcpy (pattern_buf_end, value_contents (v), TYPE_LENGTH (t));
217 	  pattern_buf_end += TYPE_LENGTH (t);
218 	}
219 
220       if (*s == ',')
221 	++s;
222       s = skip_spaces_const (s);
223     }
224 
225   if (pattern_buf_end == pattern_buf)
226     error (_("Missing search pattern."));
227 
228   pattern_len = pattern_buf_end - pattern_buf;
229 
230   if (search_space_len < pattern_len)
231     error (_("Search space too small to contain pattern."));
232 
233   *max_countp = max_count;
234   *pattern_bufp = pattern_buf;
235   *pattern_lenp = pattern_len;
236   *start_addrp = start_addr;
237   *search_space_lenp = search_space_len;
238 
239   /* We successfully parsed the arguments, leave the freeing of PATTERN_BUF
240      to the caller now.  */
241   discard_cleanups (old_cleanups);
242 }
243 
244 static void
245 find_command (char *args, int from_tty)
246 {
247   struct gdbarch *gdbarch = get_current_arch ();
248   bfd_boolean big_p = gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG;
249   /* Command line parameters.
250      These are initialized to avoid uninitialized warnings from -Wall.  */
251   ULONGEST max_count = 0;
252   gdb_byte *pattern_buf = 0;
253   ULONGEST pattern_len = 0;
254   CORE_ADDR start_addr = 0;
255   ULONGEST search_space_len = 0;
256   /* End of command line parameters.  */
257   unsigned int found_count;
258   CORE_ADDR last_found_addr;
259   struct cleanup *old_cleanups;
260 
261   parse_find_args (args, &max_count, &pattern_buf, &pattern_len,
262 		   &start_addr, &search_space_len, big_p);
263 
264   old_cleanups = make_cleanup (free_current_contents, &pattern_buf);
265 
266   /* Perform the search.  */
267 
268   found_count = 0;
269   last_found_addr = 0;
270 
271   while (search_space_len >= pattern_len
272 	 && found_count < max_count)
273     {
274       /* Offset from start of this iteration to the next iteration.  */
275       ULONGEST next_iter_incr;
276       CORE_ADDR found_addr;
277       int found = target_search_memory (start_addr, search_space_len,
278 					pattern_buf, pattern_len, &found_addr);
279 
280       if (found <= 0)
281 	break;
282 
283       print_address (gdbarch, found_addr, gdb_stdout);
284       printf_filtered ("\n");
285       ++found_count;
286       last_found_addr = found_addr;
287 
288       /* Begin next iteration at one byte past this match.  */
289       next_iter_incr = (found_addr - start_addr) + 1;
290 
291       /* For robustness, we don't let search_space_len go -ve here.  */
292       if (search_space_len >= next_iter_incr)
293 	search_space_len -= next_iter_incr;
294       else
295 	search_space_len = 0;
296       start_addr += next_iter_incr;
297     }
298 
299   /* Record and print the results.  */
300 
301   set_internalvar_integer (lookup_internalvar ("numfound"), found_count);
302   if (found_count > 0)
303     {
304       struct type *ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
305 
306       set_internalvar (lookup_internalvar ("_"),
307 		       value_from_pointer (ptr_type, last_found_addr));
308     }
309 
310   if (found_count == 0)
311     printf_filtered ("Pattern not found.\n");
312   else
313     printf_filtered ("%d pattern%s found.\n", found_count,
314 		     found_count > 1 ? "s" : "");
315 
316   do_cleanups (old_cleanups);
317 }
318 
319 /* Provide a prototype to silence -Wmissing-prototypes.  */
320 extern initialize_file_ftype _initialize_mem_search;
321 
322 void
323 _initialize_mem_search (void)
324 {
325   add_cmd ("find", class_vars, find_command, _("\
326 Search memory for a sequence of bytes.\n\
327 Usage:\nfind \
328 [/size-char] [/max-count] start-address, end-address, expr1 [, expr2 ...]\n\
329 find [/size-char] [/max-count] start-address, +length, expr1 [, expr2 ...]\n\
330 size-char is one of b,h,w,g for 8,16,32,64 bit values respectively,\n\
331 and if not specified the size is taken from the type of the expression\n\
332 in the current language.\n\
333 Note that this means for example that in the case of C-like languages\n\
334 a search for an untyped 0x42 will search for \"(int) 0x42\"\n\
335 which is typically four bytes.\n\
336 \n\
337 The address of the last match is stored as the value of \"$_\".\n\
338 Convenience variable \"$numfound\" is set to the number of matches."),
339 	   &cmdlist);
340 }
341