[Lldb-commits] [lldb] [lldb] Add 'FindInMemory()' overload for PostMortemProcess. (PR #102536)

Kevin Frei via lldb-commits lldb-commits at lists.llvm.org
Tue Aug 13 09:58:30 PDT 2024


================
@@ -2835,6 +2835,34 @@ void PruneThreadPlans();
                               AddressRanges &matches, size_t alignment,
                               size_t max_matches);
 
+  template <typename IT>
+  lldb::addr_t FindInMemoryGeneric(IT &&iterator, lldb::addr_t low,
+                                   lldb::addr_t high, const uint8_t *buf,
+                                   size_t size) {
+    const size_t region_size = high - low;
+
+    if (region_size < size)
+      return LLDB_INVALID_ADDRESS;
+
+    std::vector<size_t> bad_char_heuristic(256, size);
+
+    for (size_t idx = 0; idx < size - 1; idx++) {
+      decltype(bad_char_heuristic)::size_type bcu_idx = buf[idx];
+      bad_char_heuristic[bcu_idx] = size - idx - 1;
+    }
+    for (size_t s = 0; s <= (region_size - size);) {
+      int64_t j = size - 1;
+      while (j >= 0 && buf[j] == iterator[s + j])
+        j--;
+      if (j < 0)
+        return low + s;
+      else
+        s += bad_char_heuristic[iterator[s + size - 1]];
+    }
+
+    return LLDB_INVALID_ADDRESS;
+  }
+
----------------
kevinfrei wrote:

This algorithm is probably a performance win, but obscures the simplicity of the function. Obscuring simplicity for good reasons requires an explanation. At least name drop BMH in the comments or link to the wikipedia page (https://en.wikipedia.org/wiki/Boyer%E2%80%93Moore%E2%80%93Horspool_algorithm)

https://github.com/llvm/llvm-project/pull/102536


More information about the lldb-commits mailing list