From eb6671e7c88cbe6d5b01b8dbdd38b9f925b2f41b Mon Sep 17 00:00:00 2001 From: Aaron Smith Date: Fri, 15 Feb 2019 04:32:50 +0000 Subject: [PATCH] Implement GetLoadAddress for the Windows process plugin Summary: When a process is loaded, update its sections with the load address to resolve any created breakpoints. For the remote debugging case, the debugged process is launched remotely so GetLoadAddress is intended to pass the load address from remote to LLDB (client). Reviewers: zturner, llvm-commits, clayborg, labath Reviewed By: labath Subscribers: mgorny, sas, Hui, clayborg, labath, lldb-commits Differential Revision: https://reviews.llvm.org/D56237 llvm-svn: 354099 --- .../Windows-DYLD/DynamicLoaderWindowsDYLD.cpp | 96 ++++++++++++++++--- .../Windows-DYLD/DynamicLoaderWindowsDYLD.h | 11 +++ .../Process/Windows/Common/CMakeLists.txt | 1 + .../Process/Windows/Common/ProcessWindows.cpp | 41 +++----- .../Process/Windows/Common/ProcessWindows.h | 3 + 5 files changed, 111 insertions(+), 41 deletions(-) diff --git a/lldb/source/Plugins/DynamicLoader/Windows-DYLD/DynamicLoaderWindowsDYLD.cpp b/lldb/source/Plugins/DynamicLoader/Windows-DYLD/DynamicLoaderWindowsDYLD.cpp index 727a4ae5cc4b..f45d7116dcfc 100644 --- a/lldb/source/Plugins/DynamicLoader/Windows-DYLD/DynamicLoaderWindowsDYLD.cpp +++ b/lldb/source/Plugins/DynamicLoader/Windows-DYLD/DynamicLoaderWindowsDYLD.cpp @@ -12,6 +12,7 @@ #include "lldb/Core/Module.h" #include "lldb/Core/PluginManager.h" #include "lldb/Target/ExecutionContext.h" +#include "lldb/Target/Platform.h" #include "lldb/Target/Process.h" #include "lldb/Target/RegisterContext.h" #include "lldb/Target/Target.h" @@ -61,6 +62,56 @@ DynamicLoader *DynamicLoaderWindowsDYLD::CreateInstance(Process *process, return nullptr; } +void DynamicLoaderWindowsDYLD::OnLoadModule(const ModuleSpec &module_spec, + lldb::addr_t module_addr) { + // Confusingly, there is no Target::AddSharedModule. Instead, calling + // GetSharedModule() with a new module will add it to the module list and + // return a corresponding ModuleSP. + Status error; + ModuleSP module_sp = + m_process->GetTarget().GetSharedModule(module_spec, &error); + if (error.Fail()) + return; + + m_loaded_modules[module_sp] = module_addr; + UpdateLoadedSectionsCommon(module_sp, module_addr, false); +} + +void DynamicLoaderWindowsDYLD::OnUnloadModule(lldb::addr_t module_addr) { + Address resolved_addr; + if (!m_process->GetTarget().ResolveLoadAddress(module_addr, resolved_addr)) + return; + + ModuleSP module_sp = resolved_addr.GetModule(); + if (module_sp) { + m_loaded_modules.erase(module_sp); + UnloadSectionsCommon(module_sp); + } +} + +lldb::addr_t DynamicLoaderWindowsDYLD::GetLoadAddress(ModuleSP executable) { + // First, see if the load address is already cached. + auto it = m_loaded_modules.find(executable); + if (it != m_loaded_modules.end() && it->second != LLDB_INVALID_ADDRESS) + return it->second; + + lldb::addr_t load_addr = LLDB_INVALID_ADDRESS; + + // Second, try to get it through the process plugins. For a remote process, + // the remote platform will be responsible for providing it. + FileSpec file_spec(executable->GetPlatformFileSpec()); + bool is_loaded = false; + Status status = + m_process->GetFileLoadAddress(file_spec, is_loaded, load_addr); + // Servers other than lldb server could respond with a bogus address. + if (status.Success() && is_loaded && load_addr != LLDB_INVALID_ADDRESS) { + m_loaded_modules[executable] = load_addr; + return load_addr; + } + + return LLDB_INVALID_ADDRESS; +} + void DynamicLoaderWindowsDYLD::DidAttach() { Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER)); if (log) @@ -73,21 +124,17 @@ void DynamicLoaderWindowsDYLD::DidAttach() { // Try to fetch the load address of the file from the process, since there // could be randomization of the load address. + lldb::addr_t load_addr = GetLoadAddress(executable); + if (load_addr == LLDB_INVALID_ADDRESS) + return; - // It might happen that the remote has a different dir for the file, so we - // only send the basename of the executable in the query. I think this is safe - // because I doubt that two executables with the same basenames are loaded in - // memory... - FileSpec file_spec( - executable->GetPlatformFileSpec().GetFilename().GetCString()); - bool is_loaded; - addr_t base_addr = 0; - lldb::addr_t load_addr; - Status error = m_process->GetFileLoadAddress(file_spec, is_loaded, load_addr); - if (error.Success() && is_loaded) { - base_addr = load_addr; - UpdateLoadedSections(executable, LLDB_INVALID_ADDRESS, base_addr, false); - } + // Request the process base address. + lldb::addr_t image_base = m_process->GetImageInfoAddress(); + if (image_base == load_addr) + return; + + // Rebase the process's modules if there is a mismatch. + UpdateLoadedSections(executable, LLDB_INVALID_ADDRESS, load_addr, false); ModuleList module_list; module_list.Append(executable); @@ -95,7 +142,26 @@ void DynamicLoaderWindowsDYLD::DidAttach() { m_process->LoadModules(); } -void DynamicLoaderWindowsDYLD::DidLaunch() {} +void DynamicLoaderWindowsDYLD::DidLaunch() { + Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER)); + if (log) + log->Printf("DynamicLoaderWindowsDYLD::%s()", __FUNCTION__); + + ModuleSP executable = GetTargetExecutable(); + if (!executable.get()) + return; + + lldb::addr_t load_addr = GetLoadAddress(executable); + if (load_addr != LLDB_INVALID_ADDRESS) { + // Update the loaded sections so that the breakpoints can be resolved. + UpdateLoadedSections(executable, LLDB_INVALID_ADDRESS, load_addr, false); + + ModuleList module_list; + module_list.Append(executable); + m_process->GetTarget().ModulesDidLoad(module_list); + m_process->LoadModules(); + } +} Status DynamicLoaderWindowsDYLD::CanLoadImage() { return Status(); } diff --git a/lldb/source/Plugins/DynamicLoader/Windows-DYLD/DynamicLoaderWindowsDYLD.h b/lldb/source/Plugins/DynamicLoader/Windows-DYLD/DynamicLoaderWindowsDYLD.h index 5eb390085731..ef859255db1a 100644 --- a/lldb/source/Plugins/DynamicLoader/Windows-DYLD/DynamicLoaderWindowsDYLD.h +++ b/lldb/source/Plugins/DynamicLoader/Windows-DYLD/DynamicLoaderWindowsDYLD.h @@ -12,6 +12,8 @@ #include "lldb/Target/DynamicLoader.h" #include "lldb/lldb-forward.h" +#include + namespace lldb_private { class DynamicLoaderWindowsDYLD : public DynamicLoader { @@ -27,6 +29,9 @@ public: static DynamicLoader *CreateInstance(Process *process, bool force); + void OnLoadModule(const ModuleSpec &module_spec, lldb::addr_t module_addr); + void OnUnloadModule(lldb::addr_t module_addr); + void DidAttach() override; void DidLaunch() override; Status CanLoadImage() override; @@ -35,6 +40,12 @@ public: ConstString GetPluginName() override; uint32_t GetPluginVersion() override; + +protected: + lldb::addr_t GetLoadAddress(lldb::ModuleSP executable); + +private: + std::map m_loaded_modules; }; } // namespace lldb_private diff --git a/lldb/source/Plugins/Process/Windows/Common/CMakeLists.txt b/lldb/source/Plugins/Process/Windows/Common/CMakeLists.txt index 092629086183..95ab64c1b83e 100644 --- a/lldb/source/Plugins/Process/Windows/Common/CMakeLists.txt +++ b/lldb/source/Plugins/Process/Windows/Common/CMakeLists.txt @@ -24,6 +24,7 @@ add_lldb_library(lldbPluginProcessWindowsCommon PLUGIN lldbCore lldbHost lldbInterpreter + lldbPluginDynamicLoaderWindowsDYLD lldbSymbol lldbTarget ws2_32 diff --git a/lldb/source/Plugins/Process/Windows/Common/ProcessWindows.cpp b/lldb/source/Plugins/Process/Windows/Common/ProcessWindows.cpp index fad7aa388bbe..93df360678ca 100644 --- a/lldb/source/Plugins/Process/Windows/Common/ProcessWindows.cpp +++ b/lldb/source/Plugins/Process/Windows/Common/ProcessWindows.cpp @@ -864,6 +864,13 @@ lldb::addr_t ProcessWindows::GetImageInfoAddress() { return LLDB_INVALID_ADDRESS; } +DynamicLoaderWindowsDYLD *ProcessWindows::GetDynamicLoader() { + if (m_dyld_ap.get() == NULL) + m_dyld_ap.reset(DynamicLoader::FindPlugin( + this, DynamicLoaderWindowsDYLD::GetPluginNameStatic().GetCString())); + return static_cast(m_dyld_ap.get()); +} + void ProcessWindows::OnExitProcess(uint32_t exit_code) { // No need to acquire the lock since m_session_data isn't accessed. Log *log = ProcessWindowsLog::GetLogIfAny(WINDOWS_LOG_PROCESS); @@ -916,12 +923,10 @@ void ProcessWindows::OnDebuggerConnected(lldb::addr_t image_base) { GetTarget().SetExecutableModule(module, eLoadDependentsNo); } - bool load_addr_changed; - module->SetLoadAddress(GetTarget(), image_base, false, load_addr_changed); - - ModuleList loaded_modules; - loaded_modules.Append(module); - GetTarget().ModulesDidLoad(loaded_modules); + if (auto dyld = GetDynamicLoader()) + dyld->OnLoadModule( + ModuleSpec(module->GetFileSpec(), module->GetArchitecture()), + image_base); // Add the main executable module to the list of pending module loads. We // can't call GetTarget().ModulesDidLoad() here because we still haven't @@ -1027,29 +1032,13 @@ void ProcessWindows::OnExitThread(lldb::tid_t thread_id, uint32_t exit_code) { void ProcessWindows::OnLoadDll(const ModuleSpec &module_spec, lldb::addr_t module_addr) { - // Confusingly, there is no Target::AddSharedModule. Instead, calling - // GetSharedModule() with a new module will add it to the module list and - // return a corresponding ModuleSP. - Status error; - ModuleSP module = GetTarget().GetSharedModule(module_spec, &error); - bool load_addr_changed = false; - module->SetLoadAddress(GetTarget(), module_addr, false, load_addr_changed); - - ModuleList loaded_modules; - loaded_modules.Append(module); - GetTarget().ModulesDidLoad(loaded_modules); + if (auto dyld = GetDynamicLoader()) + dyld->OnLoadModule(module_spec, module_addr); } void ProcessWindows::OnUnloadDll(lldb::addr_t module_addr) { - Address resolved_addr; - if (GetTarget().ResolveLoadAddress(module_addr, resolved_addr)) { - ModuleSP module = resolved_addr.GetModule(); - if (module) { - ModuleList unloaded_modules; - unloaded_modules.Append(module); - GetTarget().ModulesDidUnload(unloaded_modules, false); - } - } + if (auto dyld = GetDynamicLoader()) + dyld->OnUnloadModule(module_addr); } void ProcessWindows::OnDebugString(const std::string &string) {} diff --git a/lldb/source/Plugins/Process/Windows/Common/ProcessWindows.h b/lldb/source/Plugins/Process/Windows/Common/ProcessWindows.h index 184dab90ff3c..3ef6d56dd2eb 100644 --- a/lldb/source/Plugins/Process/Windows/Common/ProcessWindows.h +++ b/lldb/source/Plugins/Process/Windows/Common/ProcessWindows.h @@ -16,6 +16,7 @@ #include "llvm/Support/Mutex.h" #include "IDebugDelegate.h" +#include "Plugins/DynamicLoader/Windows-DYLD/DynamicLoaderWindowsDYLD.h" namespace lldb_private { @@ -90,6 +91,8 @@ public: lldb::addr_t GetImageInfoAddress() override; + DynamicLoaderWindowsDYLD *GetDynamicLoader() override; + // IDebugDelegate overrides. void OnExitProcess(uint32_t exit_code) override; void OnDebuggerConnected(lldb::addr_t image_base) override;