Move simple_search_memory to gdbsupport/search.cc
authorTom Tromey <tromey@adacore.com>
Wed, 7 Oct 2020 18:07:55 +0000 (12:07 -0600)
committerTom Tromey <tromey@adacore.com>
Wed, 7 Oct 2020 18:07:55 +0000 (12:07 -0600)
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.

gdb/ChangeLog
gdb/remote.c
gdb/target.c
gdb/target.h
gdbsupport/ChangeLog
gdbsupport/Makefile.am
gdbsupport/Makefile.in
gdbsupport/search.cc [new file with mode: 0644]
gdbsupport/search.h [new file with mode: 0644]

index 48ab2e044e5a65d664279eb43260ec4661fc930d..0b92bfb6536e1cf90cc5644ce6244d6c7aa74b14 100644 (file)
@@ -1,3 +1,10 @@
+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.
+
 2020-10-07  Simon Marchi  <simon.marchi@efficios.com>
 
        * Makefile.in (COMPILE): Add CXXFLAGS.
index f95148643f81bc2ab09a7575d6fd076d1c6312f1..26ee28d39036fa52d5b49b420dc2433f340feb14 100644 (file)
@@ -75,6 +75,7 @@
 #include "gdbsupport/scoped_restore.h"
 #include "gdbsupport/environ.h"
 #include "gdbsupport/byte-vector.h"
+#include "gdbsupport/search.h"
 #include <algorithm>
 #include <unordered_map>
 #include "async-event.h"
@@ -11186,6 +11187,12 @@ remote_target::search_memory (CORE_ADDR start_addr, ULONGEST search_space_len,
   int found;
   ULONGEST found_addr;
 
+  auto read_memory = [=] (CORE_ADDR addr, gdb_byte *result, size_t len)
+    {
+      return (target_read (this, TARGET_OBJECT_MEMORY, NULL, result, addr, len)
+             == len);
+    };
+
   /* Don't go to the target if we don't have to.  This is done before
      checking packet_config_support to avoid the possibility that a
      success for this edge case means the facility works in
@@ -11205,7 +11212,7 @@ remote_target::search_memory (CORE_ADDR start_addr, ULONGEST search_space_len,
     {
       /* Target doesn't provided special support, fall back and use the
         standard support (copy memory and do the search here).  */
-      return simple_search_memory (this, start_addr, search_space_len,
+      return simple_search_memory (read_memory, start_addr, search_space_len,
                                   pattern, pattern_len, found_addrp);
     }
 
@@ -11237,7 +11244,7 @@ remote_target::search_memory (CORE_ADDR start_addr, ULONGEST search_space_len,
         supported.  If so, fall back to the simple way.  */
       if (packet_config_support (packet) == PACKET_DISABLE)
        {
-         return simple_search_memory (this, start_addr, search_space_len,
+         return simple_search_memory (read_memory, start_addr, search_space_len,
                                       pattern, pattern_len, found_addrp);
        }
       return -1;
index dd78a848caec3fa316cc81d31cbfea0c676421a6..6fd60a15144dea59aee802864e539e7482e97a3a 100644 (file)
@@ -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);
 }
 
index 695f1b2b7f6497185b40c28fed7698f7d9424534..854553357149c74c9418ba2a556426c67527c59a 100644 (file)
@@ -2129,14 +2129,6 @@ extern const struct target_desc *target_read_description (struct target_ops *);
 #define target_get_ada_task_ptid(lwp, tid) \
      (current_top_target ()->get_ada_task_ptid) (lwp,tid)
 
-/* Utility implementation of searching memory.  */
-extern 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);
-
 /* Main entry point for searching memory.  */
 extern int target_search_memory (CORE_ADDR start_addr,
                                  ULONGEST search_space_len,
index 05462e67d7ee45734d297733389c9115c734d5dc..adcc53abb48f218b5599555cb35629b33300d455 100644 (file)
@@ -1,3 +1,10 @@
+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.
+
 2020-10-02  Simon Marchi  <simon.marchi@polymtl.ca>
 
        * event-loop.h (debug_event_loop): New variable declaration.
index 9763c96cd3d6bffcdaa04223d685d49f9947ce73..9f4ec938b98a0e02152919d3316d0aed514ebbe5 100644 (file)
@@ -65,6 +65,7 @@ libgdbsupport_a_SOURCES = \
     run-time-clock.cc \
     safe-strerror.cc \
     scoped_mmap.cc \
+    search.cc \
     signals.cc \
     signals-state-save-restore.cc \
     tdesc.cc \
index 0223f6d6b3521335b5c6bf91d88a0c36033b8af9..044ef1555c276d1af78c828a56011b1a8409ffb3 100644 (file)
@@ -156,7 +156,7 @@ am_libgdbsupport_a_OBJECTS = agent.$(OBJEXT) btrace-common.$(OBJEXT) \
        netstuff.$(OBJEXT) new-op.$(OBJEXT) pathstuff.$(OBJEXT) \
        print-utils.$(OBJEXT) ptid.$(OBJEXT) rsp-low.$(OBJEXT) \
        run-time-clock.$(OBJEXT) safe-strerror.$(OBJEXT) \
-       scoped_mmap.$(OBJEXT) signals.$(OBJEXT) \
+       scoped_mmap.$(OBJEXT) search.$(OBJEXT) signals.$(OBJEXT) \
        signals-state-save-restore.$(OBJEXT) tdesc.$(OBJEXT) \
        thread-pool.$(OBJEXT) xml-utils.$(OBJEXT) $(am__objects_1)
 libgdbsupport_a_OBJECTS = $(am_libgdbsupport_a_OBJECTS)
@@ -388,6 +388,7 @@ libgdbsupport_a_SOURCES = \
     run-time-clock.cc \
     safe-strerror.cc \
     scoped_mmap.cc \
+    search.cc \
     signals.cc \
     signals-state-save-restore.cc \
     tdesc.cc \
@@ -492,6 +493,7 @@ distclean-compile:
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/run-time-clock.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/safe-strerror.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/scoped_mmap.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/search.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/selftest.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/signals-state-save-restore.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/signals.Po@am__quote@
diff --git a/gdbsupport/search.cc b/gdbsupport/search.cc
new file mode 100644 (file)
index 0000000..f10c570
--- /dev/null
@@ -0,0 +1,120 @@
+/* Target memory searching
+
+   Copyright (C) 2020 Free Software Foundation, Inc.
+
+   This file is part of GDB.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+#include "gdbsupport/common-defs.h"
+
+#include "gdbsupport/search.h"
+#include "gdbsupport/byte-vector.h"
+
+/* 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
+  (gdb::function_view<target_read_memory_ftype> read_memory,
+   CORE_ADDR start_addr, ULONGEST search_space_len,
+   const gdb_byte *pattern, ULONGEST pattern_len,
+   CORE_ADDR *found_addrp)
+{
+  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 (!read_memory (start_addr, search_buf.data (), 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);
+         if (keep_len > 0)
+           memcpy (&search_buf[0], &search_buf[chunk_size], keep_len);
+
+         nr_to_read = std::min (search_space_len - keep_len,
+                                (ULONGEST) chunk_size);
+
+         if (!read_memory (read_addr, &search_buf[keep_len], 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;
+}
diff --git a/gdbsupport/search.h b/gdbsupport/search.h
new file mode 100644 (file)
index 0000000..886d80f
--- /dev/null
@@ -0,0 +1,42 @@
+/* Copyright (C) 2020 Free Software Foundation, Inc.
+
+   This file is part of GDB.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+#ifndef COMMON_SEARCH_H
+#define COMMON_SEARCH_H
+
+#include "gdbsupport/function-view.h"
+
+/* This is needed by the unit test, so appears here.  */
+#define SEARCH_CHUNK_SIZE 16000
+
+/* The type of a callback function that can be used to read memory.
+   Note that target_read_memory is not used here, because gdbserver
+   wants to be able to examine trace data when searching, and
+   target_read_memory does not do this.  */
+
+typedef bool target_read_memory_ftype (CORE_ADDR, gdb_byte *, size_t);
+
+/* Utility implementation of searching memory.  */
+extern int simple_search_memory
+  (gdb::function_view<target_read_memory_ftype> read_memory,
+   CORE_ADDR start_addr,
+   ULONGEST search_space_len,
+   const gdb_byte *pattern,
+   ULONGEST pattern_len,
+   CORE_ADDR *found_addrp);
+
+#endif /* COMMON_SEARCH_H */