Skip to content

Commit c14b6df

Browse files
author
Rahul Reddy Chamala
committed
Ignore module load errors by default and create placeholder modules for missing modules
1 parent 45b45e0 commit c14b6df

File tree

3 files changed

+54
-59
lines changed

3 files changed

+54
-59
lines changed

lldb/source/Plugins/Process/scripted/ScriptedProcess.cpp

Lines changed: 53 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
#include "lldb/Utility/ScriptedMetadata.h"
2626
#include "lldb/Utility/State.h"
2727

28+
#include "Plugins/ObjectFile/Placeholder/ObjectFilePlaceholder.h"
29+
2830
#include <mutex>
2931

3032
LLDB_PLUGIN_DEFINE(ScriptedProcess)
@@ -422,11 +424,9 @@ bool ScriptedProcess::GetProcessInfo(ProcessInstanceInfo &info) {
422424
lldb_private::StructuredData::ObjectSP
423425
ScriptedProcess::GetLoadedDynamicLibrariesInfos() {
424426
Status error;
425-
auto handle_error_with_message = [&error](llvm::StringRef message,
426-
bool ignore_error) {
427-
ScriptedInterface::ErrorWithMessage<bool>(LLVM_PRETTY_FUNCTION,
428-
message.data(), error);
429-
return ignore_error;
427+
auto error_with_message = [&error](llvm::StringRef message) {
428+
return ScriptedInterface::ErrorWithMessage<bool>(LLVM_PRETTY_FUNCTION,
429+
message.data(), error);
430430
};
431431

432432
StructuredData::ArraySP loaded_images_sp = GetInterface().GetLoadedImages();
@@ -438,79 +438,94 @@ ScriptedProcess::GetLoadedDynamicLibrariesInfos() {
438438
ModuleList module_list;
439439
Target &target = GetTarget();
440440

441-
auto reload_image = [&target, &module_list, &handle_error_with_message](
441+
auto reload_image = [&target, &module_list, &error_with_message](
442442
StructuredData::Object *obj) -> bool {
443443
StructuredData::Dictionary *dict = obj->GetAsDictionary();
444444

445445
if (!dict)
446-
return handle_error_with_message(
447-
"Couldn't cast image object into dictionary.", false);
446+
return error_with_message("Couldn't cast image object into dictionary.");
448447

449448
ModuleSpec module_spec;
450449
llvm::StringRef value;
451450

452451
bool has_path = dict->HasKey("path");
453452
bool has_uuid = dict->HasKey("uuid");
454453
if (!has_path && !has_uuid)
455-
return handle_error_with_message(
456-
"Dictionary should have key 'path' or 'uuid'", false);
454+
return error_with_message("Dictionary should have key 'path' or 'uuid'");
457455
if (!dict->HasKey("load_addr"))
458-
return handle_error_with_message("Dictionary is missing key 'load_addr'",
459-
false);
456+
return error_with_message("Dictionary is missing key 'load_addr'");
460457

458+
llvm::StringRef path = "";
461459
if (has_path) {
462-
dict->GetValueForKeyAsString("path", value);
463-
module_spec.GetFileSpec().SetPath(value);
460+
dict->GetValueForKeyAsString("path", path);
461+
module_spec.GetFileSpec().SetPath(path);
464462
}
465463

466464
if (has_uuid) {
467465
dict->GetValueForKeyAsString("uuid", value);
468466
module_spec.GetUUID().SetFromStringRef(value);
469467
}
470-
module_spec.GetArchitecture() = target.GetArchitecture();
471-
472-
ModuleSP module_sp =
473-
target.GetOrCreateModule(module_spec, true /* notify */);
474-
475-
bool ignore_module_load_error = false;
476-
dict->GetValueForKeyAsBoolean("ignore_module_load_error",
477-
ignore_module_load_error);
478-
if (!module_sp)
479-
return handle_error_with_message("Couldn't create or get module.",
480-
ignore_module_load_error);
481468

482469
lldb::addr_t load_addr = LLDB_INVALID_ADDRESS;
483470
lldb::offset_t slide = LLDB_INVALID_OFFSET;
484471
dict->GetValueForKeyAsInteger("load_addr", load_addr);
485472
dict->GetValueForKeyAsInteger("slide", slide);
486473
if (load_addr == LLDB_INVALID_ADDRESS)
487-
return handle_error_with_message(
488-
"Couldn't get valid load address or slide offset.",
489-
ignore_module_load_error);
474+
return error_with_message(
475+
"Couldn't get valid load address or slide offset.");
490476

491477
if (slide != LLDB_INVALID_OFFSET)
492478
load_addr += slide;
493479

480+
module_spec.GetArchitecture() = target.GetArchitecture();
481+
482+
ModuleSP module_sp =
483+
target.GetOrCreateModule(module_spec, true /* notify */);
484+
485+
bool is_placeholder_module = false;
486+
487+
if (!module_sp) {
488+
// Create a placeholder module
489+
LLDB_LOGF(
490+
GetLog(LLDBLog::Process),
491+
"ScriptedProcess::%s unable to locate the matching "
492+
"object file path %s, creating a placeholder module at 0x%" PRIx64,
493+
__FUNCTION__, path.str().c_str(), load_addr);
494+
495+
module_sp = Module::CreateModuleFromObjectFile<ObjectFilePlaceholder>(
496+
module_spec, load_addr, module_spec.GetFileSpec().MemorySize());
497+
498+
is_placeholder_module = true;
499+
}
500+
494501
bool changed = false;
495502
module_sp->SetLoadAddress(target, load_addr, false /*=value_is_offset*/,
496503
changed);
497504

498505
if (!changed && !module_sp->GetObjectFile())
499-
return handle_error_with_message(
500-
"Couldn't set the load address for module.",
501-
ignore_module_load_error);
506+
return error_with_message("Couldn't set the load address for module.");
502507

503-
dict->GetValueForKeyAsString("path", value);
504-
FileSpec objfile(value);
508+
FileSpec objfile(path);
505509
module_sp->SetFileSpecAndObjectName(objfile, objfile.GetFilename());
506510

507-
return ignore_module_load_error ? true
508-
: module_list.AppendIfNeeded(module_sp);
511+
if (is_placeholder_module) {
512+
target.GetImages().AppendIfNeeded(module_sp, true /*notify=*/);
513+
return true;
514+
}
515+
516+
return module_list.AppendIfNeeded(module_sp);
509517
};
510518

511-
if (!loaded_images_sp->ForEach(reload_image))
512-
return ScriptedInterface::ErrorWithMessage<StructuredData::ObjectSP>(
513-
LLVM_PRETTY_FUNCTION, "Couldn't reload all images.", error);
519+
size_t loaded_images_size = loaded_images_sp->GetSize();
520+
bool print_error = true;
521+
for (size_t idx = 0; idx < loaded_images_size; idx++) {
522+
const auto &loaded_image = loaded_images_sp->GetItemAtIndex(idx);
523+
if (!reload_image(loaded_image.get()) && print_error) {
524+
print_error = false;
525+
ScriptedInterface::ErrorWithMessage<StructuredData::ObjectSP>(
526+
LLVM_PRETTY_FUNCTION, "Couldn't reload all images.", error);
527+
}
528+
}
514529

515530
target.ModulesDidLoad(module_list);
516531

lldb/test/API/functionalities/scripted_process/TestStackCoreScriptedProcess.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@ def cleanup():
9696
{
9797
"path": "/random/path/random.dylib",
9898
"load_addr": 12345678,
99-
"ignore_module_load_error": True,
10099
},
101100
],
102101
}

lldb/test/API/functionalities/scripted_process/stack_core_scripted_process.py

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -69,25 +69,6 @@ def __init__(self, exe_ctx: lldb.SBExecutionContext, args: lldb.SBStructuredData
6969
return
7070

7171
module_path = module_path_arg.GetStringValue(100)
72-
module_name = os.path.basename(module_path)
73-
74-
# Get ignore_module_load_error boolean from args
75-
ignore_module_load_error = False
76-
ignore_module_load_error_arg = custom_module.GetValueForKey(
77-
"ignore_module_load_error"
78-
)
79-
if (
80-
ignore_module_load_error_arg
81-
and ignore_module_load_error_arg.IsValid()
82-
and ignore_module_load_error_arg.GetType()
83-
== lldb.eStructuredDataTypeBoolean
84-
):
85-
ignore_module_load_error = (
86-
ignore_module_load_error_arg.GetBooleanValue()
87-
)
88-
89-
if not os.path.exists(module_path) and not ignore_module_load_error:
90-
return
9172

9273
# Get custom module load address from args
9374
module_load_addr = None
@@ -102,6 +83,7 @@ def __init__(self, exe_ctx: lldb.SBExecutionContext, args: lldb.SBStructuredData
10283

10384
# If module load address is not specified/valid, try to find it from corefile module
10485
if module_load_addr is None:
86+
module_name = os.path.basename(module_path)
10587
corefile_module = self.get_module_with_name(
10688
self.corefile_target, module_name
10789
)
@@ -119,7 +101,6 @@ def __init__(self, exe_ctx: lldb.SBExecutionContext, args: lldb.SBStructuredData
119101
{
120102
"path": module_path,
121103
"load_addr": module_load_addr,
122-
"ignore_module_load_error": ignore_module_load_error,
123104
}
124105
)
125106

0 commit comments

Comments
 (0)