[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 10:28:59 PDT 2024


================
@@ -0,0 +1,31 @@
+import lldb
+
+SINGLE_INSTANCE_PATTERN = "there_is_only_one_of_me"
+DOUBLE_INSTANCE_PATTERN = "there_is_exactly_two_of_me"
+
+
+def GetAddressRanges(test_base):
+    mem_regions = test_base.process.GetMemoryRegions()
+    test_base.assertTrue(len(mem_regions) > 0, "Make sure there are memory regions")
+    addr_ranges = lldb.SBAddressRangeList()
+    for i in range(mem_regions.GetSize()):
+        region_info = lldb.SBMemoryRegionInfo()
+        if not mem_regions.GetMemoryRegionAtIndex(i, region_info):
+            continue
+        if not (region_info.IsReadable() and region_info.IsWritable()):
+            continue
+        if region_info.IsExecutable():
+            continue
+        if not region_info.GetName() or region_info.GetName() != "[heap]":
----------------
clayborg wrote:

We can't rely on `[heap]` being the memory region name, this won't work on macOS:
```
(lldb) p (void *)malloc(12)
(void *) 0x00006000005dc000
(lldb) memory region 0x00006000005dc000
[0x0000600000000000-0x0000600020000000) rw-
```
if you want memory regions that contain data from the heap, I would run malloc as an expression, then find the memory region it exists in by using the expression result:
```
>>> v = lldb.frame.EvaluateExpression('(void *)malloc(16)')
>>> v.GetValueAsUnsigned()
105553122394112
>>> v.GetValue()
'0x00006000005d8000'
```
Then you can use the `v.GetValueAsUnsigned()` to find the right memory region.

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


More information about the lldb-commits mailing list