[lldb/Plugins] Add memory region support in ScriptedProcess

This patch adds support for memory regions in Scripted Processes.
This is necessary to read the stack memory region in order to
reconstruct each stackframe of the program.

In order to do so, this patch makes some changes to the SBAPI, namely:
- Add a new constructor for `SBMemoryRegionInfo` that takes arguments
  such as the memory region name, address range, permissions ...
  This is used when reading memory at some address to compute the offset
  in the binary blob provided by the user.
- Add a `GetMemoryRegionContainingAddress` method to `SBMemoryRegionInfoList`
  to simplify the access to a specific memory region.

With these changes, lldb is now able to unwind the stack and reconstruct
each frame. On top of that, reloading the target module at offset 0 allows
lldb to symbolicate the `ScriptedProcess` using debug info, similarly to an
ordinary Process.

To test this, I wrote a simple program with multiple function calls, ran it in
lldb, stopped at a leaf function and read the registers values and copied
the stack memory into a binary file. These are then used in the python script.

Differential Revision: https://reviews.llvm.org/D108953

Signed-off-by: Med Ismail Bennani <medismail.bennani@gmail.com>
This commit is contained in:
Med Ismail Bennani
2021-10-08 12:25:04 +00:00
parent 59d8dd79e1
commit a758c9f720
23 changed files with 340 additions and 48 deletions

View File

@@ -239,7 +239,7 @@ size_t ScriptedProcess::DoReadMemory(lldb::addr_t addr, void *buf, size_t size,
lldb::DataExtractorSP data_extractor_sp =
GetInterface().ReadMemoryAtAddress(addr, size, error);
if (!data_extractor_sp || error.Fail())
if (!data_extractor_sp || !data_extractor_sp->GetByteSize() || error.Fail())
return 0;
offset_t bytes_copied = data_extractor_sp->CopyByteOrderedData(
@@ -258,24 +258,34 @@ ArchSpec ScriptedProcess::GetArchitecture() {
Status ScriptedProcess::GetMemoryRegionInfo(lldb::addr_t load_addr,
MemoryRegionInfo &region) {
// TODO: Implement
return Status();
CheckInterpreterAndScriptObject();
Status error;
if (auto region_or_err =
GetInterface().GetMemoryRegionContainingAddress(load_addr, error))
region = *region_or_err;
return error;
}
Status ScriptedProcess::GetMemoryRegions(MemoryRegionInfos &region_list) {
CheckInterpreterAndScriptObject();
Status error;
lldb::addr_t address = 0;
lldb::MemoryRegionInfoSP mem_region_sp = nullptr;
while ((mem_region_sp =
GetInterface().GetMemoryRegionContainingAddress(address))) {
auto range = mem_region_sp->GetRange();
while (auto region_or_err =
GetInterface().GetMemoryRegionContainingAddress(address, error)) {
if (error.Fail())
break;
MemoryRegionInfo &mem_region = *region_or_err;
auto range = mem_region.GetRange();
address += range.GetRangeBase() + range.GetByteSize();
region_list.push_back(*mem_region_sp.get());
region_list.push_back(mem_region);
}
return {};
return error;
}
void ScriptedProcess::Clear() { Process::m_thread_list.Clear(); }