From: Tom Tromey <tromey@adacore.com>
To: gdb-patches@sourceware.org
Cc: Tom Tromey <tromey@adacore.com>
Subject: [PATCH v2 3/6] Use simple_search_memory in gdbserver
Date: Fri, 25 Sep 2020 13:49:10 -0600 [thread overview]
Message-ID: <20200925194913.1744541-4-tromey@adacore.com> (raw)
In-Reply-To: <20200925194913.1744541-1-tromey@adacore.com>
This replaces gdbserver's memory-searching function with
simple_search_memory.
gdbserver/ChangeLog
2020-09-25 Tom Tromey <tromey@adacore.com>
* server.cc (handle_search_memory_1): Remove.
(handle_search_memory): Use simple_search_memory.
---
gdbserver/ChangeLog | 5 ++
gdbserver/server.cc | 113 +++-----------------------------------------
2 files changed, 11 insertions(+), 107 deletions(-)
diff --git a/gdbserver/server.cc b/gdbserver/server.cc
index d45154d1f54..1c21660027e 100644
--- a/gdbserver/server.cc
+++ b/gdbserver/server.cc
@@ -49,6 +49,7 @@
#include "gdbsupport/scope-exit.h"
#include "gdbsupport/gdb_select.h"
#include "gdbsupport/scoped_restore.h"
+#include "gdbsupport/search.h"
#define require_running_or_return(BUF) \
if (!target_running ()) \
@@ -1038,89 +1039,6 @@ gdb_write_memory (CORE_ADDR memaddr, const unsigned char *myaddr, int len)
}
}
-/* Subroutine of handle_search_memory to simplify it. */
-
-static int
-handle_search_memory_1 (CORE_ADDR start_addr, CORE_ADDR search_space_len,
- gdb_byte *pattern, unsigned pattern_len,
- gdb_byte *search_buf,
- unsigned chunk_size, unsigned search_buf_size,
- CORE_ADDR *found_addrp)
-{
- /* Prime the search buffer. */
-
- if (gdb_read_memory (start_addr, search_buf, search_buf_size)
- != search_buf_size)
- {
- warning ("Unable to access %ld bytes of target "
- "memory at 0x%lx, halting search.",
- (long) search_buf_size, (long) 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 = (search_space_len < search_buf_size
- ? search_space_len
- : search_buf_size);
-
- found_ptr = (gdb_byte *) memmem (search_buf, nr_search_bytes, pattern,
- pattern_len);
-
- if (found_ptr != NULL)
- {
- CORE_ADDR found_addr = start_addr + (found_ptr - search_buf);
- *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. */
- memcpy (search_buf, search_buf + chunk_size, keep_len);
-
- nr_to_read = (search_space_len - keep_len < chunk_size
- ? search_space_len - keep_len
- : chunk_size);
-
- if (gdb_read_memory (read_addr, search_buf + keep_len,
- nr_to_read) != nr_to_read)
- {
- warning ("Unable to access %ld bytes of target memory "
- "at 0x%lx, halting search.",
- (long) nr_to_read, (long) read_addr);
- return -1;
- }
-
- start_addr += chunk_size;
- }
- }
-
- /* Not found. */
-
- return 0;
-}
-
/* Handle qSearch:memory packets. */
static void
@@ -1130,12 +1048,6 @@ handle_search_memory (char *own_buf, int packet_len)
CORE_ADDR search_space_len;
gdb_byte *pattern;
unsigned int pattern_len;
- /* 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. */
- gdb_byte *search_buf;
- unsigned search_buf_size;
int found;
CORE_ADDR found_addr;
int cmd_name_len = sizeof ("qSearch:memory:") - 1;
@@ -1158,25 +1070,13 @@ handle_search_memory (char *own_buf, int packet_len)
return;
}
- 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;
-
- search_buf = (gdb_byte *) malloc (search_buf_size);
- if (search_buf == NULL)
+ auto read_memory = [] (CORE_ADDR addr, gdb_byte *result, size_t len)
{
- free (pattern);
- error ("Unable to allocate memory to perform the search");
- strcpy (own_buf, "E00");
- return;
- }
+ return gdb_read_memory (addr, result, len) == len;
+ };
- found = handle_search_memory_1 (start_addr, search_space_len,
- pattern, pattern_len,
- search_buf, chunk_size, search_buf_size,
- &found_addr);
+ found = simple_search_memory (read_memory, start_addr, search_space_len,
+ pattern, pattern_len, &found_addr);
if (found > 0)
sprintf (own_buf, "1,%lx", (long) found_addr);
@@ -1185,7 +1085,6 @@ handle_search_memory (char *own_buf, int packet_len)
else
strcpy (own_buf, "E00");
- free (search_buf);
free (pattern);
}
--
2.26.2
next prev parent reply other threads:[~2020-09-25 19:49 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-09-25 19:49 [PATCH v2 0/6] Share memory-searching code Tom Tromey
2020-09-25 19:49 ` [PATCH v2 1/6] Rename some tests in find.exp Tom Tromey
2020-09-25 19:49 ` [PATCH v2 2/6] Move simple_search_memory to gdbsupport/search.cc Tom Tromey
2020-09-25 19:49 ` Tom Tromey [this message]
2020-09-25 19:49 ` [PATCH v2 4/6] Remove some dead code from handle_search_memory Tom Tromey
2020-09-28 15:13 ` Christian Biesinger
2020-09-28 19:46 ` Tom Tromey
2020-09-28 20:11 ` Christian Biesinger
2020-10-07 18:06 ` Tom Tromey
2020-09-25 19:49 ` [PATCH v2 5/6] Document inclusive range in help for "find" Tom Tromey
2020-09-25 19:49 ` [PATCH v2 6/6] Add simple_search_memory unit tests Tom Tromey
2020-10-07 18:07 ` [PATCH v2 0/6] Share memory-searching code Tom Tromey
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20200925194913.1744541-4-tromey@adacore.com \
--to=tromey@adacore.com \
--cc=gdb-patches@sourceware.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).