aboutsummaryrefslogtreecommitdiff
path: root/gdb/target.c
diff options
context:
space:
mode:
authorTom Tromey <tromey@adacore.com>2020-10-07 12:07:55 -0600
committerTom Tromey <tromey@adacore.com>2020-10-07 12:07:55 -0600
commit4a72de73660810536e9bb416d41dc8a6128f91da (patch)
tree4ada40b22e18b0ab7b64494c58d45c1492a76187 /gdb/target.c
parente86efb3310cf3aa9515cca07d0835b7e8af79b38 (diff)
downloadbinutils-4a72de73660810536e9bb416d41dc8a6128f91da.zip
binutils-4a72de73660810536e9bb416d41dc8a6128f91da.tar.gz
binutils-4a72de73660810536e9bb416d41dc8a6128f91da.tar.bz2
Move simple_search_memory to gdbsupport/search.cc
This moves the simple_search_memory function to a new file, gdbsupport/search.cc. The API is slightly changed to make it more general. This generality is useful for wiring it to gdbserver, and also for unit testing. gdb/ChangeLog 2020-10-07 Tom Tromey <tromey@adacore.com> * target.h (simple_search_memory): Don't declare. * target.c (simple_search_memory): Move to gdbsupport. (default_search_memory): Update. * remote.c (remote_target::search_memory): Update. gdbsupport/ChangeLog 2020-10-07 Tom Tromey <tromey@adacore.com> * Makefile.in: Rebuild. * Makefile.am (libgdbsupport_a_SOURCES): Add search.cc. * search.h: New file. * search.cc: New file.
Diffstat (limited to 'gdb/target.c')
-rw-r--r--gdb/target.c110
1 files changed, 8 insertions, 102 deletions
diff --git a/gdb/target.c b/gdb/target.c
index dd78a84..6fd60a1 100644
--- a/gdb/target.c
+++ b/gdb/target.c
@@ -47,6 +47,7 @@
#include "event-top.h"
#include <algorithm>
#include "gdbsupport/byte-vector.h"
+#include "gdbsupport/search.h"
#include "terminal.h"
#include <unordered_map>
#include "target-connection.h"
@@ -2147,106 +2148,6 @@ target_read_description (struct target_ops *target)
return target->read_description ();
}
-/* This implements a basic search of memory, reading target memory and
- performing the search here (as opposed to performing the search in on the
- target side with, for example, gdbserver). */
-
-int
-simple_search_memory (struct target_ops *ops,
- CORE_ADDR start_addr, ULONGEST search_space_len,
- const gdb_byte *pattern, ULONGEST pattern_len,
- CORE_ADDR *found_addrp)
-{
- /* NOTE: also defined in find.c testcase. */
-#define SEARCH_CHUNK_SIZE 16000
- const unsigned chunk_size = SEARCH_CHUNK_SIZE;
- /* Buffer to hold memory contents for searching. */
- unsigned search_buf_size;
-
- search_buf_size = chunk_size + pattern_len - 1;
-
- /* No point in trying to allocate a buffer larger than the search space. */
- if (search_space_len < search_buf_size)
- search_buf_size = search_space_len;
-
- gdb::byte_vector search_buf (search_buf_size);
-
- /* Prime the search buffer. */
-
- if (target_read (ops, TARGET_OBJECT_MEMORY, NULL,
- search_buf.data (), start_addr, search_buf_size)
- != search_buf_size)
- {
- warning (_("Unable to access %s bytes of target "
- "memory at %s, halting search."),
- pulongest (search_buf_size), hex_string (start_addr));
- return -1;
- }
-
- /* Perform the search.
-
- The loop is kept simple by allocating [N + pattern-length - 1] bytes.
- When we've scanned N bytes we copy the trailing bytes to the start and
- read in another N bytes. */
-
- while (search_space_len >= pattern_len)
- {
- gdb_byte *found_ptr;
- unsigned nr_search_bytes
- = std::min (search_space_len, (ULONGEST) search_buf_size);
-
- found_ptr = (gdb_byte *) memmem (search_buf.data (), nr_search_bytes,
- pattern, pattern_len);
-
- if (found_ptr != NULL)
- {
- CORE_ADDR found_addr = start_addr + (found_ptr - search_buf.data ());
-
- *found_addrp = found_addr;
- return 1;
- }
-
- /* Not found in this chunk, skip to next chunk. */
-
- /* Don't let search_space_len wrap here, it's unsigned. */
- if (search_space_len >= chunk_size)
- search_space_len -= chunk_size;
- else
- search_space_len = 0;
-
- if (search_space_len >= pattern_len)
- {
- unsigned keep_len = search_buf_size - chunk_size;
- CORE_ADDR read_addr = start_addr + chunk_size + keep_len;
- int nr_to_read;
-
- /* Copy the trailing part of the previous iteration to the front
- of the buffer for the next iteration. */
- gdb_assert (keep_len == pattern_len - 1);
- memcpy (&search_buf[0], &search_buf[chunk_size], keep_len);
-
- nr_to_read = std::min (search_space_len - keep_len,
- (ULONGEST) chunk_size);
-
- if (target_read (ops, TARGET_OBJECT_MEMORY, NULL,
- &search_buf[keep_len], read_addr,
- nr_to_read) != nr_to_read)
- {
- warning (_("Unable to access %s bytes of target "
- "memory at %s, halting search."),
- plongest (nr_to_read),
- hex_string (read_addr));
- return -1;
- }
-
- start_addr += chunk_size;
- }
- }
-
- /* Not found. */
-
- return 0;
-}
/* Default implementation of memory-searching. */
@@ -2256,9 +2157,14 @@ default_search_memory (struct target_ops *self,
const gdb_byte *pattern, ULONGEST pattern_len,
CORE_ADDR *found_addrp)
{
+ auto read_memory = [=] (CORE_ADDR addr, gdb_byte *result, size_t len)
+ {
+ return target_read (current_top_target (), TARGET_OBJECT_MEMORY, NULL,
+ result, addr, len) == len;
+ };
+
/* Start over from the top of the target stack. */
- return simple_search_memory (current_top_target (),
- start_addr, search_space_len,
+ return simple_search_memory (read_memory, start_addr, search_space_len,
pattern, pattern_len, found_addrp);
}