Skip to content

Commit 2a1a024

Browse files
authored
Make SBMemoryRegionInfoList iterable with Python SWIG (llvm#117358)
This PR fixes a simple SWIG issue with SBMemoryRegionInfoList not being iterable out-of-the-box. This is mostly because of limitations to the `lldb_iter` function, which doesn't allow for specifying arguments to the size / iter functions passed. Before: ``` (lldb) script Python Interactive Interpreter. To exit, type 'quit()', 'exit()' or Ctrl-D. >>> for region in lldb.process.GetMemoryRegions(): ... print(region) ... Traceback (most recent call last): File "<console>", line 1, in <module> File "/opt/llvm/stable/Toolchains/llvm-sand.xctoolchain/usr/lib/python3.10/site-packages/lldb/__init__.py", line 114, in lldb_iter yield elem(i) TypeError: SBMemoryRegionInfoList.GetMemoryRegionAtIndex() missing 1 required positional argument: 'region_info' ``` After: ``` (lldb) script Python Interactive Interpreter. To exit, type 'quit()', 'exit()' or Ctrl-D. >>> for region in lldb.process.GetMemoryRegions(): ... print(region) ... [0x0000000000200000-0x00000000002cf000 R--] [0x00000000002cf000-0x0000000000597000 R-X] [0x0000000000597000-0x00000000005ad000 R--] [0x00000000005ad000-0x00000000005b1000 RW-] [0x00000000005b1000-0x0000000000b68000 RW-] [0x000000007fff7000-0x000000008fff7000 RW-] [0x000002008fff7000-0x000010007fff8000 RW-] [0x0000503000000000-0x0000503000010000 RW-] [0x0000503e00000000-0x0000503e00010000 RW-] [0x0000504000000000-0x0000504000010000 RW-] [0x0000504e00000000-0x0000504e00010000 RW-] [0x000050d000000000-0x000050d000010000 RW-] [0x000050de00000000-0x000050de00010000 RW-] [0x000050e000000000-0x000050e000010000 RW-] [0x000050ee00000000-0x000050ee00010000 RW-] [0x0000511000000000-0x0000511000010000 RW-] [0x0000511e00000000-0x0000511e00010000 RW-] [0x0000513000000000-0x0000513000010000 RW-] ... ```
1 parent e8b9e13 commit 2a1a024

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

lldb/bindings/interface/SBMemoryRegionInfoListExtensions.i

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@
77

88
def __iter__(self):
99
'''Iterate over all the memory regions in a lldb.SBMemoryRegionInfoList object.'''
10-
return lldb_iter(self, 'GetSize', 'GetMemoryRegionAtIndex')
10+
import lldb
11+
size = self.GetSize()
12+
region = lldb.SBMemoryRegionInfo()
13+
for i in range(size):
14+
self.GetMemoryRegionAtIndex(i, region)
15+
yield region
1116
%}
1217
#endif
1318
}

lldb/test/API/python_api/find_in_memory/TestFindInMemory.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,3 +152,16 @@ def test_find_in_memory_unaligned(self):
152152
)
153153
self.assertSuccess(error)
154154
self.assertEqual(addr, lldb.LLDB_INVALID_ADDRESS)
155+
156+
def test_memory_info_list_iterable(self):
157+
"""Make sure the SBMemoryRegionInfoList is iterable"""
158+
self.assertTrue(self.process, PROCESS_IS_VALID)
159+
self.assertState(self.process.GetState(), lldb.eStateStopped, PROCESS_STOPPED)
160+
161+
info_list = self.process.GetMemoryRegions()
162+
self.assertTrue(info_list.GetSize() > 0)
163+
try:
164+
for info in info_list:
165+
pass
166+
except Exception:
167+
self.fail("SBMemoryRegionInfoList is not iterable")

0 commit comments

Comments
 (0)