| //===----------------------------------------------------------------------===// | |
| // | |
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | |
| // See https://llvm.org/LICENSE.txt for license information. | |
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | |
| // | |
| //===----------------------------------------------------------------------===// | |
| #include "DynamicLoaderAcceleratorGDBRemote.h" | |
| #include "Plugins/Process/gdb-remote/ProcessGDBRemote.h" | |
| #include "lldb/Core/Module.h" | |
| #include "lldb/Core/PluginManager.h" | |
| #include "lldb/Core/Section.h" | |
| #include "lldb/Target/Target.h" | |
| #include "lldb/Target/TargetGroup.h" | |
| #include "lldb/Utility/DataBufferHeap.h" | |
| #include "lldb/Utility/DataExtractor.h" | |
| #include "lldb/Utility/LLDBLog.h" | |
| #include "lldb/Utility/Log.h" | |
| using namespace lldb; | |
| using namespace lldb_private; | |
| using namespace lldb_private::process_gdb_remote; | |
| LLDB_PLUGIN_DEFINE(DynamicLoaderAcceleratorGDBRemote) | |
| DynamicLoader * | |
| DynamicLoaderAcceleratorGDBRemote::CreateInstance(Process *process, | |
| bool force) { | |
| // The library list comes from the accelerator's GDB server. | |
| if (!force || | |
| process->GetPluginName() != ProcessGDBRemote::GetPluginNameStatic()) | |
| return nullptr; | |
| return new DynamicLoaderAcceleratorGDBRemote(process); | |
| } | |
| DynamicLoaderAcceleratorGDBRemote::DynamicLoaderAcceleratorGDBRemote( | |
| Process *process) | |
| : DynamicLoader(process) {} | |
| void DynamicLoaderAcceleratorGDBRemote::DidAttach() { | |
| LoadModulesFromGDBServer(/*full=*/true); | |
| } | |
| void DynamicLoaderAcceleratorGDBRemote::DidLaunch() { | |
| LoadModulesFromGDBServer(/*full=*/true); | |
| } | |
| bool DynamicLoaderAcceleratorGDBRemote::HandleStopReasonDynamicLoader() { | |
| return LoadModulesFromGDBServer(/*full=*/false); | |
| } | |
| bool DynamicLoaderAcceleratorGDBRemote::LoadModulesFromGDBServer(bool full) { | |
| Log *log = GetLog(LLDBLog::DynamicLoader); | |
| // CreateInstance ensures that m_process is a ProcessGDBRemote. | |
| ProcessGDBRemote *accelerator_process = | |
| static_cast<ProcessGDBRemote *>(m_process); | |
| // Check jLLDBSettings to determine whether to send the library request on | |
| // the accelerator GDB remote connection or on the native connection, which | |
| // forwards it to the selected LLDBServerAcceleratorPlugin. | |
| ProcessGDBRemote *send_process = accelerator_process; | |
| AcceleratorDynamicLoaderArgs args; | |
| args.full = full; | |
| Target &target = m_process->GetTarget(); | |
| std::shared_ptr<TargetGroupCPUAndAccelerators> group_sp = | |
| target.GetFirstTargetGroupOfType<TargetGroupCPUAndAccelerators>(); | |
| TargetSP native_target_sp = group_sp ? group_sp->GetCPUTarget() : nullptr; | |
| ProcessSP native_process_sp = | |
| native_target_sp ? native_target_sp->GetProcessSP() : nullptr; | |
| std::optional<LLDBSettings> process_settings = | |
| accelerator_process->GetGDBRemote().GetLLDBSettings(); | |
| if (process_settings && !process_settings->send_dyld_packet_to_accelerator) { | |
| if (process_settings->accelerator_plugin_name.empty()) { | |
| LLDB_LOG(log, "cannot route dynamic-loader request through the native " | |
| "connection without an accelerator plugin name"); | |
| return false; | |
| } | |
| if (!native_process_sp || native_process_sp->GetPluginName() != | |
| ProcessGDBRemote::GetPluginNameStatic()) { | |
| LLDB_LOG(log, "cannot route dynamic-loader request through the native " | |
| "connection: no native gdb-remote process"); | |
| return false; | |
| } | |
| send_process = static_cast<ProcessGDBRemote *>(native_process_sp.get()); | |
| args.plugin_name = process_settings->accelerator_plugin_name; | |
| } | |
| ModuleList loaded_module_list; | |
| std::optional<AcceleratorDynamicLoaderResponse> response = | |
| send_process->GetGDBRemote().GetAcceleratorDynamicLoaderLibraryInfos( | |
| args); | |
| if (!response) { | |
| LLDB_LOG(log, "failed to get dynamic loader info from the GDB server"); | |
| return false; | |
| } | |
| for (const AcceleratorDynamicLoaderLibraryInfo &info : | |
| response->library_infos) { | |
| std::shared_ptr<DataBufferHeap> data_sp; | |
| if (info.load && info.native_memory_address && info.native_memory_size) { | |
| LLDB_LOG(log, "reading \"{0}\" from native memory at {1:x}", | |
| info.pathname, *info.native_memory_address); | |
| if (native_process_sp) { | |
| data_sp = std::make_shared<DataBufferHeap>(*info.native_memory_size, 0); | |
| Status error; | |
| const size_t bytes_read = native_process_sp->ReadMemory( | |
| *info.native_memory_address, data_sp->GetBytes(), | |
| data_sp->GetByteSize(), error); | |
| if (bytes_read != *info.native_memory_size) { | |
| LLDB_LOG(log, | |
| "failed to read \"{0}\" from native memory at {1:x}: {2}", | |
| info.pathname, *info.native_memory_address, error); | |
| data_sp.reset(); | |
| } | |
| } else { | |
| LLDB_LOG(log, "no native process available to read \"{0}\" at {1:x}", | |
| info.pathname, *info.native_memory_address); | |
| } | |
| } | |
| UUID uuid; | |
| if (info.uuid_str) | |
| uuid.SetFromStringRef(*info.uuid_str); | |
| DataExtractorSP extractor_sp; | |
| if (data_sp) | |
| extractor_sp = std::make_shared<DataExtractor>(data_sp); | |
| ModuleSpec module_spec(FileSpec(info.pathname), uuid, extractor_sp); | |
| // Restrict the object to a slice of the containing file, when provided. | |
| if (info.file_offset) | |
| module_spec.SetObjectOffset(*info.file_offset); | |
| if (info.file_size) | |
| module_spec.SetObjectSize(*info.file_size); | |
| if (!info.load) { | |
| ModuleList matching_module_list; | |
| target.GetImages().FindModules(module_spec, matching_module_list); | |
| matching_module_list.ForEach( | |
| [this](const ModuleSP &module_sp) -> IterationAction { | |
| UnloadSections(module_sp); | |
| return IterationAction::Continue; | |
| }); | |
| target.ModulesDidUnload(matching_module_list, | |
| /*delete_locations=*/false); | |
| continue; | |
| } | |
| ModuleSP module_sp = | |
| target.GetOrCreateModule(module_spec, /*notify=*/false); | |
| if (!module_sp) | |
| continue; | |
| bool changed = false; | |
| if (info.load_address) { | |
| module_sp->SetLoadAddress(target, *info.load_address, | |
| /*value_is_offset=*/true, changed); | |
| } else if (!info.loaded_sections.empty()) { | |
| for (const AcceleratorSectionInfo § : info.loaded_sections) { | |
| if (sect.names.empty()) | |
| continue; | |
| SectionSP section_sp; | |
| for (const std::string &name : sect.names) { | |
| ConstString section_name(name); | |
| if (section_sp) | |
| section_sp = | |
| section_sp->GetChildren().FindSectionByName(section_name); | |
| else | |
| section_sp = | |
| module_sp->GetSectionList()->FindSectionByName(section_name); | |
| if (!section_sp) | |
| break; | |
| } | |
| if (section_sp) | |
| changed |= target.SetSectionLoadAddress(section_sp, sect.load_address, | |
| /*warn_multiple=*/true); | |
| } | |
| } else { | |
| // No slide: load at the file addresses. | |
| module_sp->SetLoadAddress(target, 0, /*value_is_offset=*/true, changed); | |
| } | |
| if (changed) | |
| loaded_module_list.AppendIfNeeded(module_sp); | |
| } | |
| target.ModulesDidLoad(loaded_module_list); | |
| return true; | |
| } | |
| ThreadPlanSP DynamicLoaderAcceleratorGDBRemote::GetStepThroughTrampolinePlan( | |
| Thread &thread, bool stop_others) { | |
| return ThreadPlanSP(); | |
| } | |
| Status DynamicLoaderAcceleratorGDBRemote::CanLoadImage() { | |
| return Status::FromErrorString("can't load images on accelerator targets"); | |
| } | |
| void DynamicLoaderAcceleratorGDBRemote::Initialize() { | |
| PluginManager::RegisterPlugin(GetPluginNameStatic(), | |
| GetPluginDescriptionStatic(), CreateInstance); | |
| } | |
| void DynamicLoaderAcceleratorGDBRemote::Terminate() { | |
| PluginManager::UnregisterPlugin(CreateInstance); | |
| } | |
| llvm::StringRef | |
| DynamicLoaderAcceleratorGDBRemote::GetPluginDescriptionStatic() { | |
| return "Dynamic loader plug-in that gets shared library loads/unloads from " | |
| "an lldb-server accelerator plugin."; | |
| } |