Skip to content

Allow option to ignore module load errors in ScriptedProcess #127153

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 46 additions & 18 deletions lldb/source/Plugins/Process/scripted/ScriptedProcess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
#include "lldb/Utility/ScriptedMetadata.h"
#include "lldb/Utility/State.h"

#include "Plugins/ObjectFile/Placeholder/ObjectFilePlaceholder.h"

#include <mutex>

LLDB_PLUGIN_DEFINE(ScriptedProcess)
Expand Down Expand Up @@ -165,7 +167,7 @@ Status ScriptedProcess::DoLoadCore() {
Status ScriptedProcess::DoLaunch(Module *exe_module,
ProcessLaunchInfo &launch_info) {
LLDB_LOGF(GetLog(LLDBLog::Process), "ScriptedProcess::%s launching process", __FUNCTION__);

/* MARK: This doesn't reflect how lldb actually launches a process.
In reality, it attaches to debugserver, then resume the process.
That's not true in all cases. If debugserver is remote, lldb
Expand Down Expand Up @@ -444,7 +446,6 @@ ScriptedProcess::GetLoadedDynamicLibrariesInfos() {
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");
Expand All @@ -453,22 +454,17 @@ ScriptedProcess::GetLoadedDynamicLibrariesInfos() {
if (!dict->HasKey("load_addr"))
return error_with_message("Dictionary is missing key 'load_addr'");

llvm::StringRef path = "";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you're using a new StringRef here for path, I'd rename the StringRef used to get UUID accordingly instead of leaving the value variable name.

if (has_path) {
dict->GetValueForKeyAsString("path", value);
module_spec.GetFileSpec().SetPath(value);
dict->GetValueForKeyAsString("path", path);
module_spec.GetFileSpec().SetPath(path);
}

llvm::StringRef uuid = "";
if (has_uuid) {
dict->GetValueForKeyAsString("uuid", value);
module_spec.GetUUID().SetFromStringRef(value);
dict->GetValueForKeyAsString("uuid", uuid);
module_spec.GetUUID().SetFromStringRef(uuid);
}
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::offset_t slide = LLDB_INVALID_OFFSET;
Expand All @@ -481,23 +477,55 @@ ScriptedProcess::GetLoadedDynamicLibrariesInfos() {
if (slide != LLDB_INVALID_OFFSET)
load_addr += slide;

module_spec.GetArchitecture() = target.GetArchitecture();

ModuleSP module_sp =
target.GetOrCreateModule(module_spec, true /* notify */);

bool is_placeholder_module = false;

if (!module_sp) {
// Create a placeholder module
LLDB_LOGF(
GetLog(LLDBLog::Process),
"ScriptedProcess::%s unable to locate the matching "
"object file path %s, creating a placeholder module at 0x%" PRIx64,
__FUNCTION__, path.str().c_str(), load_addr);

module_sp = Module::CreateModuleFromObjectFile<ObjectFilePlaceholder>(
module_spec, load_addr, module_spec.GetFileSpec().MemorySize());

is_placeholder_module = true;
}

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);
FileSpec objfile(path);
module_sp->SetFileSpecAndObjectName(objfile, objfile.GetFilename());

if (is_placeholder_module) {
target.GetImages().AppendIfNeeded(module_sp, true /*notify=*/);
return true;
}

return module_list.AppendIfNeeded(module_sp);
};

if (!loaded_images_sp->ForEach(reload_image))
return ScriptedInterface::ErrorWithMessage<StructuredData::ObjectSP>(
LLVM_PRETTY_FUNCTION, "Couldn't reload all images.", error);
size_t loaded_images_size = loaded_images_sp->GetSize();
bool print_error = true;
for (size_t idx = 0; idx < loaded_images_size; idx++) {
const auto &loaded_image = loaded_images_sp->GetItemAtIndex(idx);
if (!reload_image(loaded_image.get()) && print_error) {
print_error = false;
ScriptedInterface::ErrorWithMessage<StructuredData::ObjectSP>(
LLVM_PRETTY_FUNCTION, "Couldn't reload all images.", error);
}
}

target.ModulesDidLoad(module_list);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,28 @@ def cleanup():
)
self.assertTrue(corefile_process, PROCESS_IS_VALID)

# Create a random lib which does not exist in the corefile.
random_dylib = self.get_module_with_name(corefile_target, "random.dylib")
self.assertFalse(
random_dylib, "Dynamic library random.dylib should not be found."
)

structured_data = lldb.SBStructuredData()
structured_data.SetFromJSON(
json.dumps(
{
"backing_target_idx": self.dbg.GetIndexOfTarget(
corefile_process.GetTarget()
),
"libbaz_path": self.getBuildArtifact("libbaz.dylib"),
"custom_modules": [
{
"path": self.getBuildArtifact("libbaz.dylib"),
},
{
"path": "/random/path/random.dylib",
"load_addr": 12345678,
},
],
}
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,63 @@ def __init__(self, exe_ctx: lldb.SBExecutionContext, args: lldb.SBStructuredData
if len(self.threads) == 2:
self.threads[len(self.threads) - 1].is_stopped = True

corefile_module = self.get_module_with_name(
self.corefile_target, "libbaz.dylib"
)
if not corefile_module or not corefile_module.IsValid():
return
module_path = os.path.join(
corefile_module.GetFileSpec().GetDirectory(),
corefile_module.GetFileSpec().GetFilename(),
)
if not os.path.exists(module_path):
return
module_load_addr = corefile_module.GetObjectFileHeaderAddress().GetLoadAddress(
self.corefile_target
)
custom_modules = args.GetValueForKey("custom_modules")
if custom_modules.GetType() == lldb.eStructuredDataTypeArray:
for id in range(custom_modules.GetSize()):

custom_module = custom_modules.GetItemAtIndex(id)
if (
not custom_module
or not custom_module.IsValid()
or not custom_module.GetType() == lldb.eStructuredDataTypeDictionary
):
continue

# Get custom module path from args
module_path_arg = custom_module.GetValueForKey("path")
module_path = None
if (
not module_path_arg
or not module_path_arg.IsValid()
or not module_path_arg.GetType() == lldb.eStructuredDataTypeString
):
continue

module_path = module_path_arg.GetStringValue(100)

# Get custom module load address from args
module_load_addr = None
module_load_addr_arg = custom_module.GetValueForKey("load_addr")
if (
module_load_addr_arg
and module_load_addr_arg.IsValid()
and module_load_addr_arg.GetType()
== lldb.eStructuredDataTypeInteger
):
module_load_addr = module_load_addr_arg.GetIntegerValue()

# If module load address is not specified/valid, try to find it from corefile module
if module_load_addr is None:
module_name = os.path.basename(module_path)
corefile_module = self.get_module_with_name(
self.corefile_target, module_name
)

self.loaded_images.append({"path": module_path, "load_addr": module_load_addr})
if not corefile_module or not corefile_module.IsValid():
continue

module_load_addr = (
corefile_module.GetObjectFileHeaderAddress().GetLoadAddress(
self.corefile_target
)
)

self.loaded_images.append(
{
"path": module_path,
"load_addr": module_load_addr,
}
)

def get_memory_region_containing_address(
self, addr: int
Expand Down
Loading