GDB (xrefs)
/tmp/gdb-7.10/gdb/findcmd.c
Go to the documentation of this file.
1 /* The find command.
2 
3  Copyright (C) 2008-2015 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 "gdbcmd.h"
24 #include "value.h"
25 #include "target.h"
26 #include "cli/cli-utils.h"
27 
28 /* Copied from bfd_put_bits. */
29 
30 static void
31 put_bits (bfd_uint64_t data, gdb_byte *buf, int bits, bfd_boolean big_p)
32 {
33  int i;
34  int bytes;
35 
36  gdb_assert (bits % 8 == 0);
37 
38  bytes = bits / 8;
39  for (i = 0; i < bytes; i++)
40  {
41  int index = big_p ? bytes - i - 1 : i;
42 
43  buf[index] = data & 0xff;
44  data >>= 8;
45  }
46 }
47 
48 /* Subroutine of find_command to simplify it.
49  Parse the arguments of the "find" command. */
50 
51 static void
52 parse_find_args (char *args, ULONGEST *max_countp,
53  gdb_byte **pattern_bufp, ULONGEST *pattern_lenp,
54  CORE_ADDR *start_addrp, ULONGEST *search_space_lenp,
55  bfd_boolean big_p)
56 {
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  gdb_byte *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  gdb_byte *pattern_buf_end;
68  ULONGEST pattern_len;
69  CORE_ADDR start_addr;
70  ULONGEST search_space_len;
71  const char *s = args;
72  struct cleanup *old_cleanups;
73  struct value *v;
74 
75  if (args == NULL)
76  error (_("Missing search parameters."));
77 
78  pattern_buf = xmalloc (pattern_buf_size);
79  pattern_buf_end = pattern_buf;
80  old_cleanups = make_cleanup (free_current_contents, &pattern_buf);
81 
82  /* Get search granularity and/or max count if specified.
83  They may be specified in either order, together or separately. */
84 
85  while (*s == '/')
86  {
87  ++s;
88 
89  while (*s != '\0' && *s != '/' && !isspace (*s))
90  {
91  if (isdigit (*s))
92  {
93  max_count = atoi (s);
94  while (isdigit (*s))
95  ++s;
96  continue;
97  }
98 
99  switch (*s)
100  {
101  case 'b':
102  case 'h':
103  case 'w':
104  case 'g':
105  size = *s++;
106  break;
107  default:
108  error (_("Invalid size granularity."));
109  }
110  }
111 
112  s = skip_spaces_const (s);
113  }
114 
115  /* Get the search range. */
116 
117  v = parse_to_comma_and_eval (&s);
118  start_addr = value_as_address (v);
119 
120  if (*s == ',')
121  ++s;
122  s = skip_spaces_const (s);
123 
124  if (*s == '+')
125  {
126  LONGEST len;
127 
128  ++s;
129  v = parse_to_comma_and_eval (&s);
130  len = value_as_long (v);
131  if (len == 0)
132  {
133  do_cleanups (old_cleanups);
134  printf_filtered (_("Empty search range.\n"));
135  return;
136  }
137  if (len < 0)
138  error (_("Invalid length."));
139  /* Watch for overflows. */
140  if (len > CORE_ADDR_MAX
141  || (start_addr + len - 1) < start_addr)
142  error (_("Search space too large."));
143  search_space_len = len;
144  }
145  else
146  {
147  CORE_ADDR end_addr;
148 
149  v = parse_to_comma_and_eval (&s);
150  end_addr = value_as_address (v);
151  if (start_addr > end_addr)
152  error (_("Invalid search space, end precedes start."));
153  search_space_len = end_addr - start_addr + 1;
154  /* We don't support searching all of memory
155  (i.e. start=0, end = 0xff..ff).
156  Bail to avoid overflows later on. */
157  if (search_space_len == 0)
158  error (_("Overflow in address range "
159  "computation, choose smaller range."));
160  }
161 
162  if (*s == ',')
163  ++s;
164 
165  /* Fetch the search string. */
166 
167  while (*s != '\0')
168  {
169  LONGEST x;
170  struct type *t;
171  ULONGEST pattern_buf_size_need;
172 
173  s = skip_spaces_const (s);
174 
175  v = parse_to_comma_and_eval (&s);
176  t = value_type (v);
177 
178  /* Keep it simple and assume size == 'g' when watching for when we
179  need to grow the pattern buf. */
180  pattern_buf_size_need = (pattern_buf_end - pattern_buf
181  + max (TYPE_LENGTH (t), sizeof (int64_t)));
182  if (pattern_buf_size_need > pattern_buf_size)
183  {
184  size_t current_offset = pattern_buf_end - pattern_buf;
185 
186  pattern_buf_size = pattern_buf_size_need * 2;
187  pattern_buf = xrealloc (pattern_buf, pattern_buf_size);
188  pattern_buf_end = pattern_buf + current_offset;
189  }
190 
191  if (size != '\0')
192  {
193  x = value_as_long (v);
194  switch (size)
195  {
196  case 'b':
197  *pattern_buf_end++ = x;
198  break;
199  case 'h':
200  put_bits (x, pattern_buf_end, 16, big_p);
201  pattern_buf_end += sizeof (int16_t);
202  break;
203  case 'w':
204  put_bits (x, pattern_buf_end, 32, big_p);
205  pattern_buf_end += sizeof (int32_t);
206  break;
207  case 'g':
208  put_bits (x, pattern_buf_end, 64, big_p);
209  pattern_buf_end += sizeof (int64_t);
210  break;
211  }
212  }
213  else
214  {
215  memcpy (pattern_buf_end, value_contents (v), TYPE_LENGTH (t));
216  pattern_buf_end += TYPE_LENGTH (t);
217  }
218 
219  if (*s == ',')
220  ++s;
221  s = skip_spaces_const (s);
222  }
223 
224  if (pattern_buf_end == pattern_buf)
225  error (_("Missing search pattern."));
226 
227  pattern_len = pattern_buf_end - pattern_buf;
228 
229  if (search_space_len < pattern_len)
230  error (_("Search space too small to contain pattern."));
231 
232  *max_countp = max_count;
233  *pattern_bufp = pattern_buf;
234  *pattern_lenp = pattern_len;
235  *start_addrp = start_addr;
236  *search_space_lenp = search_space_len;
237 
238  /* We successfully parsed the arguments, leave the freeing of PATTERN_BUF
239  to the caller now. */
240  discard_cleanups (old_cleanups);
241 }
242 
243 static void
244 find_command (char *args, int from_tty)
245 {
246  struct gdbarch *gdbarch = get_current_arch ();
247  bfd_boolean big_p = gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG;
248  /* Command line parameters.
249  These are initialized to avoid uninitialized warnings from -Wall. */
250  ULONGEST max_count = 0;
251  gdb_byte *pattern_buf = 0;
252  ULONGEST pattern_len = 0;
253  CORE_ADDR start_addr = 0;
254  ULONGEST search_space_len = 0;
255  /* End of command line parameters. */
256  unsigned int found_count;
257  CORE_ADDR last_found_addr;
258  struct cleanup *old_cleanups;
259 
260  parse_find_args (args, &max_count, &pattern_buf, &pattern_len,
261  &start_addr, &search_space_len, big_p);
262 
263  old_cleanups = make_cleanup (free_current_contents, &pattern_buf);
264 
265  /* Perform the search. */
266 
267  found_count = 0;
268  last_found_addr = 0;
269 
270  while (search_space_len >= pattern_len
271  && found_count < max_count)
272  {
273  /* Offset from start of this iteration to the next iteration. */
274  ULONGEST next_iter_incr;
275  CORE_ADDR found_addr;
276  int found = target_search_memory (start_addr, search_space_len,
277  pattern_buf, pattern_len, &found_addr);
278 
279  if (found <= 0)
280  break;
281 
282  print_address (gdbarch, found_addr, gdb_stdout);
283  printf_filtered ("\n");
284  ++found_count;
285  last_found_addr = found_addr;
286 
287  /* Begin next iteration at one byte past this match. */
288  next_iter_incr = (found_addr - start_addr) + 1;
289 
290  /* For robustness, we don't let search_space_len go -ve here. */
291  if (search_space_len >= next_iter_incr)
292  search_space_len -= next_iter_incr;
293  else
294  search_space_len = 0;
295  start_addr += next_iter_incr;
296  }
297 
298  /* Record and print the results. */
299 
300  set_internalvar_integer (lookup_internalvar ("numfound"), found_count);
301  if (found_count > 0)
302  {
303  struct type *ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
304 
306  value_from_pointer (ptr_type, last_found_addr));
307  }
308 
309  if (found_count == 0)
310  printf_filtered ("Pattern not found.\n");
311  else
312  printf_filtered ("%d pattern%s found.\n", found_count,
313  found_count > 1 ? "s" : "");
314 
315  do_cleanups (old_cleanups);
316 }
317 
318 /* Provide a prototype to silence -Wmissing-prototypes. */
320 
321 void
323 {
324  add_cmd ("find", class_vars, find_command, _("\
325 Search memory for a sequence of bytes.\n\
326 Usage:\nfind \
327 [/size-char] [/max-count] start-address, end-address, expr1 [, expr2 ...]\n\
328 find [/size-char] [/max-count] start-address, +length, expr1 [, expr2 ...]\n\
329 size-char is one of b,h,w,g for 8,16,32,64 bit values respectively,\n\
330 and if not specified the size is taken from the type of the expression\n\
331 in the current language.\n\
332 Note that this means for example that in the case of C-like languages\n\
333 a search for an untyped 0x42 will search for \"(int) 0x42\"\n\
334 which is typically four bytes.\n\
335 \n\
336 The address of the last match is stored as the value of \"$_\".\n\
337 Convenience variable \"$numfound\" is set to the number of matches."),
338  &cmdlist);
339 }
bfd_vma CORE_ADDR
Definition: common-types.h:41
void print_address(struct gdbarch *, CORE_ADDR, struct ui_file *)
Definition: printcmd.c:741
LONGEST value_as_long(struct value *val)
Definition: value.c:2654
struct ui_file * gdb_stdout
Definition: main.c:71
const struct builtin_type * builtin_type(struct gdbarch *gdbarch)
Definition: gdbtypes.c:4766
struct cmd_list_element * cmdlist
Definition: cli-cmds.c:103
struct internalvar * lookup_internalvar(const char *name)
Definition: value.c:2131
#define _(String)
Definition: gdb_locale.h:40
#define bits(obj, st, fn)
void printf_filtered(const char *format,...)
Definition: utils.c:2388
const gdb_byte * value_contents(struct value *value)
Definition: value.c:1329
void initialize_file_ftype(void)
Definition: defs.h:281
#define CORE_ADDR_MAX
Definition: common-types.h:59
const char * skip_spaces_const(const char *chp)
Definition: common-utils.c:271
struct cmd_list_element * add_cmd(const char *name, enum command_class theclass, cmd_cfunc_ftype *fun, const char *doc, struct cmd_list_element **list)
Definition: cli-decode.c:192
void free_current_contents(void *ptr)
Definition: utils.c:476
enum bfd_endian gdbarch_byte_order(struct gdbarch *gdbarch)
Definition: gdbarch.c:1420
void set_internalvar(struct internalvar *var, struct value *val)
Definition: value.c:2295
struct cleanup * make_cleanup(make_cleanup_ftype *function, void *arg)
Definition: cleanups.c:117
Definition: gdbtypes.h:749
struct gdbarch * get_current_arch(void)
Definition: arch-utils.c:781
#define gdb_assert(expr)
Definition: gdb_assert.h:33
void * xmalloc(YYSIZE_T)
Definition: value.c:172
struct value * parse_to_comma_and_eval(const char **expp)
Definition: eval.c:141
PTR xrealloc(PTR ptr, size_t size)
Definition: common-utils.c:51
bfd_byte gdb_byte
Definition: common-types.h:38
struct value * value_from_pointer(struct type *type, CORE_ADDR addr)
Definition: value.c:3490
void discard_cleanups(struct cleanup *old_chain)
Definition: cleanups.c:213
#define max(a, b)
Definition: defs.h:109
struct type * builtin_data_ptr
Definition: gdbtypes.h:1533
int target_search_memory(CORE_ADDR start_addr, ULONGEST search_space_len, const gdb_byte *pattern, ULONGEST pattern_len, CORE_ADDR *found_addrp)
Definition: target.c:2448
void set_internalvar_integer(struct internalvar *var, LONGEST l)
Definition: value.c:2347
initialize_file_ftype _initialize_mem_search
unsigned long long ULONGEST
Definition: common-types.h:53
#define INITIAL_PATTERN_BUF_SIZE
struct type * value_type(const struct value *value)
Definition: value.c:1021
CORE_ADDR value_as_address(struct value *val)
Definition: value.c:2679
#define TYPE_LENGTH(thistype)
Definition: gdbtypes.h:1237
static void find_command(char *args, int from_tty)
Definition: findcmd.c:244
void error(const char *fmt,...)
Definition: errors.c:38
size_t size
Definition: go32-nat.c:242
static void parse_find_args(char *args, ULONGEST *max_countp, gdb_byte **pattern_bufp, ULONGEST *pattern_lenp, CORE_ADDR *start_addrp, ULONGEST *search_space_lenp, bfd_boolean big_p)
Definition: findcmd.c:52
static void put_bits(bfd_uint64_t data, gdb_byte *buf, int bits, bfd_boolean big_p)
Definition: findcmd.c:31
long long LONGEST
Definition: common-types.h:52
void do_cleanups(struct cleanup *old_chain)
Definition: cleanups.c:175
const ULONGEST const LONGEST len
Definition: target.h:309