[lldb/Plugins] Add ability to load modules to Scripted Processes

This patch introduces a new way to load modules programatically with
Scripted Processes. To do so, the scripted process blueprint holds a
list of dictionary describing the modules to load, which their path or
uuid, load address and eventually a slide offset.

LLDB will fetch that list after launching the ScriptedProcess, and
iterate over each entry to create the module that will be loaded in the
Scripted Process' target.

The patch also refactors the StackCoreScriptedProcess test to stop
inside the `libbaz` module and make sure it's loaded correctly and that
we can fetch some variables from it.

rdar://74520238

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

Signed-off-by: Med Ismail Bennani <medismail.bennani@gmail.com>
This commit is contained in:
Med Ismail Bennani
2022-03-04 11:22:32 -08:00
parent 6eddd987c9
commit 680ca7f21a
12 changed files with 189 additions and 29 deletions

View File

@@ -170,6 +170,7 @@ Status ScriptedProcess::DoLaunch(Module *exe_module,
void ScriptedProcess::DidLaunch() {
CheckInterpreterAndScriptObject();
m_pid = GetInterface().GetProcessID();
GetLoadedDynamicLibrariesInfos();
}
Status ScriptedProcess::DoResume() {
@@ -378,6 +379,93 @@ bool ScriptedProcess::GetProcessInfo(ProcessInstanceInfo &info) {
return true;
}
lldb_private::StructuredData::ObjectSP
ScriptedProcess::GetLoadedDynamicLibrariesInfos() {
CheckInterpreterAndScriptObject();
Status error;
auto error_with_message = [&error](llvm::StringRef message) {
return ScriptedInterface::ErrorWithMessage<bool>(LLVM_PRETTY_FUNCTION,
message.data(), error);
};
StructuredData::ArraySP loaded_images_sp = GetInterface().GetLoadedImages();
if (!loaded_images_sp || !loaded_images_sp->GetSize())
return GetInterface().ErrorWithMessage<StructuredData::ObjectSP>(
LLVM_PRETTY_FUNCTION, "No loaded images.", error);
ModuleList module_list;
Target &target = GetTarget();
auto reload_image = [&target, &module_list, &error_with_message](
StructuredData::Object *obj) -> bool {
StructuredData::Dictionary *dict = obj->GetAsDictionary();
if (!dict)
return error_with_message("Couldn't cast image object into dictionary.");
ModuleSpec module_spec;
llvm::StringRef value;
bool has_path = dict->HasKey("path");
bool has_uuid = dict->HasKey("uuid");
if (!has_path && !has_uuid)
return error_with_message("Dictionary should have key 'path' or 'uuid'");
if (!dict->HasKey("load_addr"))
return error_with_message("Dictionary is missing key 'load_addr'");
if (has_path) {
dict->GetValueForKeyAsString("path", value);
module_spec.GetFileSpec().SetPath(value);
}
if (has_uuid) {
dict->GetValueForKeyAsString("uuid", value);
module_spec.GetUUID().SetFromStringRef(value);
}
module_spec.GetArchitecture() = target.GetArchitecture();
ModuleSP module_sp =
target.GetOrCreateModule(module_spec, true /* notify */);
if (!module_sp)
return error_with_message("Couldn't create or get module.");
lldb::addr_t load_addr = LLDB_INVALID_ADDRESS;
lldb::addr_t slide = LLDB_INVALID_OFFSET;
dict->GetValueForKeyAsInteger("load_addr", load_addr);
dict->GetValueForKeyAsInteger("slide", slide);
if (load_addr == LLDB_INVALID_ADDRESS)
return error_with_message(
"Couldn't get valid load address or slide offset.");
if (slide != LLDB_INVALID_OFFSET)
load_addr += slide;
bool changed = false;
module_sp->SetLoadAddress(target, load_addr, false /*=value_is_offset*/,
changed);
if (!changed && !module_sp->GetObjectFile())
return error_with_message("Couldn't set the load address for module.");
dict->GetValueForKeyAsString("path", value);
FileSpec objfile(value);
module_sp->SetFileSpecAndObjectName(objfile, objfile.GetFilename());
return module_list.AppendIfNeeded(module_sp);
};
if (!loaded_images_sp->ForEach(reload_image))
return GetInterface().ErrorWithMessage<StructuredData::ObjectSP>(
LLVM_PRETTY_FUNCTION, "Couldn't reload all images.", error);
target.ModulesDidLoad(module_list);
return loaded_images_sp;
}
ScriptedProcessInterface &ScriptedProcess::GetInterface() const {
return m_interpreter->GetScriptedProcessInterface();
}