[Lldb-commits] [lldb] [lldb][API] Add Find(Ranges)InMemory() to Process SB API (PR #95007)

Greg Clayton via lldb-commits lldb-commits at lists.llvm.org
Mon Jun 10 14:04:32 PDT 2024


================
@@ -2007,6 +2007,124 @@ size_t Process::ReadMemory(addr_t addr, void *buf, size_t size, Status &error) {
   }
 }
 
+void Process::DoFindInMemory(lldb::addr_t start_addr, lldb::addr_t end_addr,
+                             const uint8_t *buf, size_t size,
+                             AddressRanges &matches, size_t alignment,
+                             size_t max_count) {
+  // Inputs are already validated in FindInMemory() functions.
+  assert(buf != nullptr);
+  assert(size > 0);
+  assert(alignment > 0);
+  assert(max_count > 0);
+  assert(start_addr != LLDB_INVALID_ADDRESS);
+  assert(end_addr != LLDB_INVALID_ADDRESS);
+  assert(start_addr < end_addr);
+
+  lldb::addr_t start = start_addr;
+  while (matches.size() < max_count && (start + size) < end_addr) {
+    const lldb::addr_t found_addr = FindInMemory(start, end_addr, buf, size);
+    if (found_addr == LLDB_INVALID_ADDRESS)
+      break;
+    matches.emplace_back(found_addr, size);
+    start = found_addr + alignment;
+  }
+}
+
+AddressRanges Process::FindRangesInMemory(const uint8_t *buf, uint64_t size,
+                                          const AddressRanges &ranges,
+                                          size_t alignment, size_t max_count,
+                                          Status &error) {
+  AddressRanges matches;
+  if (buf == nullptr) {
+    error.SetErrorStringWithFormat("buffer is null");
+    return matches;
+  }
+  if (size == 0) {
+    error.SetErrorStringWithFormat("size is zero");
+    return matches;
+  }
+  if (ranges.empty()) {
+    error.SetErrorStringWithFormat("ranges in empty");
+    return matches;
+  }
+  if (alignment == 0) {
+    error.SetErrorStringWithFormat(
+        "invalid alignment %zu, must be greater than 0", alignment);
+    return matches;
+  }
+  if (max_count == 0) {
+    error.SetErrorStringWithFormat(
+        "invalid max_count %zu, must be greater than 0", max_count);
+    return matches;
+  }
+
+  Target &target = GetTarget();
+  Log *log = GetLog(LLDBLog::Process);
+  for (size_t i = 0; i < ranges.size(); ++i) {
+    if (matches.size() >= max_count) {
+      break;
+    }
+    const AddressRange &range = ranges[i];
+    if (range.IsValid() == false) {
+      LLDB_LOGF(log, "Process::%s range is invalid", __FUNCTION__);
----------------
clayborg wrote:

Remove the `"Process::%s"` since this is a user facing error. We might want to still fill in the error here with the first error:
```
if (error.Success())
  error.SetErrorStringWithFormat("invalid range: ...". range...)
```
But we should keep going and still find matches.

We will need to get the address range string that looks like "a.out[0x1000-0x2000)" and use that in the error string. 

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


More information about the lldb-commits mailing list