search.cc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /* Target memory searching
  2. Copyright (C) 2020-2022 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 "gdbsupport/common-defs.h"
  15. #include "gdbsupport/search.h"
  16. #include "gdbsupport/byte-vector.h"
  17. /* This implements a basic search of memory, reading target memory and
  18. performing the search here (as opposed to performing the search in on the
  19. target side with, for example, gdbserver). */
  20. int
  21. simple_search_memory
  22. (gdb::function_view<target_read_memory_ftype> read_memory,
  23. CORE_ADDR start_addr, ULONGEST search_space_len,
  24. const gdb_byte *pattern, ULONGEST pattern_len,
  25. CORE_ADDR *found_addrp)
  26. {
  27. const unsigned chunk_size = SEARCH_CHUNK_SIZE;
  28. /* Buffer to hold memory contents for searching. */
  29. unsigned search_buf_size;
  30. search_buf_size = chunk_size + pattern_len - 1;
  31. /* No point in trying to allocate a buffer larger than the search space. */
  32. if (search_space_len < search_buf_size)
  33. search_buf_size = search_space_len;
  34. gdb::byte_vector search_buf (search_buf_size);
  35. /* Prime the search buffer. */
  36. if (!read_memory (start_addr, search_buf.data (), search_buf_size))
  37. {
  38. warning (_("Unable to access %s bytes of target "
  39. "memory at %s, halting search."),
  40. pulongest (search_buf_size), hex_string (start_addr));
  41. return -1;
  42. }
  43. /* Perform the search.
  44. The loop is kept simple by allocating [N + pattern-length - 1] bytes.
  45. When we've scanned N bytes we copy the trailing bytes to the start and
  46. read in another N bytes. */
  47. while (search_space_len >= pattern_len)
  48. {
  49. gdb_byte *found_ptr;
  50. unsigned nr_search_bytes
  51. = std::min (search_space_len, (ULONGEST) search_buf_size);
  52. found_ptr = (gdb_byte *) memmem (search_buf.data (), nr_search_bytes,
  53. pattern, pattern_len);
  54. if (found_ptr != NULL)
  55. {
  56. CORE_ADDR found_addr = start_addr + (found_ptr - search_buf.data ());
  57. *found_addrp = found_addr;
  58. return 1;
  59. }
  60. /* Not found in this chunk, skip to next chunk. */
  61. /* Don't let search_space_len wrap here, it's unsigned. */
  62. if (search_space_len >= chunk_size)
  63. search_space_len -= chunk_size;
  64. else
  65. search_space_len = 0;
  66. if (search_space_len >= pattern_len)
  67. {
  68. unsigned keep_len = search_buf_size - chunk_size;
  69. CORE_ADDR read_addr = start_addr + chunk_size + keep_len;
  70. int nr_to_read;
  71. /* Copy the trailing part of the previous iteration to the front
  72. of the buffer for the next iteration. */
  73. gdb_assert (keep_len == pattern_len - 1);
  74. if (keep_len > 0)
  75. memcpy (&search_buf[0], &search_buf[chunk_size], keep_len);
  76. nr_to_read = std::min (search_space_len - keep_len,
  77. (ULONGEST) chunk_size);
  78. if (!read_memory (read_addr, &search_buf[keep_len], nr_to_read))
  79. {
  80. warning (_("Unable to access %s bytes of target "
  81. "memory at %s, halting search."),
  82. plongest (nr_to_read),
  83. hex_string (read_addr));
  84. return -1;
  85. }
  86. start_addr += chunk_size;
  87. }
  88. }
  89. /* Not found. */
  90. return 0;
  91. }