28#include "llvm/ADT/STLExtras.h"
29#include "llvm/ADT/SmallVector.h"
30#include "llvm/ADT/StringRef.h"
31#include "llvm/Support/Error.h"
43 llvm::StringRef plugin_name) {
45 if (!plugin_name.empty()) {
48 if (create_callback) {
49 std::unique_ptr<DynamicLoader> instance_up(
50 create_callback(process,
true));
52 return instance_up.release();
55 for (
auto create_callback :
57 std::unique_ptr<DynamicLoader> instance_up(
58 create_callback(process,
false));
60 return instance_up.release();
72 return m_process->GetStopOnSharedLibraryEvents();
76 m_process->SetStopOnSharedLibraryEvents(stop);
85 ModuleSpec module_spec(executable->GetFileSpec(),
86 executable->GetArchitecture());
87 auto module_sp = std::make_shared<Module>(module_spec);
95 if (module_sp && module_sp->GetUUID().IsValid() &&
96 executable->GetUUID().IsValid()) {
97 if (module_sp->GetUUID() != executable->GetUUID())
99 }
else if (executable->FileHasChanged()) {
118 bool base_addr_is_offset) {
124 bool base_addr_is_offset) {
126 module->SetLoadAddress(m_process->GetTarget(), base_addr, base_addr_is_offset,
138 assert(sections &&
"SectionList missing from unloaded module.");
140 const size_t num_sections = sections->
GetSize();
141 for (
size_t i = 0; i < num_sections; ++i) {
151 ObjectFile *obj_file =
module->GetObjectFile();
152 if (obj_file !=
nullptr) {
178 bool base_addr_is_offset) {
185 if (!module_sp && !
m_process->IsLiveDebugSession()) {
186 llvm::Expected<ModuleSP> memory_module_sp_or_err =
187 m_process->ReadModuleFromMemory(file, base_addr);
188 if (
auto err = memory_module_sp_or_err.takeError())
190 "Failed to read module from memory: {0}");
192 module_sp = *memory_module_sp_or_err;
193 m_process->GetTarget().GetImages().AppendIfNeeded(module_sp,
false);
198 base_addr_is_offset);
203 llvm::StringRef name) {
206 snprintf(namebuf,
sizeof(namebuf),
"memory-image-0x%" PRIx64, addr);
209 llvm::Expected<ModuleSP> module_sp_or_err =
211 if (
auto err = module_sp_or_err.takeError()) {
213 "Failed to read module from memory: {0}");
216 return *module_sp_or_err;
222 if (!bin_spec.
name.empty())
223 desc << bin_spec.
name <<
" ";
236 msg <<
"Unable to find file";
237 if (!bin_spec.
name.empty())
238 msg <<
" " << bin_spec.
name;
243 msg.
Printf(
" with slide 0x%" PRIx64, bin_spec.
value);
245 msg.
Printf(
" at address 0x%" PRIx64, bin_spec.
value);
253static std::optional<SymbolLocator::Request>
256 module_spec.
SetTarget(target.shared_from_this());
281 llvm::Expected<SymbolLocator::Result> located) {
286 llvm::Error
error = located.takeError();
288 llvm::consumeError(std::move(
error));
294 if (located->symbol_error)
304 if (!located_module_sp)
305 located_module_sp = std::make_shared<Module>(located->module_spec);
309 bin_spec.
module_sp = std::move(located_module_sp);
310 bin_spec.
module_sp->GetSymbolLocatorStatistics().merge(located->statistics);
322 Process *process, llvm::MutableArrayRef<BinarySpec> bin_specs) {
329 llvm::SmallVector<BinarySpec *> to_search;
330 std::vector<SymbolLocator::Request> requests;
332 if (!bin_spec.uuid.IsValid() && !bin_spec.value_is_offset)
334 if (!bin_spec.uuid.IsValid())
336 if (std::optional<SymbolLocator::Request> request =
338 to_search.push_back(&bin_spec);
339 requests.push_back(std::move(*request));
343 std::vector<llvm::Expected<SymbolLocator::Result>> located =
347 for (
auto [bin_spec, result] : llvm::zip_equal(to_search, located))
351llvm::Expected<ModuleSP>
374 llvm::Error
error = llvm::createStringError(message);
376 return llvm::joinErrors(std::move(search_error), std::move(
error));
377 return std::move(
error);
386 << llvm::toString(std::move(search_error)) <<
"\n";
394 bool changed =
false;
396 if (bin_spec.
module_sp->GetObjectFile()) {
399 "DynamicLoader::LoadBinaryInTarget Loading "
400 "binary %s UUID %s at %s 0x%" PRIx64,
410 "DynamicLoader::LoadBinaryInTarget Loading "
411 "binary %s UUID %s at file address",
413 bin_spec.
module_sp->SetLoadAddress(target, 0,
true ,
419 "DynamicLoader::LoadBinaryInTarget Loading binary "
420 "%s UUID %s from memory at address 0x%" PRIx64,
423 bin_spec.
module_sp->SetLoadAddress(target, 0,
true ,
437llvm::Expected<ModuleSP>
447 m_process->ReadUnsignedIntegerFromMemory(addr, size_in_bytes, 0,
error);
451 return (int64_t)value;
466 m_process->LoadOperatingSystemPlugin(flush);
static llvm::raw_ostream & error(Stream &strm)
static void FinishSearch(DynamicLoader::BinarySpec &bin_spec, llvm::Expected< SymbolLocator::Result > located)
The module is not registered with the Target until LoadBinaryInTarget.
static ModuleSP ReadUnnamedMemoryModule(Process *process, addr_t addr, llvm::StringRef name)
static void FindBinaryUUIDInMemory(Process *process, DynamicLoader::BinarySpec &bin_spec)
static std::string GetBinaryDescription(const DynamicLoader::BinarySpec &bin_spec)
static std::string GetBinaryNotFoundMessage(const DynamicLoader::BinarySpec &bin_spec)
static std::optional< SymbolLocator::Request > PrepareSearch(Target &target, DynamicLoader::BinarySpec &bin_spec)
Reads the Target, so it has to be called for one binary at a time.
#define LLDB_LOG(log,...)
The LLDB_LOG* macros defined below are the way to emit log messages.
#define LLDB_LOGF(log,...)
#define LLDB_LOG_ERROR(log, error,...)
bool IsValid() const
Tests if this ArchSpec is valid.
lldb::StreamUP GetAsyncErrorStream()
void LoadOperatingSystemPlugin(bool flush)
void SetStopWhenImagesChange(bool stop)
Set whether the process should stop when images change.
lldb::ModuleSP FindModuleViaTarget(const ModuleSpec &module_spec)
Find a module in the target that matches the given module spec.
int64_t ReadUnsignedIntWithSizeInBytes(lldb::addr_t addr, int size_in_bytes)
static llvm::Expected< lldb::ModuleSP > LocateAndLoadBinary(Process *process, BinarySpec &bin_spec)
Find a binary and load it into a Target.
lldb::addr_t ReadPointer(lldb::addr_t addr)
Process * m_process
The process that this dynamic loader plug-in is tracking.
void UpdateLoadedSectionsCommon(lldb::ModuleSP module, lldb::addr_t base_addr, bool base_addr_is_offset)
lldb::ModuleSP GetTargetExecutable()
Checks to see if the target module has changed, updates the target accordingly and returns the target...
bool GetStopWhenImagesChange() const
Get whether the process should stop when images change.
static void LocateBinaries(Process *process, llvm::MutableArrayRef< BinarySpec > bin_specs)
Search for a batch of binaries, without mutating the Target.
virtual lldb::ModuleSP LoadModuleAtAddress(const lldb_private::FileSpec &file, lldb::addr_t link_map_addr, lldb::addr_t base_addr, bool base_addr_is_offset)
Locates or creates a module given by file and updates/loads the resulting module at the virtual base ...
virtual void UpdateLoadedSections(lldb::ModuleSP module, lldb::addr_t link_map_addr, lldb::addr_t base_addr, bool base_addr_is_offset)
Updates the load address of every allocatable section in module.
DynamicLoader(Process *process)
Construct with a process.
const lldb_private::SectionList * GetSectionListFromModule(const lldb::ModuleSP module) const
static DynamicLoader * FindPlugin(Process *process, llvm::StringRef plugin_name)
Find a dynamic loader plugin for a given process.
static llvm::Expected< lldb::ModuleSP > LoadBinaryInTarget(Process *process, BinarySpec &bin_spec)
Add a binary that LocateBinaries searched for to the Target, and set its load address.
void UnloadSectionsCommon(const lldb::ModuleSP module)
virtual void UnloadSections(const lldb::ModuleSP module)
Removes the loaded sections from the target in module.
static FileSystem & Instance()
A collection class for Module objects.
static Status GetSharedModule(const ModuleSpec &module_spec, lldb::ModuleSP &module_sp, llvm::SmallVectorImpl< lldb::ModuleSP > *old_modules, bool *did_create_ptr, bool invoke_locate_callback=true, bool invoke_symbol_locators=true)
lldb::ModuleSP FindFirstModule(const ModuleSpec &module_spec) const
Finds the first module whose file specification matches module_spec.
bool AppendIfNeeded(const lldb::ModuleSP &new_module, bool notify=true)
Append a module to the module list, if it is not already there.
void Append(const lldb::ModuleSP &module_sp, bool notify=true)
Append a module to the module list.
void SetLoadAddress(lldb::addr_t addr)
Set the load address of a module in process memory.
void SetTarget(lldb::TargetSP target)
Set the target to be used when resolving a module.
A plug-in interface definition class for object file parsers.
virtual SectionList * GetSectionList(bool update_module_section_list=true)
Gets the section list for the currently selected architecture (and object for archives).
static DynamicLoaderCreateInstance GetDynamicLoaderCreateCallbackForPluginName(llvm::StringRef name)
static llvm::SmallVector< DynamicLoaderCreateInstance > GetDynamicLoaderCreateCallbacks()
A plug-in interface definition class for debugging a process.
llvm::Expected< lldb::ModuleSP > ReadModuleFromMemory(const FileSpec &file_spec, lldb::addr_t header_addr, size_t size_to_read=512)
Creates and populates a module using an in-memory object file.
Target & GetTarget()
Get the target object pointer for this module.
lldb::SectionSP GetSectionAtIndex(size_t idx) const
static Status FromError(llvm::Error error)
Avoid using this in new code. Migrate APIs to llvm::Expected instead.
llvm::StringRef GetString() const
size_t PutHex64(uint64_t uvalue, lldb::ByteOrder byte_order=lldb::eByteOrderInvalid)
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
A binary was not found and nothing could say why.
static llvm::Expected< Result > Locate(const Request &request, const FileSpecList &search_paths)
Find a binary and, if possible, its symbols.
bool GetParallelModuleLoad() const
void ModulesDidLoad(ModuleList &module_list)
This call may preload module symbols, and may do so in parallel depending on the following target set...
Module * GetExecutableModulePointer()
Debugger & GetDebugger() const
bool SetSectionUnloaded(const lldb::SectionSP §ion_sp)
lldb::ModuleSP GetOrCreateModule(const ModuleSpec &module_spec, bool notify, Status *error_ptr=nullptr)
Find a binary on the system and return its Module, or return an existing Module that is already in th...
bool SetArchitecture(const ArchSpec &arch_spec, bool set_platform=false, bool merge=true)
Set the architecture for this target.
lldb::ModuleSP GetExecutableModule()
Gets the module for the main executable.
static FileSpecList GetDefaultDebugFileSearchPaths()
lldb::PlatformSP GetPlatform()
const ModuleList & GetImages() const
Get accessor for the images for this process.
const ArchSpec & GetArchitecture() const
void SetExecutableModule(lldb::ModuleSP &module_sp, LoadDependentFiles load_dependent_files=eLoadDependentsDefault)
Set the main executable module.
std::string GetAsString(llvm::StringRef separator="-") const
#define LLDB_INVALID_ADDRESS
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
DynamicLoader *(* DynamicLoaderCreateInstance)(Process *process, bool force)
std::shared_ptr< lldb_private::Section > SectionSP
std::shared_ptr< lldb_private::Module > ModuleSP
A binary to find and load into a Target.
lldb::addr_t value
Address where the binary should be loaded, or read out of memory.
bool allow_memory_image_last_resort
If no better binary image can be found, allow reading the binary out of memory, if possible,...
UUID uuid
UUID of the binary to be loaded.
lldb::ModuleSP memory_module_sp
The binary as it was read out of the process' memory, if it had to be, so that it is not read a secon...
std::string name
Name of the binary, if available.
lldb::ModuleSP module_sp
The module found for the binary, or empty if it was not found.
Status error
What an external symbol server had to say about this binary.
bool force_symbol_search
Allow the search to do a possibly expensive external search for the ObjectFile and/or SymbolFile.
bool set_address_in_target
Whether the address of the binary should be set in the Target if it is added.
bool notify
Whether ModulesDidLoad should be called once the binary has been added to the Target.
bool value_is_offset
A flag indicating that value is an address, or an offset to be applied to the file addresses.
One binary to search for.
lldb::PlatformSP platform
A platform that may know where the binary is.
std::string description
How to name this binary in a progress report.
ModuleSpec module_spec
What to look for.
bool external_lookup
Allow contacting an external symbol server when the local searches come up empty.