gdb/findcmd.c - gdb

Functions defined

Macros defined

Source code

  1. /* The find command.

  2.    Copyright (C) 2008-2015 Free Software Foundation, Inc.

  3.    This file is part of GDB.

  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 3 of the License, or
  7.    (at your option) any later version.

  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.

  12.    You should have received a copy of the GNU General Public License
  13.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  14. #include "defs.h"
  15. #include "arch-utils.h"
  16. #include <ctype.h>
  17. #include "gdbcmd.h"
  18. #include "value.h"
  19. #include "target.h"
  20. #include "cli/cli-utils.h"

  21. /* Copied from bfd_put_bits.  */

  22. static void
  23. put_bits (bfd_uint64_t data, gdb_byte *buf, int bits, bfd_boolean big_p)
  24. {
  25.   int i;
  26.   int bytes;

  27.   gdb_assert (bits % 8 == 0);

  28.   bytes = bits / 8;
  29.   for (i = 0; i < bytes; i++)
  30.     {
  31.       int index = big_p ? bytes - i - 1 : i;

  32.       buf[index] = data & 0xff;
  33.       data >>= 8;
  34.     }
  35. }

  36. /* Subroutine of find_command to simplify it.
  37.    Parse the arguments of the "find" command.  */

  38. static void
  39. parse_find_args (char *args, ULONGEST *max_countp,
  40.                  gdb_byte **pattern_bufp, ULONGEST *pattern_lenp,
  41.                  CORE_ADDR *start_addrp, ULONGEST *search_space_lenp,
  42.                  bfd_boolean big_p)
  43. {
  44.   /* Default to using the specified type.  */
  45.   char size = '\0';
  46.   ULONGEST max_count = ~(ULONGEST) 0;
  47.   /* Buffer to hold the search pattern.  */
  48.   gdb_byte *pattern_buf;
  49.   /* Current size of search pattern buffer.
  50.      We realloc space as needed.  */
  51. #define INITIAL_PATTERN_BUF_SIZE 100
  52.   ULONGEST pattern_buf_size = INITIAL_PATTERN_BUF_SIZE;
  53.   /* Pointer to one past the last in-use part of pattern_buf.  */
  54.   gdb_byte *pattern_buf_end;
  55.   ULONGEST pattern_len;
  56.   CORE_ADDR start_addr;
  57.   ULONGEST search_space_len;
  58.   const char *s = args;
  59.   struct cleanup *old_cleanups;
  60.   struct value *v;

  61.   if (args == NULL)
  62.     error (_("Missing search parameters."));

  63.   pattern_buf = xmalloc (pattern_buf_size);
  64.   pattern_buf_end = pattern_buf;
  65.   old_cleanups = make_cleanup (free_current_contents, &pattern_buf);

  66.   /* Get search granularity and/or max count if specified.
  67.      They may be specified in either order, together or separately.  */

  68.   while (*s == '/')
  69.     {
  70.       ++s;

  71.       while (*s != '\0' && *s != '/' && !isspace (*s))
  72.         {
  73.           if (isdigit (*s))
  74.             {
  75.               max_count = atoi (s);
  76.               while (isdigit (*s))
  77.                 ++s;
  78.               continue;
  79.             }

  80.           switch (*s)
  81.             {
  82.             case 'b':
  83.             case 'h':
  84.             case 'w':
  85.             case 'g':
  86.               size = *s++;
  87.               break;
  88.             default:
  89.               error (_("Invalid size granularity."));
  90.             }
  91.         }

  92.       s = skip_spaces_const (s);
  93.     }

  94.   /* Get the search range.  */

  95.   v = parse_to_comma_and_eval (&s);
  96.   start_addr = value_as_address (v);

  97.   if (*s == ',')
  98.     ++s;
  99.   s = skip_spaces_const (s);

  100.   if (*s == '+')
  101.     {
  102.       LONGEST len;

  103.       ++s;
  104.       v = parse_to_comma_and_eval (&s);
  105.       len = value_as_long (v);
  106.       if (len == 0)
  107.         {
  108.           do_cleanups (old_cleanups);
  109.           printf_filtered (_("Empty search range.\n"));
  110.           return;
  111.         }
  112.       if (len < 0)
  113.         error (_("Invalid length."));
  114.       /* Watch for overflows.  */
  115.       if (len > CORE_ADDR_MAX
  116.           || (start_addr + len - 1) < start_addr)
  117.         error (_("Search space too large."));
  118.       search_space_len = len;
  119.     }
  120.   else
  121.     {
  122.       CORE_ADDR end_addr;

  123.       v = parse_to_comma_and_eval (&s);
  124.       end_addr = value_as_address (v);
  125.       if (start_addr > end_addr)
  126.         error (_("Invalid search space, end precedes start."));
  127.       search_space_len = end_addr - start_addr + 1;
  128.       /* We don't support searching all of memory
  129.          (i.e. start=0, end = 0xff..ff).
  130.          Bail to avoid overflows later on.  */
  131.       if (search_space_len == 0)
  132.         error (_("Overflow in address range "
  133.                  "computation, choose smaller range."));
  134.     }

  135.   if (*s == ',')
  136.     ++s;

  137.   /* Fetch the search string.  */

  138.   while (*s != '\0')
  139.     {
  140.       LONGEST x;
  141.       struct type *t;
  142.       ULONGEST pattern_buf_size_need;

  143.       s = skip_spaces_const (s);

  144.       v = parse_to_comma_and_eval (&s);
  145.       t = value_type (v);

  146.       /* Keep it simple and assume size == 'g' when watching for when we
  147.          need to grow the pattern buf.  */
  148.       pattern_buf_size_need = (pattern_buf_end - pattern_buf
  149.                                + max (TYPE_LENGTH (t), sizeof (int64_t)));
  150.       if (pattern_buf_size_need > pattern_buf_size)
  151.         {
  152.           size_t current_offset = pattern_buf_end - pattern_buf;

  153.           pattern_buf_size = pattern_buf_size_need * 2;
  154.           pattern_buf = xrealloc (pattern_buf, pattern_buf_size);
  155.           pattern_buf_end = pattern_buf + current_offset;
  156.         }

  157.       if (size != '\0')
  158.         {
  159.           x = value_as_long (v);
  160.           switch (size)
  161.             {
  162.             case 'b':
  163.               *pattern_buf_end++ = x;
  164.               break;
  165.             case 'h':
  166.               put_bits (x, pattern_buf_end, 16, big_p);
  167.               pattern_buf_end += sizeof (int16_t);
  168.               break;
  169.             case 'w':
  170.               put_bits (x, pattern_buf_end, 32, big_p);
  171.               pattern_buf_end += sizeof (int32_t);
  172.               break;
  173.             case 'g':
  174.               put_bits (x, pattern_buf_end, 64, big_p);
  175.               pattern_buf_end += sizeof (int64_t);
  176.               break;
  177.             }
  178.         }
  179.       else
  180.         {
  181.           memcpy (pattern_buf_end, value_contents (v), TYPE_LENGTH (t));
  182.           pattern_buf_end += TYPE_LENGTH (t);
  183.         }

  184.       if (*s == ',')
  185.         ++s;
  186.       s = skip_spaces_const (s);
  187.     }

  188.   if (pattern_buf_end == pattern_buf)
  189.     error (_("Missing search pattern."));

  190.   pattern_len = pattern_buf_end - pattern_buf;

  191.   if (search_space_len < pattern_len)
  192.     error (_("Search space too small to contain pattern."));

  193.   *max_countp = max_count;
  194.   *pattern_bufp = pattern_buf;
  195.   *pattern_lenp = pattern_len;
  196.   *start_addrp = start_addr;
  197.   *search_space_lenp = search_space_len;

  198.   /* We successfully parsed the arguments, leave the freeing of PATTERN_BUF
  199.      to the caller now.  */
  200.   discard_cleanups (old_cleanups);
  201. }

  202. static void
  203. find_command (char *args, int from_tty)
  204. {
  205.   struct gdbarch *gdbarch = get_current_arch ();
  206.   bfd_boolean big_p = gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG;
  207.   /* Command line parameters.
  208.      These are initialized to avoid uninitialized warnings from -Wall.  */
  209.   ULONGEST max_count = 0;
  210.   gdb_byte *pattern_buf = 0;
  211.   ULONGEST pattern_len = 0;
  212.   CORE_ADDR start_addr = 0;
  213.   ULONGEST search_space_len = 0;
  214.   /* End of command line parameters.  */
  215.   unsigned int found_count;
  216.   CORE_ADDR last_found_addr;
  217.   struct cleanup *old_cleanups;

  218.   parse_find_args (args, &max_count, &pattern_buf, &pattern_len,
  219.                    &start_addr, &search_space_len, big_p);

  220.   old_cleanups = make_cleanup (free_current_contents, &pattern_buf);

  221.   /* Perform the search.  */

  222.   found_count = 0;
  223.   last_found_addr = 0;

  224.   while (search_space_len >= pattern_len
  225.          && found_count < max_count)
  226.     {
  227.       /* Offset from start of this iteration to the next iteration.  */
  228.       ULONGEST next_iter_incr;
  229.       CORE_ADDR found_addr;
  230.       int found = target_search_memory (start_addr, search_space_len,
  231.                                         pattern_buf, pattern_len, &found_addr);

  232.       if (found <= 0)
  233.         break;

  234.       print_address (gdbarch, found_addr, gdb_stdout);
  235.       printf_filtered ("\n");
  236.       ++found_count;
  237.       last_found_addr = found_addr;

  238.       /* Begin next iteration at one byte past this match.  */
  239.       next_iter_incr = (found_addr - start_addr) + 1;

  240.       /* For robustness, we don't let search_space_len go -ve here.  */
  241.       if (search_space_len >= next_iter_incr)
  242.         search_space_len -= next_iter_incr;
  243.       else
  244.         search_space_len = 0;
  245.       start_addr += next_iter_incr;
  246.     }

  247.   /* Record and print the results.  */

  248.   set_internalvar_integer (lookup_internalvar ("numfound"), found_count);
  249.   if (found_count > 0)
  250.     {
  251.       struct type *ptr_type = builtin_type (gdbarch)->builtin_data_ptr;

  252.       set_internalvar (lookup_internalvar ("_"),
  253.                        value_from_pointer (ptr_type, last_found_addr));
  254.     }

  255.   if (found_count == 0)
  256.     printf_filtered ("Pattern not found.\n");
  257.   else
  258.     printf_filtered ("%d pattern%s found.\n", found_count,
  259.                      found_count > 1 ? "s" : "");

  260.   do_cleanups (old_cleanups);
  261. }

  262. /* Provide a prototype to silence -Wmissing-prototypes.  */
  263. extern initialize_file_ftype _initialize_mem_search;

  264. void
  265. _initialize_mem_search (void)
  266. {
  267.   add_cmd ("find", class_vars, find_command, _("\
  268. Search memory for a sequence of bytes.\n\
  269. Usage:\nfind \
  270. [/size-char] [/max-count] start-address, end-address, expr1 [, expr2 ...]\n\
  271. find [/size-char] [/max-count] start-address, +length, expr1 [, expr2 ...]\n\
  272. size-char is one of b,h,w,g for 8,16,32,64 bit values respectively,\n\
  273. and if not specified the size is taken from the type of the expression\n\
  274. in the current language.\n\
  275. Note that this means for example that in the case of C-like languages\n\
  276. a search for an untyped 0x42 will search for \"(int) 0x42\"\n\
  277. which is typically four bytes.\n\
  278. \n\
  279. The address of the last match is stored as the value of \"$_\".\n\
  280. Convenience variable \"$numfound\" is set to the number of matches."),
  281.            &cmdlist);
  282. }