Пример #1
0
static void
find_command (char *args, int from_tty)
{
  struct gdbarch *gdbarch = get_current_arch ();
  bfd_boolean big_p = gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG;
  /* Command line parameters.
     These are initialized to avoid uninitialized warnings from -Wall.  */
  ULONGEST max_count = 0;
  char *pattern_buf = 0;
  ULONGEST pattern_len = 0;
  CORE_ADDR start_addr = 0;
  ULONGEST search_space_len = 0;
  /* End of command line parameters.  */
  unsigned int found_count;
  CORE_ADDR last_found_addr;
  struct cleanup *old_cleanups;

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

  old_cleanups = make_cleanup (free_current_contents, &pattern_buf);

  /* Perform the search.  */

  found_count = 0;
  last_found_addr = 0;

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

      if (found <= 0)
	break;

      print_address (gdbarch, found_addr, gdb_stdout);
      printf_filtered ("\n");
      ++found_count;
      last_found_addr = found_addr;

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

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

  /* Record and print the results.  */

  set_internalvar_integer (lookup_internalvar ("numfound"), found_count);
  if (found_count > 0)
    {
      struct type *ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
      set_internalvar (lookup_internalvar ("_"),
		       value_from_pointer (ptr_type, last_found_addr));
    }

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

  do_cleanups (old_cleanups);
}
Пример #2
0
/* Implementation of
   gdb.search_memory (address, length, pattern).  ADDRESS is the
   address to start the search.  LENGTH specifies the scope of the
   search from ADDRESS.  PATTERN is the pattern to search for (and
   must be a Python object supporting the buffer protocol).
   Returns a Python Long object holding the address where the pattern
   was located, or if the pattern was not found, returns None.  */
static PyObject *
infpy_search_memory (PyObject *self, PyObject *args, PyObject *kw)
{
    CORE_ADDR start_addr, length;
    static char *keywords[] = { "address", "length", "pattern", NULL };
    PyObject *pattern, *start_addr_obj, *length_obj;
    volatile struct gdb_exception except;
    Py_ssize_t pattern_size;
    const void *buffer;
    CORE_ADDR found_addr;
    int found = 0;

    if (! PyArg_ParseTupleAndKeywords (args, kw, "OOO", keywords,
                                       &start_addr_obj, &length_obj,
                                       &pattern))
        return NULL;

    if (get_addr_from_python (start_addr_obj, &start_addr)
            && get_addr_from_python (length_obj, &length))
    {
        if (!length)
        {
            PyErr_SetString (PyExc_ValueError,
                             _("Search range is empty."));
            return NULL;
        }
        /* Watch for overflows.  */
        else if (length > CORE_ADDR_MAX
                 || (start_addr + length - 1) < start_addr)
        {
            PyErr_SetString (PyExc_ValueError,
                             _("The search range is too large."));

            return NULL;
        }
    }
    else
    {
        PyErr_SetString (PyExc_RuntimeError,
                         _("Cannot get search address/range from Python."));

        return NULL;
    }

    if (!PyObject_CheckReadBuffer (pattern))
    {
        PyErr_SetString (PyExc_RuntimeError,
                         _("The pattern is not a Python buffer."));

        return NULL;
    }

    if (PyObject_AsReadBuffer (pattern, &buffer, &pattern_size) == -1)
        return NULL;

    TRY_CATCH (except, RETURN_MASK_ALL)
    {
        found = target_search_memory (start_addr, length,
                                      buffer, pattern_size,
                                      &found_addr);
    }