[nfc][lldb] Move FastSearch from CommandObjectMemoryFind to Process (#93688)

Moving CommandObjectMemoryFind::FastSearch() to Process::FindInMemory(). Plan to expose FindInMemory as public API in SBProcess.
This commit is contained in:
Miro Bucko
2024-05-30 00:37:57 +07:00
committed by GitHub
parent 8c5a7a1fc4
commit 265589785c
3 changed files with 78 additions and 59 deletions

View File

@@ -112,6 +112,33 @@ public:
}
};
class ProcessMemoryIterator {
public:
ProcessMemoryIterator(Process &process, lldb::addr_t base)
: m_process(process), m_base_addr(base) {}
bool IsValid() { return m_is_valid; }
uint8_t operator[](lldb::addr_t offset) {
if (!IsValid())
return 0;
uint8_t retval = 0;
Status error;
if (0 == m_process.ReadMemory(m_base_addr + offset, &retval, 1, error)) {
m_is_valid = false;
return 0;
}
return retval;
}
private:
Process &m_process;
const lldb::addr_t m_base_addr;
bool m_is_valid = true;
};
static constexpr OptionEnumValueElement g_follow_fork_mode_values[] = {
{
eFollowParent,
@@ -3191,6 +3218,33 @@ Status Process::Halt(bool clear_thread_plans, bool use_run_lock) {
return Status();
}
lldb::addr_t Process::FindInMemory(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);
ProcessMemoryIterator iterator(*this, low);
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;
}
Status Process::StopForDestroyOrDetach(lldb::EventSP &exit_event_sp) {
Status error;