diff options
author | Dimitry Andric <dim@FreeBSD.org> | 2019-08-20 18:01:57 +0000 |
---|---|---|
committer | Dimitry Andric <dim@FreeBSD.org> | 2019-08-20 18:01:57 +0000 |
commit | 88c643b6fec27eec436c8d138fee6346e92337d6 (patch) | |
tree | 82cd13b2f3cde1c9e5f79689ba4e6ba67694843f /source/Plugins/DynamicLoader | |
parent | 94994d372d014ce4c8758b9605d63fae651bd8aa (diff) |
Notes
Diffstat (limited to 'source/Plugins/DynamicLoader')
15 files changed, 0 insertions, 5408 deletions
diff --git a/source/Plugins/DynamicLoader/CMakeLists.txt b/source/Plugins/DynamicLoader/CMakeLists.txt deleted file mode 100644 index 9f3b2ab0e50f..000000000000 --- a/source/Plugins/DynamicLoader/CMakeLists.txt +++ /dev/null @@ -1,6 +0,0 @@ -add_subdirectory(Darwin-Kernel) -add_subdirectory(MacOSX-DYLD) -add_subdirectory(POSIX-DYLD) -add_subdirectory(Static) -add_subdirectory(Hexagon-DYLD) -add_subdirectory(Windows-DYLD) diff --git a/source/Plugins/DynamicLoader/Darwin-Kernel/CMakeLists.txt b/source/Plugins/DynamicLoader/Darwin-Kernel/CMakeLists.txt deleted file mode 100644 index ffc797b7475b..000000000000 --- a/source/Plugins/DynamicLoader/Darwin-Kernel/CMakeLists.txt +++ /dev/null @@ -1,13 +0,0 @@ -add_lldb_library(lldbPluginDynamicLoaderDarwinKernel PLUGIN - DynamicLoaderDarwinKernel.cpp - - LINK_LIBS - lldbBreakpoint - lldbCore - lldbHost - lldbInterpreter - lldbSymbol - lldbTarget - lldbUtility - lldbPluginPlatformMacOSX - ) diff --git a/source/Plugins/DynamicLoader/Darwin-Kernel/DynamicLoaderDarwinKernel.cpp b/source/Plugins/DynamicLoader/Darwin-Kernel/DynamicLoaderDarwinKernel.cpp deleted file mode 100644 index 3a80c68dd4d7..000000000000 --- a/source/Plugins/DynamicLoader/Darwin-Kernel/DynamicLoaderDarwinKernel.cpp +++ /dev/null @@ -1,1583 +0,0 @@ -//===-- DynamicLoaderDarwinKernel.cpp -----------------------------*- C++ -//-*-===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// - -#include "Plugins/Platform/MacOSX/PlatformDarwinKernel.h" -#include "lldb/Breakpoint/StoppointCallbackContext.h" -#include "lldb/Core/Debugger.h" -#include "lldb/Core/Module.h" -#include "lldb/Core/ModuleSpec.h" -#include "lldb/Core/PluginManager.h" -#include "lldb/Core/Section.h" -#include "lldb/Core/StreamFile.h" -#include "lldb/Host/Symbols.h" -#include "lldb/Interpreter/OptionValueProperties.h" -#include "lldb/Symbol/ObjectFile.h" -#include "lldb/Target/OperatingSystem.h" -#include "lldb/Target/RegisterContext.h" -#include "lldb/Target/StackFrame.h" -#include "lldb/Target/Target.h" -#include "lldb/Target/Thread.h" -#include "lldb/Target/ThreadPlanRunToAddress.h" -#include "lldb/Utility/DataBuffer.h" -#include "lldb/Utility/DataBufferHeap.h" -#include "lldb/Utility/Log.h" -#include "lldb/Utility/State.h" - -#include "DynamicLoaderDarwinKernel.h" - -//#define ENABLE_DEBUG_PRINTF // COMMENT THIS LINE OUT PRIOR TO CHECKIN -#ifdef ENABLE_DEBUG_PRINTF -#include <stdio.h> -#define DEBUG_PRINTF(fmt, ...) printf(fmt, ##__VA_ARGS__) -#else -#define DEBUG_PRINTF(fmt, ...) -#endif - -using namespace lldb; -using namespace lldb_private; - -// Progressively greater amounts of scanning we will allow For some targets -// very early in startup, we can't do any random reads of memory or we can -// crash the device so a setting is needed that can completely disable the -// KASLR scans. - -enum KASLRScanType { - eKASLRScanNone = 0, // No reading into the inferior at all - eKASLRScanLowgloAddresses, // Check one word of memory for a possible kernel - // addr, then see if a kernel is there - eKASLRScanNearPC, // Scan backwards from the current $pc looking for kernel; - // checking at 96 locations total - eKASLRScanExhaustiveScan // Scan through the entire possible kernel address - // range looking for a kernel -}; - -static constexpr OptionEnumValueElement g_kaslr_kernel_scan_enum_values[] = { - {eKASLRScanNone, "none", - "Do not read memory looking for a Darwin kernel when attaching."}, - {eKASLRScanLowgloAddresses, "basic", "Check for the Darwin kernel's load " - "addr in the lowglo page " - "(boot-args=debug) only."}, - {eKASLRScanNearPC, "fast-scan", "Scan near the pc value on attach to find " - "the Darwin kernel's load address."}, - {eKASLRScanExhaustiveScan, "exhaustive-scan", - "Scan through the entire potential address range of Darwin kernel (only " - "on 32-bit targets)."}}; - -static constexpr PropertyDefinition g_properties[] = { - {"load-kexts", OptionValue::eTypeBoolean, true, true, NULL, {}, - "Automatically loads kext images when attaching to a kernel."}, - {"scan-type", OptionValue::eTypeEnum, true, eKASLRScanNearPC, NULL, - OptionEnumValues(g_kaslr_kernel_scan_enum_values), - "Control how many reads lldb will make while searching for a Darwin " - "kernel on attach."}}; - -enum { ePropertyLoadKexts, ePropertyScanType }; - -class DynamicLoaderDarwinKernelProperties : public Properties { -public: - static ConstString &GetSettingName() { - static ConstString g_setting_name("darwin-kernel"); - return g_setting_name; - } - - DynamicLoaderDarwinKernelProperties() : Properties() { - m_collection_sp.reset(new OptionValueProperties(GetSettingName())); - m_collection_sp->Initialize(g_properties); - } - - virtual ~DynamicLoaderDarwinKernelProperties() {} - - bool GetLoadKexts() const { - const uint32_t idx = ePropertyLoadKexts; - return m_collection_sp->GetPropertyAtIndexAsBoolean( - NULL, idx, g_properties[idx].default_uint_value != 0); - } - - KASLRScanType GetScanType() const { - const uint32_t idx = ePropertyScanType; - return (KASLRScanType)m_collection_sp->GetPropertyAtIndexAsEnumeration( - NULL, idx, g_properties[idx].default_uint_value); - } -}; - -typedef std::shared_ptr<DynamicLoaderDarwinKernelProperties> - DynamicLoaderDarwinKernelPropertiesSP; - -static const DynamicLoaderDarwinKernelPropertiesSP &GetGlobalProperties() { - static DynamicLoaderDarwinKernelPropertiesSP g_settings_sp; - if (!g_settings_sp) - g_settings_sp.reset(new DynamicLoaderDarwinKernelProperties()); - return g_settings_sp; -} - -//---------------------------------------------------------------------- -// Create an instance of this class. This function is filled into the plugin -// info class that gets handed out by the plugin factory and allows the lldb to -// instantiate an instance of this class. -//---------------------------------------------------------------------- -DynamicLoader *DynamicLoaderDarwinKernel::CreateInstance(Process *process, - bool force) { - if (!force) { - // If the user provided an executable binary and it is not a kernel, this - // plugin should not create an instance. - Module *exe_module = process->GetTarget().GetExecutableModulePointer(); - if (exe_module) { - ObjectFile *object_file = exe_module->GetObjectFile(); - if (object_file) { - if (object_file->GetStrata() != ObjectFile::eStrataKernel) { - return NULL; - } - } - } - - // If the target's architecture does not look like an Apple environment, - // this plugin should not create an instance. - const llvm::Triple &triple_ref = - process->GetTarget().GetArchitecture().GetTriple(); - switch (triple_ref.getOS()) { - case llvm::Triple::Darwin: - case llvm::Triple::MacOSX: - case llvm::Triple::IOS: - case llvm::Triple::TvOS: - case llvm::Triple::WatchOS: - // NEED_BRIDGEOS_TRIPLE case llvm::Triple::BridgeOS: - if (triple_ref.getVendor() != llvm::Triple::Apple) { - return NULL; - } - break; - // If we have triple like armv7-unknown-unknown, we should try looking for - // a Darwin kernel. - case llvm::Triple::UnknownOS: - break; - default: - return NULL; - break; - } - } - - // At this point if there is an ExecutableModule, it is a kernel and the - // Target is some variant of an Apple system. If the Process hasn't provided - // the kernel load address, we need to look around in memory to find it. - - const addr_t kernel_load_address = SearchForDarwinKernel(process); - if (CheckForKernelImageAtAddress(kernel_load_address, process).IsValid()) { - process->SetCanRunCode(false); - return new DynamicLoaderDarwinKernel(process, kernel_load_address); - } - return NULL; -} - -lldb::addr_t -DynamicLoaderDarwinKernel::SearchForDarwinKernel(Process *process) { - addr_t kernel_load_address = process->GetImageInfoAddress(); - if (kernel_load_address == LLDB_INVALID_ADDRESS) { - kernel_load_address = SearchForKernelAtSameLoadAddr(process); - if (kernel_load_address == LLDB_INVALID_ADDRESS) { - kernel_load_address = SearchForKernelWithDebugHints(process); - if (kernel_load_address == LLDB_INVALID_ADDRESS) { - kernel_load_address = SearchForKernelNearPC(process); - if (kernel_load_address == LLDB_INVALID_ADDRESS) { - kernel_load_address = SearchForKernelViaExhaustiveSearch(process); - } - } - } - } - return kernel_load_address; -} - -//---------------------------------------------------------------------- -// Check if the kernel binary is loaded in memory without a slide. First verify -// that the ExecutableModule is a kernel before we proceed. Returns the address -// of the kernel if one was found, else LLDB_INVALID_ADDRESS. -//---------------------------------------------------------------------- -lldb::addr_t -DynamicLoaderDarwinKernel::SearchForKernelAtSameLoadAddr(Process *process) { - Module *exe_module = process->GetTarget().GetExecutableModulePointer(); - if (exe_module == NULL) - return LLDB_INVALID_ADDRESS; - - ObjectFile *exe_objfile = exe_module->GetObjectFile(); - if (exe_objfile == NULL) - return LLDB_INVALID_ADDRESS; - - if (exe_objfile->GetType() != ObjectFile::eTypeExecutable || - exe_objfile->GetStrata() != ObjectFile::eStrataKernel) - return LLDB_INVALID_ADDRESS; - - if (!exe_objfile->GetBaseAddress().IsValid()) - return LLDB_INVALID_ADDRESS; - - if (CheckForKernelImageAtAddress( - exe_objfile->GetBaseAddress().GetFileAddress(), process) == - exe_module->GetUUID()) - return exe_objfile->GetBaseAddress().GetFileAddress(); - - return LLDB_INVALID_ADDRESS; -} - -//---------------------------------------------------------------------- -// If the debug flag is included in the boot-args nvram setting, the kernel's -// load address will be noted in the lowglo page at a fixed address Returns the -// address of the kernel if one was found, else LLDB_INVALID_ADDRESS. -//---------------------------------------------------------------------- -lldb::addr_t -DynamicLoaderDarwinKernel::SearchForKernelWithDebugHints(Process *process) { - if (GetGlobalProperties()->GetScanType() == eKASLRScanNone) - return LLDB_INVALID_ADDRESS; - - Status read_err; - addr_t kernel_addresses_64[] = { - 0xfffffff000004010ULL, // newest arm64 devices - 0xffffff8000004010ULL, // 2014-2015-ish arm64 devices - 0xffffff8000002010ULL, // oldest arm64 devices - LLDB_INVALID_ADDRESS}; - addr_t kernel_addresses_32[] = {0xffff0110, // 2016 and earlier armv7 devices - 0xffff1010, - LLDB_INVALID_ADDRESS}; - - uint8_t uval[8]; - if (process->GetAddressByteSize() == 8) { - for (size_t i = 0; kernel_addresses_64[i] != LLDB_INVALID_ADDRESS; i++) { - if (process->ReadMemoryFromInferior (kernel_addresses_64[i], uval, 8, read_err) == 8) - { - DataExtractor data (&uval, 8, process->GetByteOrder(), process->GetAddressByteSize()); - offset_t offset = 0; - uint64_t addr = data.GetU64 (&offset); - if (CheckForKernelImageAtAddress(addr, process).IsValid()) { - return addr; - } - } - } - } - - if (process->GetAddressByteSize() == 4) { - for (size_t i = 0; kernel_addresses_32[i] != LLDB_INVALID_ADDRESS; i++) { - if (process->ReadMemoryFromInferior (kernel_addresses_32[i], uval, 4, read_err) == 4) - { - DataExtractor data (&uval, 4, process->GetByteOrder(), process->GetAddressByteSize()); - offset_t offset = 0; - uint32_t addr = data.GetU32 (&offset); - if (CheckForKernelImageAtAddress(addr, process).IsValid()) { - return addr; - } - } - } - } - - return LLDB_INVALID_ADDRESS; -} - -//---------------------------------------------------------------------- -// If the kernel is currently executing when lldb attaches, and we don't have a -// better way of finding the kernel's load address, try searching backwards -// from the current pc value looking for the kernel's Mach header in memory. -// Returns the address of the kernel if one was found, else -// LLDB_INVALID_ADDRESS. -//---------------------------------------------------------------------- -lldb::addr_t -DynamicLoaderDarwinKernel::SearchForKernelNearPC(Process *process) { - if (GetGlobalProperties()->GetScanType() == eKASLRScanNone || - GetGlobalProperties()->GetScanType() == eKASLRScanLowgloAddresses) { - return LLDB_INVALID_ADDRESS; - } - - ThreadSP thread = process->GetThreadList().GetSelectedThread(); - if (thread.get() == NULL) - return LLDB_INVALID_ADDRESS; - addr_t pc = thread->GetRegisterContext()->GetPC(LLDB_INVALID_ADDRESS); - - // The kernel is always loaded in high memory, if the top bit is zero, - // this isn't a kernel. - if (process->GetTarget().GetArchitecture().GetAddressByteSize() == 8) { - if ((pc & (1ULL << 63)) == 0) { - return LLDB_INVALID_ADDRESS; - } - } else { - if ((pc & (1ULL << 31)) == 0) { - return LLDB_INVALID_ADDRESS; - } - } - - if (pc == LLDB_INVALID_ADDRESS) - return LLDB_INVALID_ADDRESS; - - // The kernel will load at at one megabyte boundary (0x100000), or at that - // boundary plus an offset of one page (0x1000) or two, or four (0x4000), - // depending on the device. - - // Round the current pc down to the nearest one megabyte boundary - the place - // where we will start searching. - addr_t addr = pc & ~0xfffff; - - // Search backwards 32 megabytes, looking for the start of the kernel at each - // one-megabyte boundary. - for (int i = 0; i < 32; i++, addr -= 0x100000) { - // x86_64 kernels are at offset 0 - if (CheckForKernelImageAtAddress(addr, process).IsValid()) - return addr; - // 32-bit arm kernels are at offset 0x1000 (one 4k page) - if (CheckForKernelImageAtAddress(addr + 0x1000, process).IsValid()) - return addr + 0x1000; - // 64-bit arm kernels are at offset 0x4000 (one 16k page) - if (CheckForKernelImageAtAddress(addr + 0x4000, process).IsValid()) - return addr + 0x4000; - } - - return LLDB_INVALID_ADDRESS; -} - -//---------------------------------------------------------------------- -// Scan through the valid address range for a kernel binary. This is uselessly -// slow in 64-bit environments so we don't even try it. This scan is not -// enabled by default even for 32-bit targets. Returns the address of the -// kernel if one was found, else LLDB_INVALID_ADDRESS. -//---------------------------------------------------------------------- -lldb::addr_t DynamicLoaderDarwinKernel::SearchForKernelViaExhaustiveSearch( - Process *process) { - if (GetGlobalProperties()->GetScanType() != eKASLRScanExhaustiveScan) { - return LLDB_INVALID_ADDRESS; - } - - addr_t kernel_range_low, kernel_range_high; - if (process->GetTarget().GetArchitecture().GetAddressByteSize() == 8) { - kernel_range_low = 1ULL << 63; - kernel_range_high = UINT64_MAX; - } else { - kernel_range_low = 1ULL << 31; - kernel_range_high = UINT32_MAX; - } - - // Stepping through memory at one-megabyte resolution looking for a kernel - // rarely works (fast enough) with a 64-bit address space -- for now, let's - // not even bother. We may be attaching to something which *isn't* a kernel - // and we don't want to spin for minutes on-end looking for a kernel. - if (process->GetTarget().GetArchitecture().GetAddressByteSize() == 8) - return LLDB_INVALID_ADDRESS; - - addr_t addr = kernel_range_low; - - while (addr >= kernel_range_low && addr < kernel_range_high) { - // x86_64 kernels are at offset 0 - if (CheckForKernelImageAtAddress(addr, process).IsValid()) - return addr; - // 32-bit arm kernels are at offset 0x1000 (one 4k page) - if (CheckForKernelImageAtAddress(addr + 0x1000, process).IsValid()) - return addr + 0x1000; - // 64-bit arm kernels are at offset 0x4000 (one 16k page) - if (CheckForKernelImageAtAddress(addr + 0x4000, process).IsValid()) - return addr + 0x4000; - addr += 0x100000; - } - return LLDB_INVALID_ADDRESS; -} - -//---------------------------------------------------------------------- -// Read the mach_header struct out of memory and return it. -// Returns true if the mach_header was successfully read, -// Returns false if there was a problem reading the header, or it was not -// a Mach-O header. -//---------------------------------------------------------------------- - -bool -DynamicLoaderDarwinKernel::ReadMachHeader(addr_t addr, Process *process, llvm::MachO::mach_header &header) { - Status read_error; - - // Read the mach header and see whether it looks like a kernel - if (process->DoReadMemory (addr, &header, sizeof(header), read_error) != - sizeof(header)) - return false; - - const uint32_t magicks[] = { llvm::MachO::MH_MAGIC_64, llvm::MachO::MH_MAGIC, llvm::MachO::MH_CIGAM, llvm::MachO::MH_CIGAM_64}; - - bool found_matching_pattern = false; - for (size_t i = 0; i < llvm::array_lengthof (magicks); i++) - if (::memcmp (&header.magic, &magicks[i], sizeof (uint32_t)) == 0) - found_matching_pattern = true; - - if (!found_matching_pattern) - return false; - - if (header.magic == llvm::MachO::MH_CIGAM || - header.magic == llvm::MachO::MH_CIGAM_64) { - header.magic = llvm::ByteSwap_32(header.magic); - header.cputype = llvm::ByteSwap_32(header.cputype); - header.cpusubtype = llvm::ByteSwap_32(header.cpusubtype); - header.filetype = llvm::ByteSwap_32(header.filetype); - header.ncmds = llvm::ByteSwap_32(header.ncmds); - header.sizeofcmds = llvm::ByteSwap_32(header.sizeofcmds); - header.flags = llvm::ByteSwap_32(header.flags); - } - - return true; -} - -//---------------------------------------------------------------------- -// Given an address in memory, look to see if there is a kernel image at that -// address. -// Returns a UUID; if a kernel was not found at that address, UUID.IsValid() -// will be false. -//---------------------------------------------------------------------- -lldb_private::UUID -DynamicLoaderDarwinKernel::CheckForKernelImageAtAddress(lldb::addr_t addr, - Process *process) { - Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER)); - if (addr == LLDB_INVALID_ADDRESS) - return UUID(); - - if (log) - log->Printf("DynamicLoaderDarwinKernel::CheckForKernelImageAtAddress: " - "looking for kernel binary at 0x%" PRIx64, - addr); - - llvm::MachO::mach_header header; - - if (!ReadMachHeader(addr, process, header)) - return UUID(); - - // First try a quick test -- read the first 4 bytes and see if there is a - // valid Mach-O magic field there - // (the first field of the mach_header/mach_header_64 struct). - // A kernel is an executable which does not have the dynamic link object flag - // set. - if (header.filetype == llvm::MachO::MH_EXECUTE && - (header.flags & llvm::MachO::MH_DYLDLINK) == 0) { - // Create a full module to get the UUID - ModuleSP memory_module_sp = - process->ReadModuleFromMemory(FileSpec("temp_mach_kernel"), addr); - if (!memory_module_sp.get()) - return UUID(); - - ObjectFile *exe_objfile = memory_module_sp->GetObjectFile(); - if (exe_objfile == NULL) { - if (log) - log->Printf("DynamicLoaderDarwinKernel::CheckForKernelImageAtAddress " - "found a binary at 0x%" PRIx64 - " but could not create an object file from memory", - addr); - return UUID(); - } - - if (exe_objfile->GetType() == ObjectFile::eTypeExecutable && - exe_objfile->GetStrata() == ObjectFile::eStrataKernel) { - ArchSpec kernel_arch(eArchTypeMachO, header.cputype, header.cpusubtype); - if (!process->GetTarget().GetArchitecture().IsCompatibleMatch( - kernel_arch)) { - process->GetTarget().SetArchitecture(kernel_arch); - } - if (log) { - std::string uuid_str; - if (memory_module_sp->GetUUID().IsValid()) { - uuid_str = "with UUID "; - uuid_str += memory_module_sp->GetUUID().GetAsString(); - } else { - uuid_str = "and no LC_UUID found in load commands "; - } - log->Printf( - "DynamicLoaderDarwinKernel::CheckForKernelImageAtAddress: " - "kernel binary image found at 0x%" PRIx64 " with arch '%s' %s", - addr, kernel_arch.GetTriple().str().c_str(), uuid_str.c_str()); - } - return memory_module_sp->GetUUID(); - } - } - - return UUID(); -} - -//---------------------------------------------------------------------- -// Constructor -//---------------------------------------------------------------------- -DynamicLoaderDarwinKernel::DynamicLoaderDarwinKernel(Process *process, - lldb::addr_t kernel_addr) - : DynamicLoader(process), m_kernel_load_address(kernel_addr), m_kernel(), - m_kext_summary_header_ptr_addr(), m_kext_summary_header_addr(), - m_kext_summary_header(), m_known_kexts(), m_mutex(), - m_break_id(LLDB_INVALID_BREAK_ID) { - Status error; - PlatformSP platform_sp( - Platform::Create(PlatformDarwinKernel::GetPluginNameStatic(), error)); - // Only select the darwin-kernel Platform if we've been asked to load kexts. - // It can take some time to scan over all of the kext info.plists and that - // shouldn't be done if kext loading is explicitly disabled. - if (platform_sp.get() && GetGlobalProperties()->GetLoadKexts()) { - process->GetTarget().SetPlatform(platform_sp); - } -} - -//---------------------------------------------------------------------- -// Destructor -//---------------------------------------------------------------------- -DynamicLoaderDarwinKernel::~DynamicLoaderDarwinKernel() { Clear(true); } - -void DynamicLoaderDarwinKernel::UpdateIfNeeded() { - LoadKernelModuleIfNeeded(); - SetNotificationBreakpointIfNeeded(); -} -//------------------------------------------------------------------ -/// Called after attaching a process. -/// -/// Allow DynamicLoader plug-ins to execute some code after -/// attaching to a process. -//------------------------------------------------------------------ -void DynamicLoaderDarwinKernel::DidAttach() { - PrivateInitialize(m_process); - UpdateIfNeeded(); -} - -//------------------------------------------------------------------ -/// Called after attaching a process. -/// -/// Allow DynamicLoader plug-ins to execute some code after -/// attaching to a process. -//------------------------------------------------------------------ -void DynamicLoaderDarwinKernel::DidLaunch() { - PrivateInitialize(m_process); - UpdateIfNeeded(); -} - -//---------------------------------------------------------------------- -// Clear out the state of this class. -//---------------------------------------------------------------------- -void DynamicLoaderDarwinKernel::Clear(bool clear_process) { - std::lock_guard<std::recursive_mutex> guard(m_mutex); - - if (m_process->IsAlive() && LLDB_BREAK_ID_IS_VALID(m_break_id)) - m_process->ClearBreakpointSiteByID(m_break_id); - - if (clear_process) - m_process = NULL; - m_kernel.Clear(); - m_known_kexts.clear(); - m_kext_summary_header_ptr_addr.Clear(); - m_kext_summary_header_addr.Clear(); - m_break_id = LLDB_INVALID_BREAK_ID; -} - -bool DynamicLoaderDarwinKernel::KextImageInfo::LoadImageAtFileAddress( - Process *process) { - if (IsLoaded()) - return true; - - if (m_module_sp) { - bool changed = false; - if (m_module_sp->SetLoadAddress(process->GetTarget(), 0, true, changed)) - m_load_process_stop_id = process->GetStopID(); - } - return false; -} - -void DynamicLoaderDarwinKernel::KextImageInfo::SetModule(ModuleSP module_sp) { - m_module_sp = module_sp; - if (module_sp.get() && module_sp->GetObjectFile()) { - if (module_sp->GetObjectFile()->GetType() == ObjectFile::eTypeExecutable && - module_sp->GetObjectFile()->GetStrata() == ObjectFile::eStrataKernel) { - m_kernel_image = true; - } else { - m_kernel_image = false; - } - } -} - -ModuleSP DynamicLoaderDarwinKernel::KextImageInfo::GetModule() { - return m_module_sp; -} - -void DynamicLoaderDarwinKernel::KextImageInfo::SetLoadAddress( - addr_t load_addr) { - m_load_address = load_addr; -} - -addr_t DynamicLoaderDarwinKernel::KextImageInfo::GetLoadAddress() const { - return m_load_address; -} - -uint64_t DynamicLoaderDarwinKernel::KextImageInfo::GetSize() const { - return m_size; -} - -void DynamicLoaderDarwinKernel::KextImageInfo::SetSize(uint64_t size) { - m_size = size; -} - -uint32_t DynamicLoaderDarwinKernel::KextImageInfo::GetProcessStopId() const { - return m_load_process_stop_id; -} - -void DynamicLoaderDarwinKernel::KextImageInfo::SetProcessStopId( - uint32_t stop_id) { - m_load_process_stop_id = stop_id; -} - -bool DynamicLoaderDarwinKernel::KextImageInfo:: -operator==(const KextImageInfo &rhs) { - if (m_uuid.IsValid() || rhs.GetUUID().IsValid()) { - return m_uuid == rhs.GetUUID(); - } - - return m_name == rhs.GetName() && m_load_address == rhs.GetLoadAddress(); -} - -void DynamicLoaderDarwinKernel::KextImageInfo::SetName(const char *name) { - m_name = name; -} - -std::string DynamicLoaderDarwinKernel::KextImageInfo::GetName() const { - return m_name; -} - -void DynamicLoaderDarwinKernel::KextImageInfo::SetUUID(const UUID &uuid) { - m_uuid = uuid; -} - -UUID DynamicLoaderDarwinKernel::KextImageInfo::GetUUID() const { - return m_uuid; -} - -// Given the m_load_address from the kext summaries, and a UUID, try to create -// an in-memory Module at that address. Require that the MemoryModule have a -// matching UUID and detect if this MemoryModule is a kernel or a kext. -// -// Returns true if m_memory_module_sp is now set to a valid Module. - -bool DynamicLoaderDarwinKernel::KextImageInfo::ReadMemoryModule( - Process *process) { - Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_HOST); - if (m_memory_module_sp.get() != NULL) - return true; - if (m_load_address == LLDB_INVALID_ADDRESS) - return false; - - FileSpec file_spec(m_name.c_str()); - - llvm::MachO::mach_header mh; - size_t size_to_read = 512; - if (ReadMachHeader(m_load_address, process, mh)) { - if (mh.magic == llvm::MachO::MH_CIGAM || mh.magic == llvm::MachO::MH_MAGIC) - size_to_read = sizeof(llvm::MachO::mach_header) + mh.sizeofcmds; - if (mh.magic == llvm::MachO::MH_CIGAM_64 || - mh.magic == llvm::MachO::MH_MAGIC_64) - size_to_read = sizeof(llvm::MachO::mach_header_64) + mh.sizeofcmds; - } - - ModuleSP memory_module_sp = - process->ReadModuleFromMemory(file_spec, m_load_address, size_to_read); - - if (memory_module_sp.get() == NULL) - return false; - - bool is_kernel = false; - if (memory_module_sp->GetObjectFile()) { - if (memory_module_sp->GetObjectFile()->GetType() == - ObjectFile::eTypeExecutable && - memory_module_sp->GetObjectFile()->GetStrata() == - ObjectFile::eStrataKernel) { - is_kernel = true; - } else if (memory_module_sp->GetObjectFile()->GetType() == - ObjectFile::eTypeSharedLibrary) { - is_kernel = false; - } - } - - // If this is a kext, and the kernel specified what UUID we should find at - // this load address, require that the memory module have a matching UUID or - // something has gone wrong and we should discard it. - if (m_uuid.IsValid()) { - if (m_uuid != memory_module_sp->GetUUID()) { - if (log) { - log->Printf("KextImageInfo::ReadMemoryModule the kernel said to find " - "uuid %s at 0x%" PRIx64 - " but instead we found uuid %s, throwing it away", - m_uuid.GetAsString().c_str(), m_load_address, - memory_module_sp->GetUUID().GetAsString().c_str()); - } - return false; - } - } - - // If the in-memory Module has a UUID, let's use that. - if (!m_uuid.IsValid() && memory_module_sp->GetUUID().IsValid()) { - m_uuid = memory_module_sp->GetUUID(); - } - - m_memory_module_sp = memory_module_sp; - m_kernel_image = is_kernel; - if (is_kernel) { - if (log) { - // This is unusual and probably not intended - log->Printf("KextImageInfo::ReadMemoryModule read the kernel binary out " - "of memory"); - } - if (memory_module_sp->GetArchitecture().IsValid()) { - process->GetTarget().SetArchitecture(memory_module_sp->GetArchitecture()); - } - if (m_uuid.IsValid()) { - ModuleSP exe_module_sp = process->GetTarget().GetExecutableModule(); - if (exe_module_sp.get() && exe_module_sp->GetUUID().IsValid()) { - if (m_uuid != exe_module_sp->GetUUID()) { - // The user specified a kernel binary that has a different UUID than - // the kernel actually running in memory. This never ends well; - // clear the user specified kernel binary from the Target. - - m_module_sp.reset(); - - ModuleList user_specified_kernel_list; - user_specified_kernel_list.Append(exe_module_sp); - process->GetTarget().GetImages().Remove(user_specified_kernel_list); - } - } - } - } - - return true; -} - -bool DynamicLoaderDarwinKernel::KextImageInfo::IsKernel() const { - return m_kernel_image; -} - -void DynamicLoaderDarwinKernel::KextImageInfo::SetIsKernel(bool is_kernel) { - m_kernel_image = is_kernel; -} - -bool DynamicLoaderDarwinKernel::KextImageInfo::LoadImageUsingMemoryModule( - Process *process) { - if (IsLoaded()) - return true; - - Target &target = process->GetTarget(); - - // If we don't have / can't create a memory module for this kext, don't try - // to load it - we won't have the correct segment load addresses. - if (!ReadMemoryModule(process)) { - Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER)); - if (log) - log->Printf("Unable to read '%s' from memory at address 0x%" PRIx64 - " to get the segment load addresses.", - m_name.c_str(), m_load_address); - return false; - } - - bool uuid_is_valid = m_uuid.IsValid(); - - if (IsKernel() && uuid_is_valid && m_memory_module_sp.get()) { - Stream *s = target.GetDebugger().GetOutputFile().get(); - if (s) { - s->Printf("Kernel UUID: %s\n", - m_memory_module_sp->GetUUID().GetAsString().c_str()); - s->Printf("Load Address: 0x%" PRIx64 "\n", m_load_address); - } - } - - if (!m_module_sp) { - // See if the kext has already been loaded into the target, probably by the - // user doing target modules add. - const ModuleList &target_images = target.GetImages(); - m_module_sp = target_images.FindModule(m_uuid); - - // Search for the kext on the local filesystem via the UUID - if (!m_module_sp && uuid_is_valid) { - ModuleSpec module_spec; - module_spec.GetUUID() = m_uuid; - module_spec.GetArchitecture() = target.GetArchitecture(); - - // For the kernel, we really do need an on-disk file copy of the binary - // to do anything useful. This will force a clal to - if (IsKernel()) { - if (Symbols::DownloadObjectAndSymbolFile(module_spec, true)) { - if (FileSystem::Instance().Exists(module_spec.GetFileSpec())) { - m_module_sp.reset(new Module(module_spec.GetFileSpec(), - target.GetArchitecture())); - if (m_module_sp.get() && - m_module_sp->MatchesModuleSpec(module_spec)) { - ModuleList loaded_module_list; - loaded_module_list.Append(m_module_sp); - target.ModulesDidLoad(loaded_module_list); - } - } - } - } - - // If the current platform is PlatformDarwinKernel, create a ModuleSpec - // with the filename set to be the bundle ID for this kext, e.g. - // "com.apple.filesystems.msdosfs", and ask the platform to find it. - PlatformSP platform_sp(target.GetPlatform()); - if (!m_module_sp && platform_sp) { - ConstString platform_name(platform_sp->GetPluginName()); - static ConstString g_platform_name( - PlatformDarwinKernel::GetPluginNameStatic()); - if (platform_name == g_platform_name) { - ModuleSpec kext_bundle_module_spec(module_spec); - FileSpec kext_filespec(m_name.c_str()); - kext_bundle_module_spec.GetFileSpec() = kext_filespec; - platform_sp->GetSharedModule( - kext_bundle_module_spec, process, m_module_sp, - &target.GetExecutableSearchPaths(), NULL, NULL); - } - } - - // Ask the Target to find this file on the local system, if possible. - // This will search in the list of currently-loaded files, look in the - // standard search paths on the system, and on a Mac it will try calling - // the DebugSymbols framework with the UUID to find the binary via its - // search methods. - if (!m_module_sp) { - m_module_sp = target.GetSharedModule(module_spec); - } - - if (IsKernel() && !m_module_sp) { - Stream *s = target.GetDebugger().GetOutputFile().get(); - if (s) { - s->Printf("WARNING: Unable to locate kernel binary on the debugger " - "system.\n"); - } - } - } - - // If we managed to find a module, append it to the target's list of - // images. If we also have a memory module, require that they have matching - // UUIDs - if (m_module_sp) { - bool uuid_match_ok = true; - if (m_memory_module_sp) { - if (m_module_sp->GetUUID() != m_memory_module_sp->GetUUID()) { - uuid_match_ok = false; - } - } - if (uuid_match_ok) { - target.GetImages().AppendIfNeeded(m_module_sp); - if (IsKernel() && - target.GetExecutableModulePointer() != m_module_sp.get()) { - target.SetExecutableModule(m_module_sp, eLoadDependentsNo); - } - } - } - } - - if (!m_module_sp && !IsKernel() && m_uuid.IsValid() && !m_name.empty()) { - Stream *s = target.GetDebugger().GetOutputFile().get(); - if (s) { - s->Printf("warning: Can't find binary/dSYM for %s (%s)\n", m_name.c_str(), - m_uuid.GetAsString().c_str()); - } - } - - static ConstString g_section_name_LINKEDIT("__LINKEDIT"); - - if (m_memory_module_sp && m_module_sp) { - if (m_module_sp->GetUUID() == m_memory_module_sp->GetUUID()) { - ObjectFile *ondisk_object_file = m_module_sp->GetObjectFile(); - ObjectFile *memory_object_file = m_memory_module_sp->GetObjectFile(); - - if (memory_object_file && ondisk_object_file) { - // The memory_module for kexts may have an invalid __LINKEDIT seg; skip - // it. - const bool ignore_linkedit = !IsKernel(); - - SectionList *ondisk_section_list = ondisk_object_file->GetSectionList(); - SectionList *memory_section_list = memory_object_file->GetSectionList(); - if (memory_section_list && ondisk_section_list) { - const uint32_t num_ondisk_sections = ondisk_section_list->GetSize(); - // There may be CTF sections in the memory image so we can't always - // just compare the number of sections (which are actually segments - // in mach-o parlance) - uint32_t sect_idx = 0; - - // Use the memory_module's addresses for each section to set the file - // module's load address as appropriate. We don't want to use a - // single slide value for the entire kext - different segments may be - // slid different amounts by the kext loader. - - uint32_t num_sections_loaded = 0; - for (sect_idx = 0; sect_idx < num_ondisk_sections; ++sect_idx) { - SectionSP ondisk_section_sp( - ondisk_section_list->GetSectionAtIndex(sect_idx)); - if (ondisk_section_sp) { - // Don't ever load __LINKEDIT as it may or may not be actually - // mapped into memory and there is no current way to tell. - // I filed rdar://problem/12851706 to track being able to tell - // if the __LINKEDIT is actually mapped, but until then, we need - // to not load the __LINKEDIT - if (ignore_linkedit && - ondisk_section_sp->GetName() == g_section_name_LINKEDIT) - continue; - - const Section *memory_section = - memory_section_list - ->FindSectionByName(ondisk_section_sp->GetName()) - .get(); - if (memory_section) { - target.SetSectionLoadAddress(ondisk_section_sp, - memory_section->GetFileAddress()); - ++num_sections_loaded; - } - } - } - if (num_sections_loaded > 0) - m_load_process_stop_id = process->GetStopID(); - else - m_module_sp.reset(); // No sections were loaded - } else - m_module_sp.reset(); // One or both section lists - } else - m_module_sp.reset(); // One or both object files missing - } else - m_module_sp.reset(); // UUID mismatch - } - - bool is_loaded = IsLoaded(); - - if (is_loaded && m_module_sp && IsKernel()) { - Stream *s = target.GetDebugger().GetOutputFile().get(); - if (s) { - ObjectFile *kernel_object_file = m_module_sp->GetObjectFile(); - if (kernel_object_file) { - addr_t file_address = - kernel_object_file->GetBaseAddress().GetFileAddress(); - if (m_load_address != LLDB_INVALID_ADDRESS && - file_address != LLDB_INVALID_ADDRESS) { - s->Printf("Kernel slid 0x%" PRIx64 " in memory.\n", - m_load_address - file_address); - } - } - { - s->Printf("Loaded kernel file %s\n", - m_module_sp->GetFileSpec().GetPath().c_str()); - } - s->Flush(); - } - } - return is_loaded; -} - -uint32_t DynamicLoaderDarwinKernel::KextImageInfo::GetAddressByteSize() { - if (m_memory_module_sp) - return m_memory_module_sp->GetArchitecture().GetAddressByteSize(); - if (m_module_sp) - return m_module_sp->GetArchitecture().GetAddressByteSize(); - return 0; -} - -lldb::ByteOrder DynamicLoaderDarwinKernel::KextImageInfo::GetByteOrder() { - if (m_memory_module_sp) - return m_memory_module_sp->GetArchitecture().GetByteOrder(); - if (m_module_sp) - return m_module_sp->GetArchitecture().GetByteOrder(); - return endian::InlHostByteOrder(); -} - -lldb_private::ArchSpec -DynamicLoaderDarwinKernel::KextImageInfo::GetArchitecture() const { - if (m_memory_module_sp) - return m_memory_module_sp->GetArchitecture(); - if (m_module_sp) - return m_module_sp->GetArchitecture(); - return lldb_private::ArchSpec(); -} - -//---------------------------------------------------------------------- -// Load the kernel module and initialize the "m_kernel" member. Return true -// _only_ if the kernel is loaded the first time through (subsequent calls to -// this function should return false after the kernel has been already loaded). -//---------------------------------------------------------------------- -void DynamicLoaderDarwinKernel::LoadKernelModuleIfNeeded() { - if (!m_kext_summary_header_ptr_addr.IsValid()) { - m_kernel.Clear(); - m_kernel.SetModule(m_process->GetTarget().GetExecutableModule()); - m_kernel.SetIsKernel(true); - - ConstString kernel_name("mach_kernel"); - if (m_kernel.GetModule().get() && m_kernel.GetModule()->GetObjectFile() && - !m_kernel.GetModule() - ->GetObjectFile() - ->GetFileSpec() - .GetFilename() - .IsEmpty()) { - kernel_name = - m_kernel.GetModule()->GetObjectFile()->GetFileSpec().GetFilename(); - } - m_kernel.SetName(kernel_name.AsCString()); - - if (m_kernel.GetLoadAddress() == LLDB_INVALID_ADDRESS) { - m_kernel.SetLoadAddress(m_kernel_load_address); - if (m_kernel.GetLoadAddress() == LLDB_INVALID_ADDRESS && - m_kernel.GetModule()) { - // We didn't get a hint from the process, so we will try the kernel at - // the address that it exists at in the file if we have one - ObjectFile *kernel_object_file = m_kernel.GetModule()->GetObjectFile(); - if (kernel_object_file) { - addr_t load_address = - kernel_object_file->GetBaseAddress().GetLoadAddress( - &m_process->GetTarget()); - addr_t file_address = - kernel_object_file->GetBaseAddress().GetFileAddress(); - if (load_address != LLDB_INVALID_ADDRESS && load_address != 0) { - m_kernel.SetLoadAddress(load_address); - if (load_address != file_address) { - // Don't accidentally relocate the kernel to the File address -- - // the Load address has already been set to its actual in-memory - // address. Mark it as IsLoaded. - m_kernel.SetProcessStopId(m_process->GetStopID()); - } - } else { - m_kernel.SetLoadAddress(file_address); - } - } - } - } - - if (m_kernel.GetLoadAddress() != LLDB_INVALID_ADDRESS) { - if (!m_kernel.LoadImageUsingMemoryModule(m_process)) { - m_kernel.LoadImageAtFileAddress(m_process); - } - } - - // The operating system plugin gets loaded and initialized in - // LoadImageUsingMemoryModule when we discover the kernel dSYM. For a core - // file in particular, that's the wrong place to do this, since we haven't - // fixed up the section addresses yet. So let's redo it here. - LoadOperatingSystemPlugin(false); - - if (m_kernel.IsLoaded() && m_kernel.GetModule()) { - static ConstString kext_summary_symbol("gLoadedKextSummaries"); - const Symbol *symbol = - m_kernel.GetModule()->FindFirstSymbolWithNameAndType( - kext_summary_symbol, eSymbolTypeData); - if (symbol) { - m_kext_summary_header_ptr_addr = symbol->GetAddress(); - // Update all image infos - ReadAllKextSummaries(); - } - } else { - m_kernel.Clear(); - } - } -} - -//---------------------------------------------------------------------- -// Static callback function that gets called when our DYLD notification -// breakpoint gets hit. We update all of our image infos and then let our super -// class DynamicLoader class decide if we should stop or not (based on global -// preference). -//---------------------------------------------------------------------- -bool DynamicLoaderDarwinKernel::BreakpointHitCallback( - void *baton, StoppointCallbackContext *context, user_id_t break_id, - user_id_t break_loc_id) { - return static_cast<DynamicLoaderDarwinKernel *>(baton)->BreakpointHit( - context, break_id, break_loc_id); -} - -bool DynamicLoaderDarwinKernel::BreakpointHit(StoppointCallbackContext *context, - user_id_t break_id, - user_id_t break_loc_id) { - Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER)); - if (log) - log->Printf("DynamicLoaderDarwinKernel::BreakpointHit (...)\n"); - - ReadAllKextSummaries(); - - if (log) - PutToLog(log); - - return GetStopWhenImagesChange(); -} - -bool DynamicLoaderDarwinKernel::ReadKextSummaryHeader() { - std::lock_guard<std::recursive_mutex> guard(m_mutex); - - // the all image infos is already valid for this process stop ID - - if (m_kext_summary_header_ptr_addr.IsValid()) { - const uint32_t addr_size = m_kernel.GetAddressByteSize(); - const ByteOrder byte_order = m_kernel.GetByteOrder(); - Status error; - // Read enough bytes for a "OSKextLoadedKextSummaryHeader" structure which - // is currently 4 uint32_t and a pointer. - uint8_t buf[24]; - DataExtractor data(buf, sizeof(buf), byte_order, addr_size); - const size_t count = 4 * sizeof(uint32_t) + addr_size; - const bool prefer_file_cache = false; - if (m_process->GetTarget().ReadPointerFromMemory( - m_kext_summary_header_ptr_addr, prefer_file_cache, error, - m_kext_summary_header_addr)) { - // We got a valid address for our kext summary header and make sure it - // isn't NULL - if (m_kext_summary_header_addr.IsValid() && - m_kext_summary_header_addr.GetFileAddress() != 0) { - const size_t bytes_read = m_process->GetTarget().ReadMemory( - m_kext_summary_header_addr, prefer_file_cache, buf, count, error); - if (bytes_read == count) { - lldb::offset_t offset = 0; - m_kext_summary_header.version = data.GetU32(&offset); - if (m_kext_summary_header.version > 128) { - Stream *s = - m_process->GetTarget().GetDebugger().GetOutputFile().get(); - s->Printf("WARNING: Unable to read kext summary header, got " - "improbable version number %u\n", - m_kext_summary_header.version); - // If we get an improbably large version number, we're probably - // getting bad memory. - m_kext_summary_header_addr.Clear(); - return false; - } - if (m_kext_summary_header.version >= 2) { - m_kext_summary_header.entry_size = data.GetU32(&offset); - if (m_kext_summary_header.entry_size > 4096) { - // If we get an improbably large entry_size, we're probably - // getting bad memory. - Stream *s = - m_process->GetTarget().GetDebugger().GetOutputFile().get(); - s->Printf("WARNING: Unable to read kext summary header, got " - "improbable entry_size %u\n", - m_kext_summary_header.entry_size); - m_kext_summary_header_addr.Clear(); - return false; - } - } else { - // Versions less than 2 didn't have an entry size, it was hard - // coded - m_kext_summary_header.entry_size = - KERNEL_MODULE_ENTRY_SIZE_VERSION_1; - } - m_kext_summary_header.entry_count = data.GetU32(&offset); - if (m_kext_summary_header.entry_count > 10000) { - // If we get an improbably large number of kexts, we're probably - // getting bad memory. - Stream *s = - m_process->GetTarget().GetDebugger().GetOutputFile().get(); - s->Printf("WARNING: Unable to read kext summary header, got " - "improbable number of kexts %u\n", - m_kext_summary_header.entry_count); - m_kext_summary_header_addr.Clear(); - return false; - } - return true; - } - } - } - } - m_kext_summary_header_addr.Clear(); - return false; -} - -// We've either (a) just attached to a new kernel, or (b) the kexts-changed -// breakpoint was hit and we need to figure out what kexts have been added or -// removed. Read the kext summaries from the inferior kernel memory, compare -// them against the m_known_kexts vector and update the m_known_kexts vector as -// needed to keep in sync with the inferior. - -bool DynamicLoaderDarwinKernel::ParseKextSummaries( - const Address &kext_summary_addr, uint32_t count) { - KextImageInfo::collection kext_summaries; - Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER)); - if (log) - log->Printf("Kexts-changed breakpoint hit, there are %d kexts currently.\n", - count); - - std::lock_guard<std::recursive_mutex> guard(m_mutex); - - if (!ReadKextSummaries(kext_summary_addr, count, kext_summaries)) - return false; - - // read the plugin.dynamic-loader.darwin-kernel.load-kexts setting -- if the - // user requested no kext loading, don't print any messages about kexts & - // don't try to read them. - const bool load_kexts = GetGlobalProperties()->GetLoadKexts(); - - // By default, all kexts we've loaded in the past are marked as "remove" and - // all of the kexts we just found out about from ReadKextSummaries are marked - // as "add". - std::vector<bool> to_be_removed(m_known_kexts.size(), true); - std::vector<bool> to_be_added(count, true); - - int number_of_new_kexts_being_added = 0; - int number_of_old_kexts_being_removed = m_known_kexts.size(); - - const uint32_t new_kexts_size = kext_summaries.size(); - const uint32_t old_kexts_size = m_known_kexts.size(); - - // The m_known_kexts vector may have entries that have been Cleared, or are a - // kernel. - for (uint32_t old_kext = 0; old_kext < old_kexts_size; old_kext++) { - bool ignore = false; - KextImageInfo &image_info = m_known_kexts[old_kext]; - if (image_info.IsKernel()) { - ignore = true; - } else if (image_info.GetLoadAddress() == LLDB_INVALID_ADDRESS && - !image_info.GetModule()) { - ignore = true; - } - - if (ignore) { - number_of_old_kexts_being_removed--; - to_be_removed[old_kext] = false; - } - } - - // Scan over the list of kexts we just read from the kernel, note those that - // need to be added and those already loaded. - for (uint32_t new_kext = 0; new_kext < new_kexts_size; new_kext++) { - bool add_this_one = true; - for (uint32_t old_kext = 0; old_kext < old_kexts_size; old_kext++) { - if (m_known_kexts[old_kext] == kext_summaries[new_kext]) { - // We already have this kext, don't re-load it. - to_be_added[new_kext] = false; - // This kext is still present, do not remove it. - to_be_removed[old_kext] = false; - - number_of_old_kexts_being_removed--; - add_this_one = false; - break; - } - } - // If this "kext" entry is actually an alias for the kernel -- the kext was - // compiled into the kernel or something -- then we don't want to load the - // kernel's text section at a different address. Ignore this kext entry. - if (kext_summaries[new_kext].GetUUID().IsValid() - && m_kernel.GetUUID().IsValid() - && kext_summaries[new_kext].GetUUID() == m_kernel.GetUUID()) { - to_be_added[new_kext] = false; - break; - } - if (add_this_one) { - number_of_new_kexts_being_added++; - } - } - - if (number_of_new_kexts_being_added == 0 && - number_of_old_kexts_being_removed == 0) - return true; - - Stream *s = m_process->GetTarget().GetDebugger().GetOutputFile().get(); - if (s && load_kexts) { - if (number_of_new_kexts_being_added > 0 && - number_of_old_kexts_being_removed > 0) { - s->Printf("Loading %d kext modules and unloading %d kext modules ", - number_of_new_kexts_being_added, - number_of_old_kexts_being_removed); - } else if (number_of_new_kexts_being_added > 0) { - s->Printf("Loading %d kext modules ", number_of_new_kexts_being_added); - } else if (number_of_old_kexts_being_removed > 0) { - s->Printf("Unloading %d kext modules ", - number_of_old_kexts_being_removed); - } - } - - if (log) { - if (load_kexts) { - log->Printf("DynamicLoaderDarwinKernel::ParseKextSummaries: %d kexts " - "added, %d kexts removed", - number_of_new_kexts_being_added, - number_of_old_kexts_being_removed); - } else { - log->Printf( - "DynamicLoaderDarwinKernel::ParseKextSummaries kext loading is " - "disabled, else would have %d kexts added, %d kexts removed", - number_of_new_kexts_being_added, number_of_old_kexts_being_removed); - } - } - - if (number_of_new_kexts_being_added > 0) { - ModuleList loaded_module_list; - - const uint32_t num_of_new_kexts = kext_summaries.size(); - for (uint32_t new_kext = 0; new_kext < num_of_new_kexts; new_kext++) { - if (to_be_added[new_kext]) { - KextImageInfo &image_info = kext_summaries[new_kext]; - if (load_kexts) { - if (!image_info.LoadImageUsingMemoryModule(m_process)) { - image_info.LoadImageAtFileAddress(m_process); - } - } - - m_known_kexts.push_back(image_info); - - if (image_info.GetModule() && - m_process->GetStopID() == image_info.GetProcessStopId()) - loaded_module_list.AppendIfNeeded(image_info.GetModule()); - - if (s && load_kexts) - s->Printf("."); - - if (log) - kext_summaries[new_kext].PutToLog(log); - } - } - m_process->GetTarget().ModulesDidLoad(loaded_module_list); - } - - if (number_of_old_kexts_being_removed > 0) { - ModuleList loaded_module_list; - const uint32_t num_of_old_kexts = m_known_kexts.size(); - for (uint32_t old_kext = 0; old_kext < num_of_old_kexts; old_kext++) { - ModuleList unloaded_module_list; - if (to_be_removed[old_kext]) { - KextImageInfo &image_info = m_known_kexts[old_kext]; - // You can't unload the kernel. - if (!image_info.IsKernel()) { - if (image_info.GetModule()) { - unloaded_module_list.AppendIfNeeded(image_info.GetModule()); - } - if (s) - s->Printf("."); - image_info.Clear(); - // should pull it out of the KextImageInfos vector but that would - // mutate the list and invalidate the to_be_removed bool vector; - // leaving it in place once Cleared() is relatively harmless. - } - } - m_process->GetTarget().ModulesDidUnload(unloaded_module_list, false); - } - } - - if (s && load_kexts) { - s->Printf(" done.\n"); - s->Flush(); - } - - return true; -} - -uint32_t DynamicLoaderDarwinKernel::ReadKextSummaries( - const Address &kext_summary_addr, uint32_t image_infos_count, - KextImageInfo::collection &image_infos) { - const ByteOrder endian = m_kernel.GetByteOrder(); - const uint32_t addr_size = m_kernel.GetAddressByteSize(); - - image_infos.resize(image_infos_count); - const size_t count = image_infos.size() * m_kext_summary_header.entry_size; - DataBufferHeap data(count, 0); - Status error; - - const bool prefer_file_cache = false; - const size_t bytes_read = m_process->GetTarget().ReadMemory( - kext_summary_addr, prefer_file_cache, data.GetBytes(), data.GetByteSize(), - error); - if (bytes_read == count) { - - DataExtractor extractor(data.GetBytes(), data.GetByteSize(), endian, - addr_size); - uint32_t i = 0; - for (uint32_t kext_summary_offset = 0; - i < image_infos.size() && - extractor.ValidOffsetForDataOfSize(kext_summary_offset, - m_kext_summary_header.entry_size); - ++i, kext_summary_offset += m_kext_summary_header.entry_size) { - lldb::offset_t offset = kext_summary_offset; - const void *name_data = - extractor.GetData(&offset, KERNEL_MODULE_MAX_NAME); - if (name_data == NULL) - break; - image_infos[i].SetName((const char *)name_data); - UUID uuid = UUID::fromOptionalData(extractor.GetData(&offset, 16), 16); - image_infos[i].SetUUID(uuid); - image_infos[i].SetLoadAddress(extractor.GetU64(&offset)); - image_infos[i].SetSize(extractor.GetU64(&offset)); - } - if (i < image_infos.size()) - image_infos.resize(i); - } else { - image_infos.clear(); - } - return image_infos.size(); -} - -bool DynamicLoaderDarwinKernel::ReadAllKextSummaries() { - std::lock_guard<std::recursive_mutex> guard(m_mutex); - - if (ReadKextSummaryHeader()) { - if (m_kext_summary_header.entry_count > 0 && - m_kext_summary_header_addr.IsValid()) { - Address summary_addr(m_kext_summary_header_addr); - summary_addr.Slide(m_kext_summary_header.GetSize()); - if (!ParseKextSummaries(summary_addr, - m_kext_summary_header.entry_count)) { - m_known_kexts.clear(); - } - return true; - } - } - return false; -} - -//---------------------------------------------------------------------- -// Dump an image info structure to the file handle provided. -//---------------------------------------------------------------------- -void DynamicLoaderDarwinKernel::KextImageInfo::PutToLog(Log *log) const { - if (m_load_address == LLDB_INVALID_ADDRESS) { - LLDB_LOG(log, "uuid={0} name=\"{1}\" (UNLOADED)", m_uuid.GetAsString(), - m_name); - } else { - LLDB_LOG(log, "addr={0:x+16} size={1:x+16} uuid={2} name=\"{3}\"", - m_load_address, m_size, m_uuid.GetAsString(), m_name); - } -} - -//---------------------------------------------------------------------- -// Dump the _dyld_all_image_infos members and all current image infos that we -// have parsed to the file handle provided. -//---------------------------------------------------------------------- -void DynamicLoaderDarwinKernel::PutToLog(Log *log) const { - if (log == NULL) - return; - - std::lock_guard<std::recursive_mutex> guard(m_mutex); - log->Printf("gLoadedKextSummaries = 0x%16.16" PRIx64 - " { version=%u, entry_size=%u, entry_count=%u }", - m_kext_summary_header_addr.GetFileAddress(), - m_kext_summary_header.version, m_kext_summary_header.entry_size, - m_kext_summary_header.entry_count); - - size_t i; - const size_t count = m_known_kexts.size(); - if (count > 0) { - log->PutCString("Loaded:"); - for (i = 0; i < count; i++) - m_known_kexts[i].PutToLog(log); - } -} - -void DynamicLoaderDarwinKernel::PrivateInitialize(Process *process) { - DEBUG_PRINTF("DynamicLoaderDarwinKernel::%s() process state = %s\n", - __FUNCTION__, StateAsCString(m_process->GetState())); - Clear(true); - m_process = process; -} - -void DynamicLoaderDarwinKernel::SetNotificationBreakpointIfNeeded() { - if (m_break_id == LLDB_INVALID_BREAK_ID && m_kernel.GetModule()) { - DEBUG_PRINTF("DynamicLoaderDarwinKernel::%s() process state = %s\n", - __FUNCTION__, StateAsCString(m_process->GetState())); - - const bool internal_bp = true; - const bool hardware = false; - const LazyBool skip_prologue = eLazyBoolNo; - FileSpecList module_spec_list; - module_spec_list.Append(m_kernel.GetModule()->GetFileSpec()); - Breakpoint *bp = - m_process->GetTarget() - .CreateBreakpoint(&module_spec_list, NULL, - "OSKextLoadedKextSummariesUpdated", - eFunctionNameTypeFull, eLanguageTypeUnknown, 0, - skip_prologue, internal_bp, hardware) - .get(); - - bp->SetCallback(DynamicLoaderDarwinKernel::BreakpointHitCallback, this, - true); - m_break_id = bp->GetID(); - } -} - -//---------------------------------------------------------------------- -// Member function that gets called when the process state changes. -//---------------------------------------------------------------------- -void DynamicLoaderDarwinKernel::PrivateProcessStateChanged(Process *process, - StateType state) { - DEBUG_PRINTF("DynamicLoaderDarwinKernel::%s(%s)\n", __FUNCTION__, - StateAsCString(state)); - switch (state) { - case eStateConnected: - case eStateAttaching: - case eStateLaunching: - case eStateInvalid: - case eStateUnloaded: - case eStateExited: - case eStateDetached: - Clear(false); - break; - - case eStateStopped: - UpdateIfNeeded(); - break; - - case eStateRunning: - case eStateStepping: - case eStateCrashed: - case eStateSuspended: - break; - } -} - -ThreadPlanSP -DynamicLoaderDarwinKernel::GetStepThroughTrampolinePlan(Thread &thread, - bool stop_others) { - ThreadPlanSP thread_plan_sp; - Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP)); - if (log) - log->Printf("Could not find symbol for step through."); - return thread_plan_sp; -} - -Status DynamicLoaderDarwinKernel::CanLoadImage() { - Status error; - error.SetErrorString( - "always unsafe to load or unload shared libraries in the darwin kernel"); - return error; -} - -void DynamicLoaderDarwinKernel::Initialize() { - PluginManager::RegisterPlugin(GetPluginNameStatic(), - GetPluginDescriptionStatic(), CreateInstance, - DebuggerInitialize); -} - -void DynamicLoaderDarwinKernel::Terminate() { - PluginManager::UnregisterPlugin(CreateInstance); -} - -void DynamicLoaderDarwinKernel::DebuggerInitialize( - lldb_private::Debugger &debugger) { - if (!PluginManager::GetSettingForDynamicLoaderPlugin( - debugger, DynamicLoaderDarwinKernelProperties::GetSettingName())) { - const bool is_global_setting = true; - PluginManager::CreateSettingForDynamicLoaderPlugin( - debugger, GetGlobalProperties()->GetValueProperties(), - ConstString("Properties for the DynamicLoaderDarwinKernel plug-in."), - is_global_setting); - } -} - -lldb_private::ConstString DynamicLoaderDarwinKernel::GetPluginNameStatic() { - static ConstString g_name("darwin-kernel"); - return g_name; -} - -const char *DynamicLoaderDarwinKernel::GetPluginDescriptionStatic() { - return "Dynamic loader plug-in that watches for shared library loads/unloads " - "in the MacOSX kernel."; -} - -//------------------------------------------------------------------ -// PluginInterface protocol -//------------------------------------------------------------------ -lldb_private::ConstString DynamicLoaderDarwinKernel::GetPluginName() { - return GetPluginNameStatic(); -} - -uint32_t DynamicLoaderDarwinKernel::GetPluginVersion() { return 1; } - -lldb::ByteOrder -DynamicLoaderDarwinKernel::GetByteOrderFromMagic(uint32_t magic) { - switch (magic) { - case llvm::MachO::MH_MAGIC: - case llvm::MachO::MH_MAGIC_64: - return endian::InlHostByteOrder(); - - case llvm::MachO::MH_CIGAM: - case llvm::MachO::MH_CIGAM_64: - if (endian::InlHostByteOrder() == lldb::eByteOrderBig) - return lldb::eByteOrderLittle; - else - return lldb::eByteOrderBig; - - default: - break; - } - return lldb::eByteOrderInvalid; -} diff --git a/source/Plugins/DynamicLoader/Darwin-Kernel/DynamicLoaderDarwinKernel.h b/source/Plugins/DynamicLoader/Darwin-Kernel/DynamicLoaderDarwinKernel.h deleted file mode 100644 index 7aacebd9b50f..000000000000 --- a/source/Plugins/DynamicLoader/Darwin-Kernel/DynamicLoaderDarwinKernel.h +++ /dev/null @@ -1,307 +0,0 @@ -//===-- DynamicLoaderDarwinKernel.h -----------------------------*- C++ -*-===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// - -#ifndef liblldb_DynamicLoaderDarwinKernel_h_ -#define liblldb_DynamicLoaderDarwinKernel_h_ - -#include <mutex> -#include <string> -#include <vector> - - -#include "lldb/Host/SafeMachO.h" - -#include "lldb/Target/DynamicLoader.h" -#include "lldb/Target/Process.h" -#include "lldb/Utility/FileSpec.h" -#include "lldb/Utility/UUID.h" - -class DynamicLoaderDarwinKernel : public lldb_private::DynamicLoader { -public: - DynamicLoaderDarwinKernel(lldb_private::Process *process, - lldb::addr_t kernel_addr); - - ~DynamicLoaderDarwinKernel() override; - - //------------------------------------------------------------------ - // Static Functions - //------------------------------------------------------------------ - static void Initialize(); - - static void Terminate(); - - static lldb_private::ConstString GetPluginNameStatic(); - - static const char *GetPluginDescriptionStatic(); - - static lldb_private::DynamicLoader * - CreateInstance(lldb_private::Process *process, bool force); - - static void DebuggerInitialize(lldb_private::Debugger &debugger); - - static lldb::addr_t SearchForDarwinKernel(lldb_private::Process *process); - - //------------------------------------------------------------------ - /// Called after attaching a process. - /// - /// Allow DynamicLoader plug-ins to execute some code after - /// attaching to a process. - //------------------------------------------------------------------ - void DidAttach() override; - - void DidLaunch() override; - - lldb::ThreadPlanSP GetStepThroughTrampolinePlan(lldb_private::Thread &thread, - bool stop_others) override; - - lldb_private::Status CanLoadImage() override; - - //------------------------------------------------------------------ - // PluginInterface protocol - //------------------------------------------------------------------ - lldb_private::ConstString GetPluginName() override; - - uint32_t GetPluginVersion() override; - -protected: - void PrivateInitialize(lldb_private::Process *process); - - void PrivateProcessStateChanged(lldb_private::Process *process, - lldb::StateType state); - - void UpdateIfNeeded(); - - void LoadKernelModuleIfNeeded(); - - void Clear(bool clear_process); - - void PutToLog(lldb_private::Log *log) const; - - static bool - BreakpointHitCallback(void *baton, - lldb_private::StoppointCallbackContext *context, - lldb::user_id_t break_id, lldb::user_id_t break_loc_id); - - bool BreakpointHit(lldb_private::StoppointCallbackContext *context, - lldb::user_id_t break_id, lldb::user_id_t break_loc_id); - uint32_t GetAddrByteSize() { return m_kernel.GetAddressByteSize(); } - - static lldb::ByteOrder GetByteOrderFromMagic(uint32_t magic); - - enum { - KERNEL_MODULE_MAX_NAME = 64u, - // Versions less than 2 didn't have an entry size, - // they had a 64 bit name, 16 byte UUID, 8 byte addr, - // 8 byte size, 8 byte version, 4 byte load tag, and - // 4 byte flags - KERNEL_MODULE_ENTRY_SIZE_VERSION_1 = 64u + 16u + 8u + 8u + 8u + 4u + 4u - }; - - // class KextImageInfo represents a single kext or kernel binary image. - // The class was designed to hold the information from the - // OSKextLoadedKextSummary - // structure (in libkern/libkern/OSKextLibPrivate.h from xnu). The kernel - // maintains - // a list of loded kexts in memory (the OSKextLoadedKextSummaryHeader - // structure, - // which points to an array of OSKextLoadedKextSummary's). - // - // A KextImageInfos may have - - // - // 1. The load address, name, UUID, and size of a kext/kernel binary in memory - // (read straight out of the kernel's list-of-kexts loaded) - // 2. A ModuleSP based on a MemoryModule read out of the kernel's memory - // (very unlikely to have any symbolic information) - // 3. A ModuleSP for an on-disk copy of the kext binary, possibly with debug - // info - // or a dSYM - // - // For performance reasons, the developer may prefer that lldb not load the - // kexts out - // of memory at the start of a kernel session. But we should build up / - // maintain a - // list of kexts that the kernel has told us about so we can relocate a kext - // module - // later if the user explicitly adds it to the target. - - class KextImageInfo { - public: - KextImageInfo() - : m_name(), m_module_sp(), m_memory_module_sp(), - m_load_process_stop_id(UINT32_MAX), m_uuid(), - m_load_address(LLDB_INVALID_ADDRESS), m_size(0), - m_kernel_image(false) {} - - void Clear() { - m_load_address = LLDB_INVALID_ADDRESS; - m_size = 0; - m_name.clear(); - m_uuid.Clear(); - m_module_sp.reset(); - m_memory_module_sp.reset(); - m_load_process_stop_id = UINT32_MAX; - } - - bool LoadImageAtFileAddress(lldb_private::Process *process); - - bool LoadImageUsingMemoryModule(lldb_private::Process *process); - - bool IsLoaded() { return m_load_process_stop_id != UINT32_MAX; } - - void SetLoadAddress( - lldb::addr_t load_addr); // Address of the Mach-O header for this binary - - lldb::addr_t - GetLoadAddress() const; // Address of the Mach-O header for this binary - - lldb_private::UUID GetUUID() const; - - void SetUUID(const lldb_private::UUID &uuid); - - void SetName(const char *); - - std::string GetName() const; - - void SetModule(lldb::ModuleSP module); - - lldb::ModuleSP GetModule(); - - // try to fill in m_memory_module_sp from memory based on the m_load_address - bool ReadMemoryModule(lldb_private::Process *process); - - bool IsKernel() - const; // true if this is the mach_kernel; false if this is a kext - - void SetIsKernel(bool is_kernel); - - uint64_t GetSize() const; - - void SetSize(uint64_t size); - - uint32_t - GetProcessStopId() const; // the stop-id when this binary was first noticed - - void SetProcessStopId(uint32_t stop_id); - - bool operator==(const KextImageInfo &rhs); - - uint32_t GetAddressByteSize(); // as determined by Mach-O header - - lldb::ByteOrder GetByteOrder(); // as determined by Mach-O header - - lldb_private::ArchSpec - GetArchitecture() const; // as determined by Mach-O header - - void PutToLog(lldb_private::Log *log) const; - - typedef std::vector<KextImageInfo> collection; - typedef collection::iterator iterator; - typedef collection::const_iterator const_iterator; - - private: - std::string m_name; - lldb::ModuleSP m_module_sp; - lldb::ModuleSP m_memory_module_sp; - uint32_t m_load_process_stop_id; // the stop-id when this module was added - // to the Target - lldb_private::UUID - m_uuid; // UUID for this dylib if it has one, else all zeros - lldb::addr_t m_load_address; - uint64_t m_size; - bool m_kernel_image; // true if this is the kernel, false if this is a kext - }; - - struct OSKextLoadedKextSummaryHeader { - uint32_t version; - uint32_t entry_size; - uint32_t entry_count; - lldb::addr_t image_infos_addr; - - OSKextLoadedKextSummaryHeader() - : version(0), entry_size(0), entry_count(0), - image_infos_addr(LLDB_INVALID_ADDRESS) {} - - uint32_t GetSize() { - switch (version) { - case 0: - return 0; // Can't know the size without a valid version - case 1: - return 8; // Version 1 only had a version + entry_count - default: - break; - } - // Version 2 and above has version, entry_size, entry_count, and reserved - return 16; - } - - void Clear() { - version = 0; - entry_size = 0; - entry_count = 0; - image_infos_addr = LLDB_INVALID_ADDRESS; - } - - bool IsValid() const { return version >= 1 || version <= 2; } - }; - - void RegisterNotificationCallbacks(); - - void UnregisterNotificationCallbacks(); - - void SetNotificationBreakpointIfNeeded(); - - bool ReadAllKextSummaries(); - - bool ReadKextSummaryHeader(); - - bool ParseKextSummaries(const lldb_private::Address &kext_summary_addr, - uint32_t count); - - void - UpdateImageInfosHeaderAndLoadCommands(KextImageInfo::collection &image_infos, - uint32_t infos_count, - bool update_executable); - - uint32_t ReadKextSummaries(const lldb_private::Address &kext_summary_addr, - uint32_t image_infos_count, - KextImageInfo::collection &image_infos); - - static lldb::addr_t - SearchForKernelAtSameLoadAddr(lldb_private::Process *process); - - static lldb::addr_t - SearchForKernelWithDebugHints(lldb_private::Process *process); - - static lldb::addr_t SearchForKernelNearPC(lldb_private::Process *process); - - static lldb::addr_t - SearchForKernelViaExhaustiveSearch(lldb_private::Process *process); - - static bool - ReadMachHeader(lldb::addr_t addr, lldb_private::Process *process, llvm::MachO::mach_header &mh); - - static lldb_private::UUID - CheckForKernelImageAtAddress(lldb::addr_t addr, - lldb_private::Process *process); - - lldb::addr_t m_kernel_load_address; - KextImageInfo m_kernel; // Info about the current kernel image being used - - lldb_private::Address m_kext_summary_header_ptr_addr; - lldb_private::Address m_kext_summary_header_addr; - OSKextLoadedKextSummaryHeader m_kext_summary_header; - KextImageInfo::collection m_known_kexts; - mutable std::recursive_mutex m_mutex; - lldb::user_id_t m_break_id; - -private: - DISALLOW_COPY_AND_ASSIGN(DynamicLoaderDarwinKernel); -}; - -#endif // liblldb_DynamicLoaderDarwinKernel_h_ diff --git a/source/Plugins/DynamicLoader/Hexagon-DYLD/CMakeLists.txt b/source/Plugins/DynamicLoader/Hexagon-DYLD/CMakeLists.txt deleted file mode 100644 index c590457d0a54..000000000000 --- a/source/Plugins/DynamicLoader/Hexagon-DYLD/CMakeLists.txt +++ /dev/null @@ -1,10 +0,0 @@ -add_lldb_library(lldbPluginDynamicLoaderHexagonDYLD PLUGIN - HexagonDYLDRendezvous.cpp - DynamicLoaderHexagonDYLD.cpp - - LINK_LIBS - lldbBreakpoint - lldbCore - lldbSymbol - lldbTarget - ) diff --git a/source/Plugins/DynamicLoader/MacOSX-DYLD/CMakeLists.txt b/source/Plugins/DynamicLoader/MacOSX-DYLD/CMakeLists.txt deleted file mode 100644 index 515c82dcaca9..000000000000 --- a/source/Plugins/DynamicLoader/MacOSX-DYLD/CMakeLists.txt +++ /dev/null @@ -1,16 +0,0 @@ -add_lldb_library(lldbPluginDynamicLoaderMacOSXDYLD PLUGIN - DynamicLoaderMacOSXDYLD.cpp - DynamicLoaderMacOS.cpp - DynamicLoaderDarwin.cpp - - LINK_LIBS - lldbBreakpoint - lldbCore - lldbExpression - lldbHost - lldbSymbol - lldbTarget - lldbUtility - LINK_COMPONENTS - Support - ) diff --git a/source/Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderDarwin.cpp b/source/Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderDarwin.cpp deleted file mode 100644 index 944be9633e00..000000000000 --- a/source/Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderDarwin.cpp +++ /dev/null @@ -1,1143 +0,0 @@ -//===-- DynamicLoaderDarwin.cpp -----------------------------*- C++ -*-===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// - -#include "DynamicLoaderDarwin.h" - -#include "lldb/Breakpoint/StoppointCallbackContext.h" -#include "lldb/Core/Debugger.h" -#include "lldb/Core/Module.h" -#include "lldb/Core/ModuleSpec.h" -#include "lldb/Core/PluginManager.h" -#include "lldb/Core/Section.h" -#include "lldb/Expression/DiagnosticManager.h" -#include "lldb/Host/FileSystem.h" -#include "lldb/Symbol/ClangASTContext.h" -#include "lldb/Symbol/Function.h" -#include "lldb/Symbol/ObjectFile.h" -#include "lldb/Target/ABI.h" -#include "lldb/Target/ObjCLanguageRuntime.h" -#include "lldb/Target/RegisterContext.h" -#include "lldb/Target/StackFrame.h" -#include "lldb/Target/Target.h" -#include "lldb/Target/Thread.h" -#include "lldb/Target/ThreadPlanCallFunction.h" -#include "lldb/Target/ThreadPlanRunToAddress.h" -#include "lldb/Utility/DataBuffer.h" -#include "lldb/Utility/DataBufferHeap.h" -#include "lldb/Utility/Log.h" -#include "lldb/Utility/State.h" - -//#define ENABLE_DEBUG_PRINTF // COMMENT THIS LINE OUT PRIOR TO CHECKIN -#ifdef ENABLE_DEBUG_PRINTF -#include <stdio.h> -#define DEBUG_PRINTF(fmt, ...) printf(fmt, ##__VA_ARGS__) -#else -#define DEBUG_PRINTF(fmt, ...) -#endif - -#ifndef __APPLE__ -#include "Utility/UuidCompatibility.h" -#else -#include <uuid/uuid.h> -#endif - -using namespace lldb; -using namespace lldb_private; - -//---------------------------------------------------------------------- -// Constructor -//---------------------------------------------------------------------- -DynamicLoaderDarwin::DynamicLoaderDarwin(Process *process) - : DynamicLoader(process), m_dyld_module_wp(), m_libpthread_module_wp(), - m_pthread_getspecific_addr(), m_tid_to_tls_map(), m_dyld_image_infos(), - m_dyld_image_infos_stop_id(UINT32_MAX), m_dyld(), m_mutex() {} - -//---------------------------------------------------------------------- -// Destructor -//---------------------------------------------------------------------- -DynamicLoaderDarwin::~DynamicLoaderDarwin() {} - -//------------------------------------------------------------------ -/// Called after attaching a process. -/// -/// Allow DynamicLoader plug-ins to execute some code after -/// attaching to a process. -//------------------------------------------------------------------ -void DynamicLoaderDarwin::DidAttach() { - PrivateInitialize(m_process); - DoInitialImageFetch(); - SetNotificationBreakpoint(); -} - -//------------------------------------------------------------------ -/// Called after attaching a process. -/// -/// Allow DynamicLoader plug-ins to execute some code after -/// attaching to a process. -//------------------------------------------------------------------ -void DynamicLoaderDarwin::DidLaunch() { - PrivateInitialize(m_process); - DoInitialImageFetch(); - SetNotificationBreakpoint(); -} - -//---------------------------------------------------------------------- -// Clear out the state of this class. -//---------------------------------------------------------------------- -void DynamicLoaderDarwin::Clear(bool clear_process) { - std::lock_guard<std::recursive_mutex> guard(m_mutex); - if (clear_process) - m_process = NULL; - m_dyld_image_infos.clear(); - m_dyld_image_infos_stop_id = UINT32_MAX; - m_dyld.Clear(false); -} - -ModuleSP DynamicLoaderDarwin::FindTargetModuleForImageInfo( - ImageInfo &image_info, bool can_create, bool *did_create_ptr) { - if (did_create_ptr) - *did_create_ptr = false; - - Target &target = m_process->GetTarget(); - const ModuleList &target_images = target.GetImages(); - ModuleSpec module_spec(image_info.file_spec); - module_spec.GetUUID() = image_info.uuid; - ModuleSP module_sp(target_images.FindFirstModule(module_spec)); - - if (module_sp && !module_spec.GetUUID().IsValid() && - !module_sp->GetUUID().IsValid()) { - // No UUID, we must rely upon the cached module modification time and the - // modification time of the file on disk - if (module_sp->GetModificationTime() != - FileSystem::Instance().GetModificationTime(module_sp->GetFileSpec())) - module_sp.reset(); - } - - if (!module_sp) { - if (can_create) { - module_sp = target.GetSharedModule(module_spec); - if (!module_sp || module_sp->GetObjectFile() == NULL) - module_sp = m_process->ReadModuleFromMemory(image_info.file_spec, - image_info.address); - - if (did_create_ptr) - *did_create_ptr = (bool)module_sp; - } - } - return module_sp; -} - -void DynamicLoaderDarwin::UnloadImages( - const std::vector<lldb::addr_t> &solib_addresses) { - std::lock_guard<std::recursive_mutex> guard(m_mutex); - if (m_process->GetStopID() == m_dyld_image_infos_stop_id) - return; - - Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER)); - Target &target = m_process->GetTarget(); - if (log) - log->Printf("Removing %" PRId64 " modules.", - (uint64_t)solib_addresses.size()); - - ModuleList unloaded_module_list; - - for (addr_t solib_addr : solib_addresses) { - Address header; - if (header.SetLoadAddress(solib_addr, &target)) { - if (header.GetOffset() == 0) { - ModuleSP module_to_remove(header.GetModule()); - if (module_to_remove.get()) { - if (log) - log->Printf("Removing module at address 0x%" PRIx64, solib_addr); - // remove the sections from the Target - UnloadSections(module_to_remove); - // add this to the list of modules to remove - unloaded_module_list.AppendIfNeeded(module_to_remove); - // remove the entry from the m_dyld_image_infos - ImageInfo::collection::iterator pos, end = m_dyld_image_infos.end(); - for (pos = m_dyld_image_infos.begin(); pos != end; pos++) { - if (solib_addr == (*pos).address) { - m_dyld_image_infos.erase(pos); - break; - } - } - } - } - } - } - - if (unloaded_module_list.GetSize() > 0) { - if (log) { - log->PutCString("Unloaded:"); - unloaded_module_list.LogUUIDAndPaths( - log, "DynamicLoaderDarwin::UnloadModules"); - } - m_process->GetTarget().GetImages().Remove(unloaded_module_list); - m_dyld_image_infos_stop_id = m_process->GetStopID(); - } -} - -void DynamicLoaderDarwin::UnloadAllImages() { - Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER)); - ModuleList unloaded_modules_list; - - Target &target = m_process->GetTarget(); - const ModuleList &target_modules = target.GetImages(); - std::lock_guard<std::recursive_mutex> guard(target_modules.GetMutex()); - - size_t num_modules = target_modules.GetSize(); - ModuleSP dyld_sp(GetDYLDModule()); - - for (size_t i = 0; i < num_modules; i++) { - ModuleSP module_sp = target_modules.GetModuleAtIndexUnlocked(i); - - // Don't remove dyld - else we'll lose our breakpoint notifying us about - // libraries being re-loaded... - if (module_sp.get() != nullptr && module_sp.get() != dyld_sp.get()) { - UnloadSections(module_sp); - unloaded_modules_list.Append(module_sp); - } - } - - if (unloaded_modules_list.GetSize() != 0) { - if (log) { - log->PutCString("Unloaded:"); - unloaded_modules_list.LogUUIDAndPaths( - log, "DynamicLoaderDarwin::UnloadAllImages"); - } - target.GetImages().Remove(unloaded_modules_list); - m_dyld_image_infos.clear(); - m_dyld_image_infos_stop_id = m_process->GetStopID(); - } -} - -//---------------------------------------------------------------------- -// Update the load addresses for all segments in MODULE using the updated INFO -// that is passed in. -//---------------------------------------------------------------------- -bool DynamicLoaderDarwin::UpdateImageLoadAddress(Module *module, - ImageInfo &info) { - bool changed = false; - if (module) { - ObjectFile *image_object_file = module->GetObjectFile(); - if (image_object_file) { - SectionList *section_list = image_object_file->GetSectionList(); - if (section_list) { - std::vector<uint32_t> inaccessible_segment_indexes; - // We now know the slide amount, so go through all sections and update - // the load addresses with the correct values. - const size_t num_segments = info.segments.size(); - for (size_t i = 0; i < num_segments; ++i) { - // Only load a segment if it has protections. Things like __PAGEZERO - // don't have any protections, and they shouldn't be slid - SectionSP section_sp( - section_list->FindSectionByName(info.segments[i].name)); - - if (info.segments[i].maxprot == 0) { - inaccessible_segment_indexes.push_back(i); - } else { - const addr_t new_section_load_addr = - info.segments[i].vmaddr + info.slide; - static ConstString g_section_name_LINKEDIT("__LINKEDIT"); - - if (section_sp) { - // __LINKEDIT sections from files in the shared cache can overlap - // so check to see what the segment name is and pass "false" so - // we don't warn of overlapping "Section" objects, and "true" for - // all other sections. - const bool warn_multiple = - section_sp->GetName() != g_section_name_LINKEDIT; - - changed = m_process->GetTarget().SetSectionLoadAddress( - section_sp, new_section_load_addr, warn_multiple); - } else { - Host::SystemLog( - Host::eSystemLogWarning, - "warning: unable to find and load segment named '%s' at " - "0x%" PRIx64 " in '%s' in macosx dynamic loader plug-in.\n", - info.segments[i].name.AsCString("<invalid>"), - (uint64_t)new_section_load_addr, - image_object_file->GetFileSpec().GetPath().c_str()); - } - } - } - - // If the loaded the file (it changed) and we have segments that are - // not readable or writeable, add them to the invalid memory region - // cache for the process. This will typically only be the __PAGEZERO - // segment in the main executable. We might be able to apply this more - // generally to more sections that have no protections in the future, - // but for now we are going to just do __PAGEZERO. - if (changed && !inaccessible_segment_indexes.empty()) { - for (uint32_t i = 0; i < inaccessible_segment_indexes.size(); ++i) { - const uint32_t seg_idx = inaccessible_segment_indexes[i]; - SectionSP section_sp( - section_list->FindSectionByName(info.segments[seg_idx].name)); - - if (section_sp) { - static ConstString g_pagezero_section_name("__PAGEZERO"); - if (g_pagezero_section_name == section_sp->GetName()) { - // __PAGEZERO never slides... - const lldb::addr_t vmaddr = info.segments[seg_idx].vmaddr; - const lldb::addr_t vmsize = info.segments[seg_idx].vmsize; - Process::LoadRange pagezero_range(vmaddr, vmsize); - m_process->AddInvalidMemoryRegion(pagezero_range); - } - } - } - } - } - } - } - // We might have an in memory image that was loaded as soon as it was created - if (info.load_stop_id == m_process->GetStopID()) - changed = true; - else if (changed) { - // Update the stop ID when this library was updated - info.load_stop_id = m_process->GetStopID(); - } - return changed; -} - -//---------------------------------------------------------------------- -// Unload the segments in MODULE using the INFO that is passed in. -//---------------------------------------------------------------------- -bool DynamicLoaderDarwin::UnloadModuleSections(Module *module, - ImageInfo &info) { - bool changed = false; - if (module) { - ObjectFile *image_object_file = module->GetObjectFile(); - if (image_object_file) { - SectionList *section_list = image_object_file->GetSectionList(); - if (section_list) { - const size_t num_segments = info.segments.size(); - for (size_t i = 0; i < num_segments; ++i) { - SectionSP section_sp( - section_list->FindSectionByName(info.segments[i].name)); - if (section_sp) { - const addr_t old_section_load_addr = - info.segments[i].vmaddr + info.slide; - if (m_process->GetTarget().SetSectionUnloaded( - section_sp, old_section_load_addr)) - changed = true; - } else { - Host::SystemLog(Host::eSystemLogWarning, - "warning: unable to find and unload segment named " - "'%s' in '%s' in macosx dynamic loader plug-in.\n", - info.segments[i].name.AsCString("<invalid>"), - image_object_file->GetFileSpec().GetPath().c_str()); - } - } - } - } - } - return changed; -} - -// Given a JSON dictionary (from debugserver, most likely) of binary images -// loaded in the inferior process, add the images to the ImageInfo collection. - -bool DynamicLoaderDarwin::JSONImageInformationIntoImageInfo( - StructuredData::ObjectSP image_details, - ImageInfo::collection &image_infos) { - StructuredData::ObjectSP images_sp = - image_details->GetAsDictionary()->GetValueForKey("images"); - if (images_sp.get() == nullptr) - return false; - - image_infos.resize(images_sp->GetAsArray()->GetSize()); - - for (size_t i = 0; i < image_infos.size(); i++) { - StructuredData::ObjectSP image_sp = - images_sp->GetAsArray()->GetItemAtIndex(i); - if (image_sp.get() == nullptr || image_sp->GetAsDictionary() == nullptr) - return false; - StructuredData::Dictionary *image = image_sp->GetAsDictionary(); - // clang-format off - if (!image->HasKey("load_address") || - !image->HasKey("pathname") || - !image->HasKey("mod_date") || - !image->HasKey("mach_header") || - image->GetValueForKey("mach_header")->GetAsDictionary() == nullptr || - !image->HasKey("segments") || - image->GetValueForKey("segments")->GetAsArray() == nullptr || - !image->HasKey("uuid")) { - return false; - } - // clang-format on - image_infos[i].address = - image->GetValueForKey("load_address")->GetAsInteger()->GetValue(); - image_infos[i].mod_date = - image->GetValueForKey("mod_date")->GetAsInteger()->GetValue(); - image_infos[i].file_spec.SetFile( - image->GetValueForKey("pathname")->GetAsString()->GetValue(), - FileSpec::Style::native); - - StructuredData::Dictionary *mh = - image->GetValueForKey("mach_header")->GetAsDictionary(); - image_infos[i].header.magic = - mh->GetValueForKey("magic")->GetAsInteger()->GetValue(); - image_infos[i].header.cputype = - mh->GetValueForKey("cputype")->GetAsInteger()->GetValue(); - image_infos[i].header.cpusubtype = - mh->GetValueForKey("cpusubtype")->GetAsInteger()->GetValue(); - image_infos[i].header.filetype = - mh->GetValueForKey("filetype")->GetAsInteger()->GetValue(); - - if (image->HasKey("min_version_os_name")) { - std::string os_name = image->GetValueForKey("min_version_os_name") - ->GetAsString() - ->GetValue(); - if (os_name == "macosx") - image_infos[i].os_type = llvm::Triple::MacOSX; - else if (os_name == "ios" || os_name == "iphoneos") - image_infos[i].os_type = llvm::Triple::IOS; - else if (os_name == "tvos") - image_infos[i].os_type = llvm::Triple::TvOS; - else if (os_name == "watchos") - image_infos[i].os_type = llvm::Triple::WatchOS; - // NEED_BRIDGEOS_TRIPLE else if (os_name == "bridgeos") - // NEED_BRIDGEOS_TRIPLE image_infos[i].os_type = llvm::Triple::BridgeOS; - } - if (image->HasKey("min_version_os_sdk")) { - image_infos[i].min_version_os_sdk = - image->GetValueForKey("min_version_os_sdk") - ->GetAsString() - ->GetValue(); - } - - // Fields that aren't used by DynamicLoaderDarwin so debugserver doesn't - // currently send them in the reply. - - if (mh->HasKey("flags")) - image_infos[i].header.flags = - mh->GetValueForKey("flags")->GetAsInteger()->GetValue(); - else - image_infos[i].header.flags = 0; - - if (mh->HasKey("ncmds")) - image_infos[i].header.ncmds = - mh->GetValueForKey("ncmds")->GetAsInteger()->GetValue(); - else - image_infos[i].header.ncmds = 0; - - if (mh->HasKey("sizeofcmds")) - image_infos[i].header.sizeofcmds = - mh->GetValueForKey("sizeofcmds")->GetAsInteger()->GetValue(); - else - image_infos[i].header.sizeofcmds = 0; - - StructuredData::Array *segments = - image->GetValueForKey("segments")->GetAsArray(); - uint32_t segcount = segments->GetSize(); - for (size_t j = 0; j < segcount; j++) { - Segment segment; - StructuredData::Dictionary *seg = - segments->GetItemAtIndex(j)->GetAsDictionary(); - segment.name = - ConstString(seg->GetValueForKey("name")->GetAsString()->GetValue()); - segment.vmaddr = - seg->GetValueForKey("vmaddr")->GetAsInteger()->GetValue(); - segment.vmsize = - seg->GetValueForKey("vmsize")->GetAsInteger()->GetValue(); - segment.fileoff = - seg->GetValueForKey("fileoff")->GetAsInteger()->GetValue(); - segment.filesize = - seg->GetValueForKey("filesize")->GetAsInteger()->GetValue(); - segment.maxprot = - seg->GetValueForKey("maxprot")->GetAsInteger()->GetValue(); - - // Fields that aren't used by DynamicLoaderDarwin so debugserver doesn't - // currently send them in the reply. - - if (seg->HasKey("initprot")) - segment.initprot = - seg->GetValueForKey("initprot")->GetAsInteger()->GetValue(); - else - segment.initprot = 0; - - if (seg->HasKey("flags")) - segment.flags = - seg->GetValueForKey("flags")->GetAsInteger()->GetValue(); - else - segment.flags = 0; - - if (seg->HasKey("nsects")) - segment.nsects = - seg->GetValueForKey("nsects")->GetAsInteger()->GetValue(); - else - segment.nsects = 0; - - image_infos[i].segments.push_back(segment); - } - - image_infos[i].uuid.SetFromStringRef( - image->GetValueForKey("uuid")->GetAsString()->GetValue()); - - // All sections listed in the dyld image info structure will all either be - // fixed up already, or they will all be off by a single slide amount that - // is determined by finding the first segment that is at file offset zero - // which also has bytes (a file size that is greater than zero) in the - // object file. - - // Determine the slide amount (if any) - const size_t num_sections = image_infos[i].segments.size(); - for (size_t k = 0; k < num_sections; ++k) { - // Iterate through the object file sections to find the first section - // that starts of file offset zero and that has bytes in the file... - if ((image_infos[i].segments[k].fileoff == 0 && - image_infos[i].segments[k].filesize > 0) || - (image_infos[i].segments[k].name == ConstString("__TEXT"))) { - image_infos[i].slide = - image_infos[i].address - image_infos[i].segments[k].vmaddr; - // We have found the slide amount, so we can exit this for loop. - break; - } - } - } - - return true; -} - -void DynamicLoaderDarwin::UpdateSpecialBinariesFromNewImageInfos( - ImageInfo::collection &image_infos) { - uint32_t exe_idx = UINT32_MAX; - uint32_t dyld_idx = UINT32_MAX; - Target &target = m_process->GetTarget(); - Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER)); - ConstString g_dyld_sim_filename("dyld_sim"); - - ArchSpec target_arch = target.GetArchitecture(); - const size_t image_infos_size = image_infos.size(); - for (size_t i = 0; i < image_infos_size; i++) { - if (image_infos[i].header.filetype == llvm::MachO::MH_DYLINKER) { - // In a "simulator" process (an x86 process that is - // ios/tvos/watchos/bridgeos) we will have two dyld modules -- - // a "dyld" that we want to keep track of, and a "dyld_sim" which - // we don't need to keep track of here. If the target is an x86 - // system and the OS of the dyld binary is ios/tvos/watchos/bridgeos, - // then we are looking at dyld_sym. - - // debugserver has only recently (late 2016) started sending up the os - // type for each binary it sees -- so if we don't have an os type, use a - // filename check as our next best guess. - if (image_infos[i].os_type == llvm::Triple::OSType::UnknownOS) { - if (image_infos[i].file_spec.GetFilename() != g_dyld_sim_filename) { - dyld_idx = i; - } - } else if (target_arch.GetTriple().getArch() == llvm::Triple::x86 || - target_arch.GetTriple().getArch() == llvm::Triple::x86_64) { - if (image_infos[i].os_type != llvm::Triple::OSType::IOS && - image_infos[i].os_type != llvm::Triple::TvOS && - image_infos[i].os_type != llvm::Triple::WatchOS) { - // NEED_BRIDGEOS_TRIPLE image_infos[i].os_type != llvm::Triple::BridgeOS) { - dyld_idx = i; - } - } - else { - // catch-all for any other environment -- trust that dyld is actually - // dyld - dyld_idx = i; - } - } else if (image_infos[i].header.filetype == llvm::MachO::MH_EXECUTE) { - exe_idx = i; - } - } - - if (exe_idx != UINT32_MAX) { - const bool can_create = true; - ModuleSP exe_module_sp( - FindTargetModuleForImageInfo(image_infos[exe_idx], can_create, NULL)); - if (exe_module_sp) { - if (log) - log->Printf("Found executable module: %s", - exe_module_sp->GetFileSpec().GetPath().c_str()); - target.GetImages().AppendIfNeeded(exe_module_sp); - UpdateImageLoadAddress(exe_module_sp.get(), image_infos[exe_idx]); - if (exe_module_sp.get() != target.GetExecutableModulePointer()) { - target.SetExecutableModule(exe_module_sp, eLoadDependentsNo); - } - } - } - - if (dyld_idx != UINT32_MAX) { - const bool can_create = true; - ModuleSP dyld_sp = - FindTargetModuleForImageInfo(image_infos[dyld_idx], can_create, NULL); - if (dyld_sp.get()) { - if (log) - log->Printf("Found dyld module: %s", - dyld_sp->GetFileSpec().GetPath().c_str()); - target.GetImages().AppendIfNeeded(dyld_sp); - UpdateImageLoadAddress(dyld_sp.get(), image_infos[dyld_idx]); - SetDYLDModule(dyld_sp); - } - } -} - -void DynamicLoaderDarwin::UpdateDYLDImageInfoFromNewImageInfo( - ImageInfo &image_info) { - if (image_info.header.filetype == llvm::MachO::MH_DYLINKER) { - const bool can_create = true; - ModuleSP dyld_sp = - FindTargetModuleForImageInfo(image_info, can_create, NULL); - if (dyld_sp.get()) { - Target &target = m_process->GetTarget(); - target.GetImages().AppendIfNeeded(dyld_sp); - UpdateImageLoadAddress(dyld_sp.get(), image_info); - SetDYLDModule(dyld_sp); - } - } -} - -void DynamicLoaderDarwin::SetDYLDModule(lldb::ModuleSP &dyld_module_sp) { - m_dyld_module_wp = dyld_module_sp; -} - -ModuleSP DynamicLoaderDarwin::GetDYLDModule() { - ModuleSP dyld_sp(m_dyld_module_wp.lock()); - return dyld_sp; -} - -bool DynamicLoaderDarwin::AddModulesUsingImageInfos( - ImageInfo::collection &image_infos) { - std::lock_guard<std::recursive_mutex> guard(m_mutex); - // Now add these images to the main list. - ModuleList loaded_module_list; - Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER)); - Target &target = m_process->GetTarget(); - ModuleList &target_images = target.GetImages(); - - for (uint32_t idx = 0; idx < image_infos.size(); ++idx) { - if (log) { - log->Printf("Adding new image at address=0x%16.16" PRIx64 ".", - image_infos[idx].address); - image_infos[idx].PutToLog(log); - } - - m_dyld_image_infos.push_back(image_infos[idx]); - - ModuleSP image_module_sp( - FindTargetModuleForImageInfo(image_infos[idx], true, NULL)); - - if (image_module_sp) { - ObjectFile *objfile = image_module_sp->GetObjectFile(); - if (objfile) { - SectionList *sections = objfile->GetSectionList(); - if (sections) { - ConstString commpage_dbstr("__commpage"); - Section *commpage_section = - sections->FindSectionByName(commpage_dbstr).get(); - if (commpage_section) { - ModuleSpec module_spec(objfile->GetFileSpec(), - image_infos[idx].GetArchitecture()); - module_spec.GetObjectName() = commpage_dbstr; - ModuleSP commpage_image_module_sp( - target_images.FindFirstModule(module_spec)); - if (!commpage_image_module_sp) { - module_spec.SetObjectOffset(objfile->GetFileOffset() + - commpage_section->GetFileOffset()); - module_spec.SetObjectSize(objfile->GetByteSize()); - commpage_image_module_sp = target.GetSharedModule(module_spec); - if (!commpage_image_module_sp || - commpage_image_module_sp->GetObjectFile() == NULL) { - commpage_image_module_sp = m_process->ReadModuleFromMemory( - image_infos[idx].file_spec, image_infos[idx].address); - // Always load a memory image right away in the target in case - // we end up trying to read the symbol table from memory... The - // __LINKEDIT will need to be mapped so we can figure out where - // the symbol table bits are... - bool changed = false; - UpdateImageLoadAddress(commpage_image_module_sp.get(), - image_infos[idx]); - target.GetImages().Append(commpage_image_module_sp); - if (changed) { - image_infos[idx].load_stop_id = m_process->GetStopID(); - loaded_module_list.AppendIfNeeded(commpage_image_module_sp); - } - } - } - } - } - } - - // UpdateImageLoadAddress will return true if any segments change load - // address. We need to check this so we don't mention that all loaded - // shared libraries are newly loaded each time we hit out dyld breakpoint - // since dyld will list all shared libraries each time. - if (UpdateImageLoadAddress(image_module_sp.get(), image_infos[idx])) { - target_images.AppendIfNeeded(image_module_sp); - loaded_module_list.AppendIfNeeded(image_module_sp); - } - } - } - - if (loaded_module_list.GetSize() > 0) { - if (log) - loaded_module_list.LogUUIDAndPaths(log, - "DynamicLoaderDarwin::ModulesDidLoad"); - m_process->GetTarget().ModulesDidLoad(loaded_module_list); - } - return true; -} - -//---------------------------------------------------------------------- -// On Mac OS X libobjc (the Objective-C runtime) has several critical dispatch -// functions written in hand-written assembly, and also have hand-written -// unwind information in the eh_frame section. Normally we prefer analyzing -// the assembly instructions of a currently executing frame to unwind from that -// frame -- but on hand-written functions this profiling can fail. We should -// use the eh_frame instructions for these functions all the time. -// -// As an aside, it would be better if the eh_frame entries had a flag (or were -// extensible so they could have an Apple-specific flag) which indicates that -// the instructions are asynchronous -- accurate at every instruction, instead -// of our normal default assumption that they are not. -//---------------------------------------------------------------------- - -bool DynamicLoaderDarwin::AlwaysRelyOnEHUnwindInfo(SymbolContext &sym_ctx) { - ModuleSP module_sp; - if (sym_ctx.symbol) { - module_sp = sym_ctx.symbol->GetAddressRef().GetModule(); - } - if (module_sp.get() == NULL && sym_ctx.function) { - module_sp = - sym_ctx.function->GetAddressRange().GetBaseAddress().GetModule(); - } - if (module_sp.get() == NULL) - return false; - - ObjCLanguageRuntime *objc_runtime = m_process->GetObjCLanguageRuntime(); - return objc_runtime != NULL && objc_runtime->IsModuleObjCLibrary(module_sp); -} - -//---------------------------------------------------------------------- -// Dump a Segment to the file handle provided. -//---------------------------------------------------------------------- -void DynamicLoaderDarwin::Segment::PutToLog(Log *log, - lldb::addr_t slide) const { - if (log) { - if (slide == 0) - log->Printf("\t\t%16s [0x%16.16" PRIx64 " - 0x%16.16" PRIx64 ")", - name.AsCString(""), vmaddr + slide, vmaddr + slide + vmsize); - else - log->Printf("\t\t%16s [0x%16.16" PRIx64 " - 0x%16.16" PRIx64 - ") slide = 0x%" PRIx64, - name.AsCString(""), vmaddr + slide, vmaddr + slide + vmsize, - slide); - } -} - -const DynamicLoaderDarwin::Segment * -DynamicLoaderDarwin::ImageInfo::FindSegment(const ConstString &name) const { - const size_t num_segments = segments.size(); - for (size_t i = 0; i < num_segments; ++i) { - if (segments[i].name == name) - return &segments[i]; - } - return NULL; -} - -//---------------------------------------------------------------------- -// Dump an image info structure to the file handle provided. -//---------------------------------------------------------------------- -void DynamicLoaderDarwin::ImageInfo::PutToLog(Log *log) const { - if (!log) - return; - if (address == LLDB_INVALID_ADDRESS) { - LLDB_LOG(log, "modtime={0:x+8} uuid={1} path='{2}' (UNLOADED)", mod_date, - uuid.GetAsString(), file_spec.GetPath()); - } else { - LLDB_LOG(log, "address={0:x+16} modtime={1:x+8} uuid={2} path='{3}'", - address, mod_date, uuid.GetAsString(), file_spec.GetPath()); - for (uint32_t i = 0; i < segments.size(); ++i) - segments[i].PutToLog(log, slide); - } -} - -void DynamicLoaderDarwin::PrivateInitialize(Process *process) { - DEBUG_PRINTF("DynamicLoaderDarwin::%s() process state = %s\n", __FUNCTION__, - StateAsCString(m_process->GetState())); - Clear(true); - m_process = process; - m_process->GetTarget().ClearAllLoadedSections(); -} - -//---------------------------------------------------------------------- -// Member function that gets called when the process state changes. -//---------------------------------------------------------------------- -void DynamicLoaderDarwin::PrivateProcessStateChanged(Process *process, - StateType state) { - DEBUG_PRINTF("DynamicLoaderDarwin::%s(%s)\n", __FUNCTION__, - StateAsCString(state)); - switch (state) { - case eStateConnected: - case eStateAttaching: - case eStateLaunching: - case eStateInvalid: - case eStateUnloaded: - case eStateExited: - case eStateDetached: - Clear(false); - break; - - case eStateStopped: - // Keep trying find dyld and set our notification breakpoint each time we - // stop until we succeed - if (!DidSetNotificationBreakpoint() && m_process->IsAlive()) { - if (NeedToDoInitialImageFetch()) - DoInitialImageFetch(); - - SetNotificationBreakpoint(); - } - break; - - case eStateRunning: - case eStateStepping: - case eStateCrashed: - case eStateSuspended: - break; - } -} - -ThreadPlanSP -DynamicLoaderDarwin::GetStepThroughTrampolinePlan(Thread &thread, - bool stop_others) { - ThreadPlanSP thread_plan_sp; - StackFrame *current_frame = thread.GetStackFrameAtIndex(0).get(); - const SymbolContext ¤t_context = - current_frame->GetSymbolContext(eSymbolContextSymbol); - Symbol *current_symbol = current_context.symbol; - Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP)); - TargetSP target_sp(thread.CalculateTarget()); - - if (current_symbol != NULL) { - std::vector<Address> addresses; - - if (current_symbol->IsTrampoline()) { - const ConstString &trampoline_name = current_symbol->GetMangled().GetName( - current_symbol->GetLanguage(), Mangled::ePreferMangled); - - if (trampoline_name) { - const ModuleList &images = target_sp->GetImages(); - - SymbolContextList code_symbols; - images.FindSymbolsWithNameAndType(trampoline_name, eSymbolTypeCode, - code_symbols); - size_t num_code_symbols = code_symbols.GetSize(); - - if (num_code_symbols > 0) { - for (uint32_t i = 0; i < num_code_symbols; i++) { - SymbolContext context; - AddressRange addr_range; - if (code_symbols.GetContextAtIndex(i, context)) { - context.GetAddressRange(eSymbolContextEverything, 0, false, - addr_range); - addresses.push_back(addr_range.GetBaseAddress()); - if (log) { - addr_t load_addr = - addr_range.GetBaseAddress().GetLoadAddress(target_sp.get()); - - log->Printf("Found a trampoline target symbol at 0x%" PRIx64 - ".", - load_addr); - } - } - } - } - - SymbolContextList reexported_symbols; - images.FindSymbolsWithNameAndType( - trampoline_name, eSymbolTypeReExported, reexported_symbols); - size_t num_reexported_symbols = reexported_symbols.GetSize(); - if (num_reexported_symbols > 0) { - for (uint32_t i = 0; i < num_reexported_symbols; i++) { - SymbolContext context; - if (reexported_symbols.GetContextAtIndex(i, context)) { - if (context.symbol) { - Symbol *actual_symbol = - context.symbol->ResolveReExportedSymbol(*target_sp.get()); - if (actual_symbol) { - const Address actual_symbol_addr = - actual_symbol->GetAddress(); - if (actual_symbol_addr.IsValid()) { - addresses.push_back(actual_symbol_addr); - if (log) { - lldb::addr_t load_addr = - actual_symbol_addr.GetLoadAddress(target_sp.get()); - log->Printf( - "Found a re-exported symbol: %s at 0x%" PRIx64 ".", - actual_symbol->GetName().GetCString(), load_addr); - } - } - } - } - } - } - } - - SymbolContextList indirect_symbols; - images.FindSymbolsWithNameAndType(trampoline_name, eSymbolTypeResolver, - indirect_symbols); - size_t num_indirect_symbols = indirect_symbols.GetSize(); - if (num_indirect_symbols > 0) { - for (uint32_t i = 0; i < num_indirect_symbols; i++) { - SymbolContext context; - AddressRange addr_range; - if (indirect_symbols.GetContextAtIndex(i, context)) { - context.GetAddressRange(eSymbolContextEverything, 0, false, - addr_range); - addresses.push_back(addr_range.GetBaseAddress()); - if (log) { - addr_t load_addr = - addr_range.GetBaseAddress().GetLoadAddress(target_sp.get()); - - log->Printf("Found an indirect target symbol at 0x%" PRIx64 ".", - load_addr); - } - } - } - } - } - } else if (current_symbol->GetType() == eSymbolTypeReExported) { - // I am not sure we could ever end up stopped AT a re-exported symbol. - // But just in case: - - const Symbol *actual_symbol = - current_symbol->ResolveReExportedSymbol(*(target_sp.get())); - if (actual_symbol) { - Address target_addr(actual_symbol->GetAddress()); - if (target_addr.IsValid()) { - if (log) - log->Printf( - "Found a re-exported symbol: %s pointing to: %s at 0x%" PRIx64 - ".", - current_symbol->GetName().GetCString(), - actual_symbol->GetName().GetCString(), - target_addr.GetLoadAddress(target_sp.get())); - addresses.push_back(target_addr.GetLoadAddress(target_sp.get())); - } - } - } - - if (addresses.size() > 0) { - // First check whether any of the addresses point to Indirect symbols, - // and if they do, resolve them: - std::vector<lldb::addr_t> load_addrs; - for (Address address : addresses) { - Symbol *symbol = address.CalculateSymbolContextSymbol(); - if (symbol && symbol->IsIndirect()) { - Status error; - Address symbol_address = symbol->GetAddress(); - addr_t resolved_addr = thread.GetProcess()->ResolveIndirectFunction( - &symbol_address, error); - if (error.Success()) { - load_addrs.push_back(resolved_addr); - if (log) - log->Printf("ResolveIndirectFunction found resolved target for " - "%s at 0x%" PRIx64 ".", - symbol->GetName().GetCString(), resolved_addr); - } - } else { - load_addrs.push_back(address.GetLoadAddress(target_sp.get())); - } - } - thread_plan_sp.reset( - new ThreadPlanRunToAddress(thread, load_addrs, stop_others)); - } - } else { - if (log) - log->Printf("Could not find symbol for step through."); - } - - return thread_plan_sp; -} - -size_t DynamicLoaderDarwin::FindEquivalentSymbols( - lldb_private::Symbol *original_symbol, lldb_private::ModuleList &images, - lldb_private::SymbolContextList &equivalent_symbols) { - const ConstString &trampoline_name = original_symbol->GetMangled().GetName( - original_symbol->GetLanguage(), Mangled::ePreferMangled); - if (!trampoline_name) - return 0; - - size_t initial_size = equivalent_symbols.GetSize(); - - static const char *resolver_name_regex = "(_gc|_non_gc|\\$[A-Za-z0-9\\$]+)$"; - std::string equivalent_regex_buf("^"); - equivalent_regex_buf.append(trampoline_name.GetCString()); - equivalent_regex_buf.append(resolver_name_regex); - - RegularExpression equivalent_name_regex(equivalent_regex_buf); - const bool append = true; - images.FindSymbolsMatchingRegExAndType(equivalent_name_regex, eSymbolTypeCode, - equivalent_symbols, append); - - return equivalent_symbols.GetSize() - initial_size; -} - -lldb::ModuleSP DynamicLoaderDarwin::GetPThreadLibraryModule() { - ModuleSP module_sp = m_libpthread_module_wp.lock(); - if (!module_sp) { - SymbolContextList sc_list; - ModuleSpec module_spec; - module_spec.GetFileSpec().GetFilename().SetCString( - "libsystem_pthread.dylib"); - ModuleList module_list; - if (m_process->GetTarget().GetImages().FindModules(module_spec, - module_list)) { - if (module_list.GetSize() == 1) { - module_sp = module_list.GetModuleAtIndex(0); - if (module_sp) - m_libpthread_module_wp = module_sp; - } - } - } - return module_sp; -} - -Address DynamicLoaderDarwin::GetPthreadSetSpecificAddress() { - if (!m_pthread_getspecific_addr.IsValid()) { - ModuleSP module_sp = GetPThreadLibraryModule(); - if (module_sp) { - lldb_private::SymbolContextList sc_list; - module_sp->FindSymbolsWithNameAndType(ConstString("pthread_getspecific"), - eSymbolTypeCode, sc_list); - SymbolContext sc; - if (sc_list.GetContextAtIndex(0, sc)) { - if (sc.symbol) - m_pthread_getspecific_addr = sc.symbol->GetAddress(); - } - } - } - return m_pthread_getspecific_addr; -} - -lldb::addr_t -DynamicLoaderDarwin::GetThreadLocalData(const lldb::ModuleSP module_sp, - const lldb::ThreadSP thread_sp, - lldb::addr_t tls_file_addr) { - if (!thread_sp || !module_sp) - return LLDB_INVALID_ADDRESS; - - std::lock_guard<std::recursive_mutex> guard(m_mutex); - - const uint32_t addr_size = m_process->GetAddressByteSize(); - uint8_t buf[sizeof(lldb::addr_t) * 3]; - - lldb_private::Address tls_addr; - if (module_sp->ResolveFileAddress(tls_file_addr, tls_addr)) { - Status error; - const size_t tsl_data_size = addr_size * 3; - Target &target = m_process->GetTarget(); - if (target.ReadMemory(tls_addr, false, buf, tsl_data_size, error) == - tsl_data_size) { - const ByteOrder byte_order = m_process->GetByteOrder(); - DataExtractor data(buf, sizeof(buf), byte_order, addr_size); - lldb::offset_t offset = addr_size; // Skip the first pointer - const lldb::addr_t pthread_key = data.GetAddress(&offset); - const lldb::addr_t tls_offset = data.GetAddress(&offset); - if (pthread_key != 0) { - // First check to see if we have already figured out the location of - // TLS data for the pthread_key on a specific thread yet. If we have we - // can re-use it since its location will not change unless the process - // execs. - const tid_t tid = thread_sp->GetID(); - auto tid_pos = m_tid_to_tls_map.find(tid); - if (tid_pos != m_tid_to_tls_map.end()) { - auto tls_pos = tid_pos->second.find(pthread_key); - if (tls_pos != tid_pos->second.end()) { - return tls_pos->second + tls_offset; - } - } - StackFrameSP frame_sp = thread_sp->GetStackFrameAtIndex(0); - if (frame_sp) { - ClangASTContext *clang_ast_context = - target.GetScratchClangASTContext(); - - if (!clang_ast_context) - return LLDB_INVALID_ADDRESS; - - CompilerType clang_void_ptr_type = - clang_ast_context->GetBasicType(eBasicTypeVoid).GetPointerType(); - Address pthread_getspecific_addr = GetPthreadSetSpecificAddress(); - if (pthread_getspecific_addr.IsValid()) { - EvaluateExpressionOptions options; - - lldb::ThreadPlanSP thread_plan_sp(new ThreadPlanCallFunction( - *thread_sp, pthread_getspecific_addr, clang_void_ptr_type, - llvm::ArrayRef<lldb::addr_t>(pthread_key), options)); - - DiagnosticManager execution_errors; - ExecutionContext exe_ctx(thread_sp); - lldb::ExpressionResults results = m_process->RunThreadPlan( - exe_ctx, thread_plan_sp, options, execution_errors); - - if (results == lldb::eExpressionCompleted) { - lldb::ValueObjectSP result_valobj_sp = - thread_plan_sp->GetReturnValueObject(); - if (result_valobj_sp) { - const lldb::addr_t pthread_key_data = - result_valobj_sp->GetValueAsUnsigned(0); - if (pthread_key_data) { - m_tid_to_tls_map[tid].insert( - std::make_pair(pthread_key, pthread_key_data)); - return pthread_key_data + tls_offset; - } - } - } - } - } - } - } - } - return LLDB_INVALID_ADDRESS; -} - -bool DynamicLoaderDarwin::UseDYLDSPI(Process *process) { - Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER)); - bool use_new_spi_interface = false; - - llvm::VersionTuple version = process->GetHostOSVersion(); - if (!version.empty()) { - const llvm::Triple::OSType os_type = - process->GetTarget().GetArchitecture().GetTriple().getOS(); - - // macOS 10.12 and newer - if (os_type == llvm::Triple::MacOSX && - version >= llvm::VersionTuple(10, 12)) - use_new_spi_interface = true; - - // iOS 10 and newer - if (os_type == llvm::Triple::IOS && version >= llvm::VersionTuple(10)) - use_new_spi_interface = true; - - // tvOS 10 and newer - if (os_type == llvm::Triple::TvOS && version >= llvm::VersionTuple(10)) - use_new_spi_interface = true; - - // watchOS 3 and newer - if (os_type == llvm::Triple::WatchOS && version >= llvm::VersionTuple(3)) - use_new_spi_interface = true; - - // NEED_BRIDGEOS_TRIPLE // Any BridgeOS - // NEED_BRIDGEOS_TRIPLE if (os_type == llvm::Triple::BridgeOS) - // NEED_BRIDGEOS_TRIPLE use_new_spi_interface = true; - } - - if (log) { - if (use_new_spi_interface) - log->Printf( - "DynamicLoaderDarwin::UseDYLDSPI: Use new DynamicLoader plugin"); - else - log->Printf( - "DynamicLoaderDarwin::UseDYLDSPI: Use old DynamicLoader plugin"); - } - return use_new_spi_interface; -} diff --git a/source/Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderDarwin.h b/source/Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderDarwin.h deleted file mode 100644 index 690253ba5ff2..000000000000 --- a/source/Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderDarwin.h +++ /dev/null @@ -1,241 +0,0 @@ -//===-- DynamicLoaderDarwin.h -------------------------------*- C++ -*-===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// - -#ifndef liblldb_DynamicLoaderDarwin_h_ -#define liblldb_DynamicLoaderDarwin_h_ - -#include <map> -#include <mutex> -#include <vector> - -#include "lldb/Host/SafeMachO.h" -#include "lldb/Target/DynamicLoader.h" -#include "lldb/Target/Process.h" -#include "lldb/Utility/FileSpec.h" -#include "lldb/Utility/StructuredData.h" -#include "lldb/Utility/UUID.h" - -#include "llvm/ADT/Triple.h" - -namespace lldb_private { - -class DynamicLoaderDarwin : public lldb_private::DynamicLoader { -public: - DynamicLoaderDarwin(lldb_private::Process *process); - - virtual ~DynamicLoaderDarwin() override; - - //------------------------------------------------------------------ - /// Called after attaching a process. - /// - /// Allow DynamicLoader plug-ins to execute some code after - /// attaching to a process. - //------------------------------------------------------------------ - void DidAttach() override; - - void DidLaunch() override; - - lldb::ThreadPlanSP GetStepThroughTrampolinePlan(lldb_private::Thread &thread, - bool stop_others) override; - - size_t FindEquivalentSymbols( - lldb_private::Symbol *original_symbol, - lldb_private::ModuleList &module_list, - lldb_private::SymbolContextList &equivalent_symbols) override; - - lldb::addr_t GetThreadLocalData(const lldb::ModuleSP module, - const lldb::ThreadSP thread, - lldb::addr_t tls_file_addr) override; - - bool AlwaysRelyOnEHUnwindInfo(lldb_private::SymbolContext &sym_ctx) override; - - virtual void DoInitialImageFetch() = 0; - - virtual bool NeedToDoInitialImageFetch() = 0; - -protected: - void PrivateInitialize(lldb_private::Process *process); - - void PrivateProcessStateChanged(lldb_private::Process *process, - lldb::StateType state); - - void Clear(bool clear_process); - - // Clear method for classes derived from this one - virtual void DoClear() = 0; - - void SetDYLDModule(lldb::ModuleSP &dyld_module_sp); - - lldb::ModuleSP GetDYLDModule(); - - class Segment { - public: - Segment() - : name(), vmaddr(LLDB_INVALID_ADDRESS), vmsize(0), fileoff(0), - filesize(0), maxprot(0), initprot(0), nsects(0), flags(0) {} - - lldb_private::ConstString name; - lldb::addr_t vmaddr; - lldb::addr_t vmsize; - lldb::addr_t fileoff; - lldb::addr_t filesize; - uint32_t maxprot; - uint32_t initprot; - uint32_t nsects; - uint32_t flags; - - bool operator==(const Segment &rhs) const { - return name == rhs.name && vmaddr == rhs.vmaddr && vmsize == rhs.vmsize; - } - - void PutToLog(lldb_private::Log *log, lldb::addr_t slide) const; - }; - - struct ImageInfo { - lldb::addr_t address; // Address of mach header for this dylib - lldb::addr_t slide; // The amount to slide all segments by if there is a - // global slide. - lldb::addr_t mod_date; // Modification date for this dylib - lldb_private::FileSpec file_spec; // Resolved path for this dylib - lldb_private::UUID - uuid; // UUID for this dylib if it has one, else all zeros - llvm::MachO::mach_header header; // The mach header for this image - std::vector<Segment> segments; // All segment vmaddr and vmsize pairs for - // this executable (from memory of inferior) - uint32_t load_stop_id; // The process stop ID that the sections for this - // image were loaded - llvm::Triple::OSType os_type; // LC_VERSION_MIN_... load command os type - std::string min_version_os_sdk; // LC_VERSION_MIN_... sdk value - - ImageInfo() - : address(LLDB_INVALID_ADDRESS), slide(0), mod_date(0), file_spec(), - uuid(), header(), segments(), load_stop_id(0), - os_type(llvm::Triple::OSType::UnknownOS), min_version_os_sdk() {} - - void Clear(bool load_cmd_data_only) { - if (!load_cmd_data_only) { - address = LLDB_INVALID_ADDRESS; - slide = 0; - mod_date = 0; - file_spec.Clear(); - ::memset(&header, 0, sizeof(header)); - } - uuid.Clear(); - segments.clear(); - load_stop_id = 0; - os_type = llvm::Triple::OSType::UnknownOS; - min_version_os_sdk.clear(); - } - - bool operator==(const ImageInfo &rhs) const { - return address == rhs.address && slide == rhs.slide && - mod_date == rhs.mod_date && file_spec == rhs.file_spec && - uuid == rhs.uuid && - memcmp(&header, &rhs.header, sizeof(header)) == 0 && - segments == rhs.segments && os_type == rhs.os_type; - } - - bool UUIDValid() const { return uuid.IsValid(); } - - uint32_t GetAddressByteSize() { - if (header.cputype) { - if (header.cputype & llvm::MachO::CPU_ARCH_ABI64) - return 8; - else - return 4; - } - return 0; - } - - lldb_private::ArchSpec GetArchitecture() const { - return lldb_private::ArchSpec(lldb_private::eArchTypeMachO, - header.cputype, header.cpusubtype); - } - - const Segment *FindSegment(const lldb_private::ConstString &name) const; - - void PutToLog(lldb_private::Log *log) const; - - typedef std::vector<ImageInfo> collection; - typedef collection::iterator iterator; - typedef collection::const_iterator const_iterator; - }; - - bool UpdateImageLoadAddress(lldb_private::Module *module, ImageInfo &info); - - bool UnloadModuleSections(lldb_private::Module *module, ImageInfo &info); - - lldb::ModuleSP FindTargetModuleForImageInfo(ImageInfo &image_info, - bool can_create, - bool *did_create_ptr); - - void UnloadImages(const std::vector<lldb::addr_t> &solib_addresses); - - void UnloadAllImages(); - - virtual bool SetNotificationBreakpoint() = 0; - - virtual void ClearNotificationBreakpoint() = 0; - - virtual bool DidSetNotificationBreakpoint() = 0; - - typedef std::map<uint64_t, lldb::addr_t> PthreadKeyToTLSMap; - typedef std::map<lldb::user_id_t, PthreadKeyToTLSMap> ThreadIDToTLSMap; - - std::recursive_mutex &GetMutex() const { return m_mutex; } - - lldb::ModuleSP GetPThreadLibraryModule(); - - lldb_private::Address GetPthreadSetSpecificAddress(); - - bool JSONImageInformationIntoImageInfo( - lldb_private::StructuredData::ObjectSP image_details, - ImageInfo::collection &image_infos); - - // If image_infos contains / may contain dyld or executable image, call this - // method - // to keep our internal record keeping of the special binaries up-to-date. - void - UpdateSpecialBinariesFromNewImageInfos(ImageInfo::collection &image_infos); - - // if image_info is a dyld binary, call this method - void UpdateDYLDImageInfoFromNewImageInfo(ImageInfo &image_info); - - // If image_infos contains / may contain executable image, call this method - // to keep our internal record keeping of the special dyld binary up-to-date. - void AddExecutableModuleIfInImageInfos(ImageInfo::collection &image_infos); - - bool AddModulesUsingImageInfos(ImageInfo::collection &image_infos); - - // Whether we should use the new dyld SPI to get shared library information, - // or read - // it directly out of the dyld_all_image_infos. Whether we use the (newer) - // DynamicLoaderMacOS - // plugin or the (older) DynamicLoaderMacOSX plugin. - static bool UseDYLDSPI(lldb_private::Process *process); - - lldb::ModuleWP m_dyld_module_wp; // the dyld whose file type (mac, ios, etc) - // matches the process - lldb::ModuleWP m_libpthread_module_wp; - lldb_private::Address m_pthread_getspecific_addr; - ThreadIDToTLSMap m_tid_to_tls_map; - ImageInfo::collection - m_dyld_image_infos; // Current shared libraries information - uint32_t m_dyld_image_infos_stop_id; // The process stop ID that - // "m_dyld_image_infos" is valid for - ImageInfo m_dyld; - mutable std::recursive_mutex m_mutex; - -private: - DISALLOW_COPY_AND_ASSIGN(DynamicLoaderDarwin); -}; - -} // namespace lldb_private - -#endif // liblldb_DynamicLoaderDarwin_h_ diff --git a/source/Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderMacOS.cpp b/source/Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderMacOS.cpp deleted file mode 100644 index 1ff0ec2c7937..000000000000 --- a/source/Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderMacOS.cpp +++ /dev/null @@ -1,548 +0,0 @@ -//===-- DynamicLoaderMacOS.cpp -----------------------------*- C++ -*-===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// - -#include "lldb/Breakpoint/StoppointCallbackContext.h" -#include "lldb/Core/Debugger.h" -#include "lldb/Core/Module.h" -#include "lldb/Core/PluginManager.h" -#include "lldb/Core/Section.h" -#include "lldb/Symbol/ClangASTContext.h" -#include "lldb/Symbol/ObjectFile.h" -#include "lldb/Symbol/SymbolVendor.h" -#include "lldb/Target/ABI.h" -#include "lldb/Target/StackFrame.h" -#include "lldb/Target/Target.h" -#include "lldb/Target/Thread.h" -#include "lldb/Utility/Log.h" -#include "lldb/Utility/State.h" - -#include "DynamicLoaderDarwin.h" -#include "DynamicLoaderMacOS.h" - -using namespace lldb; -using namespace lldb_private; - -//---------------------------------------------------------------------- -// Create an instance of this class. This function is filled into the plugin -// info class that gets handed out by the plugin factory and allows the lldb to -// instantiate an instance of this class. -//---------------------------------------------------------------------- -DynamicLoader *DynamicLoaderMacOS::CreateInstance(Process *process, - bool force) { - bool create = force; - if (!create) { - create = true; - Module *exe_module = process->GetTarget().GetExecutableModulePointer(); - if (exe_module) { - ObjectFile *object_file = exe_module->GetObjectFile(); - if (object_file) { - create = (object_file->GetStrata() == ObjectFile::eStrataUser); - } - } - - if (create) { - const llvm::Triple &triple_ref = - process->GetTarget().GetArchitecture().GetTriple(); - switch (triple_ref.getOS()) { - case llvm::Triple::Darwin: - case llvm::Triple::MacOSX: - case llvm::Triple::IOS: - case llvm::Triple::TvOS: - case llvm::Triple::WatchOS: - // NEED_BRIDGEOS_TRIPLE case llvm::Triple::BridgeOS: - create = triple_ref.getVendor() == llvm::Triple::Apple; - break; - default: - create = false; - break; - } - } - } - - if (!UseDYLDSPI(process)) { - create = false; - } - - if (create) - return new DynamicLoaderMacOS(process); - return NULL; -} - -//---------------------------------------------------------------------- -// Constructor -//---------------------------------------------------------------------- -DynamicLoaderMacOS::DynamicLoaderMacOS(Process *process) - : DynamicLoaderDarwin(process), m_image_infos_stop_id(UINT32_MAX), - m_break_id(LLDB_INVALID_BREAK_ID), m_mutex(), - m_maybe_image_infos_address(LLDB_INVALID_ADDRESS) {} - -//---------------------------------------------------------------------- -// Destructor -//---------------------------------------------------------------------- -DynamicLoaderMacOS::~DynamicLoaderMacOS() { - if (LLDB_BREAK_ID_IS_VALID(m_break_id)) - m_process->GetTarget().RemoveBreakpointByID(m_break_id); -} - -bool DynamicLoaderMacOS::ProcessDidExec() { - std::lock_guard<std::recursive_mutex> baseclass_guard(GetMutex()); - bool did_exec = false; - if (m_process) { - // If we are stopped after an exec, we will have only one thread... - if (m_process->GetThreadList().GetSize() == 1) { - // Maybe we still have an image infos address around? If so see - // if that has changed, and if so we have exec'ed. - if (m_maybe_image_infos_address != LLDB_INVALID_ADDRESS) { - lldb::addr_t image_infos_address = m_process->GetImageInfoAddress(); - if (image_infos_address != m_maybe_image_infos_address) { - // We don't really have to reset this here, since we are going to - // call DoInitialImageFetch right away to handle the exec. But in - // case anybody looks at it in the meantime, it can't hurt. - m_maybe_image_infos_address = image_infos_address; - did_exec = true; - } - } - - if (!did_exec) { - // See if we are stopped at '_dyld_start' - ThreadSP thread_sp(m_process->GetThreadList().GetThreadAtIndex(0)); - if (thread_sp) { - lldb::StackFrameSP frame_sp(thread_sp->GetStackFrameAtIndex(0)); - if (frame_sp) { - const Symbol *symbol = - frame_sp->GetSymbolContext(eSymbolContextSymbol).symbol; - if (symbol) { - if (symbol->GetName() == ConstString("_dyld_start")) - did_exec = true; - } - } - } - } - } - } - - if (did_exec) { - m_libpthread_module_wp.reset(); - m_pthread_getspecific_addr.Clear(); - } - return did_exec; -} - -//---------------------------------------------------------------------- -// Clear out the state of this class. -//---------------------------------------------------------------------- -void DynamicLoaderMacOS::DoClear() { - std::lock_guard<std::recursive_mutex> guard(m_mutex); - - if (LLDB_BREAK_ID_IS_VALID(m_break_id)) - m_process->GetTarget().RemoveBreakpointByID(m_break_id); - - m_break_id = LLDB_INVALID_BREAK_ID; -} - -//---------------------------------------------------------------------- -// Check if we have found DYLD yet -//---------------------------------------------------------------------- -bool DynamicLoaderMacOS::DidSetNotificationBreakpoint() { - return LLDB_BREAK_ID_IS_VALID(m_break_id); -} - -void DynamicLoaderMacOS::ClearNotificationBreakpoint() { - if (LLDB_BREAK_ID_IS_VALID(m_break_id)) { - m_process->GetTarget().RemoveBreakpointByID(m_break_id); - m_break_id = LLDB_INVALID_BREAK_ID; - } -} - -//---------------------------------------------------------------------- -// Try and figure out where dyld is by first asking the Process if it knows -// (which currently calls down in the lldb::Process to get the DYLD info -// (available on SnowLeopard only). If that fails, then check in the default -// addresses. -//---------------------------------------------------------------------- -void DynamicLoaderMacOS::DoInitialImageFetch() { - Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER)); - - // Remove any binaries we pre-loaded in the Target before - // launching/attaching. If the same binaries are present in the process, - // we'll get them from the shared module cache, we won't need to re-load them - // from disk. - UnloadAllImages(); - - StructuredData::ObjectSP all_image_info_json_sp( - m_process->GetLoadedDynamicLibrariesInfos()); - ImageInfo::collection image_infos; - if (all_image_info_json_sp.get() && - all_image_info_json_sp->GetAsDictionary() && - all_image_info_json_sp->GetAsDictionary()->HasKey("images") && - all_image_info_json_sp->GetAsDictionary() - ->GetValueForKey("images") - ->GetAsArray()) { - if (JSONImageInformationIntoImageInfo(all_image_info_json_sp, - image_infos)) { - if (log) - log->Printf("Initial module fetch: Adding %" PRId64 " modules.\n", - (uint64_t)image_infos.size()); - - UpdateSpecialBinariesFromNewImageInfos(image_infos); - AddModulesUsingImageInfos(image_infos); - } - } - - m_dyld_image_infos_stop_id = m_process->GetStopID(); - m_maybe_image_infos_address = m_process->GetImageInfoAddress(); -} - -bool DynamicLoaderMacOS::NeedToDoInitialImageFetch() { return true; } - -//---------------------------------------------------------------------- -// Static callback function that gets called when our DYLD notification -// breakpoint gets hit. We update all of our image infos and then let our super -// class DynamicLoader class decide if we should stop or not (based on global -// preference). -//---------------------------------------------------------------------- -bool DynamicLoaderMacOS::NotifyBreakpointHit(void *baton, - StoppointCallbackContext *context, - lldb::user_id_t break_id, - lldb::user_id_t break_loc_id) { - // Let the event know that the images have changed - // DYLD passes three arguments to the notification breakpoint. - // Arg1: enum dyld_notify_mode mode - 0 = adding, 1 = removing, 2 = remove - // all Arg2: unsigned long icount - Number of shared libraries - // added/removed Arg3: uint64_t mach_headers[] - Array of load addresses - // of binaries added/removed - - DynamicLoaderMacOS *dyld_instance = (DynamicLoaderMacOS *)baton; - - ExecutionContext exe_ctx(context->exe_ctx_ref); - Process *process = exe_ctx.GetProcessPtr(); - - // This is a sanity check just in case this dyld_instance is an old dyld - // plugin's breakpoint still lying around. - if (process != dyld_instance->m_process) - return false; - - if (dyld_instance->m_image_infos_stop_id != UINT32_MAX && - process->GetStopID() < dyld_instance->m_image_infos_stop_id) { - return false; - } - - const lldb::ABISP &abi = process->GetABI(); - if (abi) { - // Build up the value array to store the three arguments given above, then - // get the values from the ABI: - - ClangASTContext *clang_ast_context = - process->GetTarget().GetScratchClangASTContext(); - ValueList argument_values; - - Value mode_value; // enum dyld_notify_mode { dyld_notify_adding=0, - // dyld_notify_removing=1, dyld_notify_remove_all=2 }; - Value count_value; // unsigned long count - Value headers_value; // uint64_t machHeaders[] (aka void*) - - CompilerType clang_void_ptr_type = - clang_ast_context->GetBasicType(eBasicTypeVoid).GetPointerType(); - CompilerType clang_uint32_type = - clang_ast_context->GetBuiltinTypeForEncodingAndBitSize( - lldb::eEncodingUint, 32); - CompilerType clang_uint64_type = - clang_ast_context->GetBuiltinTypeForEncodingAndBitSize( - lldb::eEncodingUint, 32); - - mode_value.SetValueType(Value::eValueTypeScalar); - mode_value.SetCompilerType(clang_uint32_type); - - if (process->GetTarget().GetArchitecture().GetAddressByteSize() == 4) { - count_value.SetValueType(Value::eValueTypeScalar); - count_value.SetCompilerType(clang_uint32_type); - } else { - count_value.SetValueType(Value::eValueTypeScalar); - count_value.SetCompilerType(clang_uint64_type); - } - - headers_value.SetValueType(Value::eValueTypeScalar); - headers_value.SetCompilerType(clang_void_ptr_type); - - argument_values.PushValue(mode_value); - argument_values.PushValue(count_value); - argument_values.PushValue(headers_value); - - if (abi->GetArgumentValues(exe_ctx.GetThreadRef(), argument_values)) { - uint32_t dyld_mode = - argument_values.GetValueAtIndex(0)->GetScalar().UInt(-1); - if (dyld_mode != static_cast<uint32_t>(-1)) { - // Okay the mode was right, now get the number of elements, and the - // array of new elements... - uint32_t image_infos_count = - argument_values.GetValueAtIndex(1)->GetScalar().UInt(-1); - if (image_infos_count != static_cast<uint32_t>(-1)) { - addr_t header_array = - argument_values.GetValueAtIndex(2)->GetScalar().ULongLong(-1); - if (header_array != static_cast<uint64_t>(-1)) { - std::vector<addr_t> image_load_addresses; - for (uint64_t i = 0; i < image_infos_count; i++) { - Status error; - addr_t addr = process->ReadUnsignedIntegerFromMemory( - header_array + (8 * i), 8, LLDB_INVALID_ADDRESS, error); - if (addr != LLDB_INVALID_ADDRESS) { - image_load_addresses.push_back(addr); - } - } - if (dyld_mode == 0) { - // dyld_notify_adding - dyld_instance->AddBinaries(image_load_addresses); - } else if (dyld_mode == 1) { - // dyld_notify_removing - dyld_instance->UnloadImages(image_load_addresses); - } else if (dyld_mode == 2) { - // dyld_notify_remove_all - dyld_instance->UnloadAllImages(); - } - } - } - } - } - } else { - process->GetTarget().GetDebugger().GetAsyncErrorStream()->Printf( - "No ABI plugin located for triple %s -- shared libraries will not be " - "registered!\n", - process->GetTarget().GetArchitecture().GetTriple().getTriple().c_str()); - } - - // Return true to stop the target, false to just let the target run - return dyld_instance->GetStopWhenImagesChange(); -} - -void DynamicLoaderMacOS::AddBinaries( - const std::vector<lldb::addr_t> &load_addresses) { - Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER)); - ImageInfo::collection image_infos; - - if (log) - log->Printf("Adding %" PRId64 " modules.", (uint64_t)load_addresses.size()); - StructuredData::ObjectSP binaries_info_sp = - m_process->GetLoadedDynamicLibrariesInfos(load_addresses); - if (binaries_info_sp.get() && binaries_info_sp->GetAsDictionary() && - binaries_info_sp->GetAsDictionary()->HasKey("images") && - binaries_info_sp->GetAsDictionary() - ->GetValueForKey("images") - ->GetAsArray() && - binaries_info_sp->GetAsDictionary() - ->GetValueForKey("images") - ->GetAsArray() - ->GetSize() == load_addresses.size()) { - if (JSONImageInformationIntoImageInfo(binaries_info_sp, image_infos)) { - UpdateSpecialBinariesFromNewImageInfos(image_infos); - AddModulesUsingImageInfos(image_infos); - } - m_dyld_image_infos_stop_id = m_process->GetStopID(); - } -} - -// Dump the _dyld_all_image_infos members and all current image infos that we -// have parsed to the file handle provided. -//---------------------------------------------------------------------- -void DynamicLoaderMacOS::PutToLog(Log *log) const { - if (log == NULL) - return; -} - -bool DynamicLoaderMacOS::SetNotificationBreakpoint() { - if (m_break_id == LLDB_INVALID_BREAK_ID) { - ConstString g_symbol_name("_dyld_debugger_notification"); - const Symbol *symbol = nullptr; - ModuleSP dyld_sp(GetDYLDModule()); - if (dyld_sp) { - symbol = dyld_sp->FindFirstSymbolWithNameAndType(g_symbol_name, - eSymbolTypeCode); - } - if (symbol && - (symbol->ValueIsAddress() || symbol->GetAddressRef().IsValid())) { - addr_t symbol_address = - symbol->GetAddressRef().GetOpcodeLoadAddress(&m_process->GetTarget()); - if (symbol_address != LLDB_INVALID_ADDRESS) { - bool internal = true; - bool hardware = false; - Breakpoint *breakpoint = - m_process->GetTarget() - .CreateBreakpoint(symbol_address, internal, hardware) - .get(); - breakpoint->SetCallback(DynamicLoaderMacOS::NotifyBreakpointHit, this, - true); - breakpoint->SetBreakpointKind("shared-library-event"); - m_break_id = breakpoint->GetID(); - } - } - } - return m_break_id != LLDB_INVALID_BREAK_ID; -} - -addr_t -DynamicLoaderMacOS::GetDyldLockVariableAddressFromModule(Module *module) { - SymbolContext sc; - SymbolVendor *sym_vendor = module->GetSymbolVendor(); - Target &target = m_process->GetTarget(); - if (sym_vendor) { - Symtab *symtab = sym_vendor->GetSymtab(); - if (symtab) { - std::vector<uint32_t> match_indexes; - ConstString g_symbol_name("_dyld_global_lock_held"); - uint32_t num_matches = 0; - num_matches = - symtab->AppendSymbolIndexesWithName(g_symbol_name, match_indexes); - if (num_matches == 1) { - Symbol *symbol = symtab->SymbolAtIndex(match_indexes[0]); - if (symbol && - (symbol->ValueIsAddress() || symbol->GetAddressRef().IsValid())) { - return symbol->GetAddressRef().GetOpcodeLoadAddress(&target); - } - } - } - } - return LLDB_INVALID_ADDRESS; -} - -// Look for this symbol: -// -// int __attribute__((visibility("hidden"))) _dyld_global_lock_held = -// 0; -// -// in libdyld.dylib. -Status DynamicLoaderMacOS::CanLoadImage() { - Status error; - addr_t symbol_address = LLDB_INVALID_ADDRESS; - Target &target = m_process->GetTarget(); - const ModuleList &target_modules = target.GetImages(); - std::lock_guard<std::recursive_mutex> guard(target_modules.GetMutex()); - const size_t num_modules = target_modules.GetSize(); - ConstString g_libdyld_name("libdyld.dylib"); - - // Find any modules named "libdyld.dylib" and look for the symbol there first - for (size_t i = 0; i < num_modules; i++) { - Module *module_pointer = target_modules.GetModulePointerAtIndexUnlocked(i); - if (module_pointer) { - if (module_pointer->GetFileSpec().GetFilename() == g_libdyld_name) { - symbol_address = GetDyldLockVariableAddressFromModule(module_pointer); - if (symbol_address != LLDB_INVALID_ADDRESS) - break; - } - } - } - - // Search through all modules looking for the symbol in them - if (symbol_address == LLDB_INVALID_ADDRESS) { - for (size_t i = 0; i < num_modules; i++) { - Module *module_pointer = - target_modules.GetModulePointerAtIndexUnlocked(i); - if (module_pointer) { - addr_t symbol_address = - GetDyldLockVariableAddressFromModule(module_pointer); - if (symbol_address != LLDB_INVALID_ADDRESS) - break; - } - } - } - - // Default assumption is that it is OK to load images. Only say that we - // cannot load images if we find the symbol in libdyld and it indicates that - // we cannot. - - if (symbol_address != LLDB_INVALID_ADDRESS) { - { - int lock_held = - m_process->ReadUnsignedIntegerFromMemory(symbol_address, 4, 0, error); - if (lock_held != 0) { - error.SetErrorString("dyld lock held - unsafe to load images."); - } - } - } else { - // If we were unable to find _dyld_global_lock_held in any modules, or it - // is not loaded into memory yet, we may be at process startup (sitting at - // _dyld_start) - so we should not allow dlopen calls. But if we found more - // than one module then we are clearly past _dyld_start so in that case - // we'll default to "it's safe". - if (num_modules <= 1) - error.SetErrorString("could not find the dyld library or " - "the dyld lock symbol"); - } - return error; -} - -bool DynamicLoaderMacOS::GetSharedCacheInformation( - lldb::addr_t &base_address, UUID &uuid, LazyBool &using_shared_cache, - LazyBool &private_shared_cache) { - base_address = LLDB_INVALID_ADDRESS; - uuid.Clear(); - using_shared_cache = eLazyBoolCalculate; - private_shared_cache = eLazyBoolCalculate; - - if (m_process) { - StructuredData::ObjectSP info = m_process->GetSharedCacheInfo(); - StructuredData::Dictionary *info_dict = nullptr; - if (info.get() && info->GetAsDictionary()) { - info_dict = info->GetAsDictionary(); - } - - // {"shared_cache_base_address":140735683125248,"shared_cache_uuid - // ":"DDB8D70C- - // C9A2-3561-B2C8-BE48A4F33F96","no_shared_cache":false,"shared_cache_private_cache":false} - - if (info_dict && info_dict->HasKey("shared_cache_uuid") && - info_dict->HasKey("no_shared_cache") && - info_dict->HasKey("shared_cache_base_address")) { - base_address = info_dict->GetValueForKey("shared_cache_base_address") - ->GetIntegerValue(LLDB_INVALID_ADDRESS); - std::string uuid_str = - info_dict->GetValueForKey("shared_cache_uuid")->GetStringValue(); - if (!uuid_str.empty()) - uuid.SetFromStringRef(uuid_str); - if (!info_dict->GetValueForKey("no_shared_cache")->GetBooleanValue()) - using_shared_cache = eLazyBoolYes; - else - using_shared_cache = eLazyBoolNo; - if (info_dict->GetValueForKey("shared_cache_private_cache") - ->GetBooleanValue()) - private_shared_cache = eLazyBoolYes; - else - private_shared_cache = eLazyBoolNo; - - return true; - } - } - return false; -} - -void DynamicLoaderMacOS::Initialize() { - PluginManager::RegisterPlugin(GetPluginNameStatic(), - GetPluginDescriptionStatic(), CreateInstance); -} - -void DynamicLoaderMacOS::Terminate() { - PluginManager::UnregisterPlugin(CreateInstance); -} - -lldb_private::ConstString DynamicLoaderMacOS::GetPluginNameStatic() { - static ConstString g_name("macos-dyld"); - return g_name; -} - -const char *DynamicLoaderMacOS::GetPluginDescriptionStatic() { - return "Dynamic loader plug-in that watches for shared library loads/unloads " - "in MacOSX user processes."; -} - -//------------------------------------------------------------------ -// PluginInterface protocol -//------------------------------------------------------------------ -lldb_private::ConstString DynamicLoaderMacOS::GetPluginName() { - return GetPluginNameStatic(); -} - -uint32_t DynamicLoaderMacOS::GetPluginVersion() { return 1; } diff --git a/source/Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderMacOS.h b/source/Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderMacOS.h deleted file mode 100644 index 6303c066511c..000000000000 --- a/source/Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderMacOS.h +++ /dev/null @@ -1,118 +0,0 @@ -//===-- DynamicLoaderMacOS.h -------------------------------*- C++ -*-===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// - -// This is the DynamicLoader plugin for Darwin (macOS / iPhoneOS / tvOS / -// watchOS / BridgeOS) -// platforms late 2016 and newer, where lldb will call dyld SPI functions to get -// information about shared libraries, information about the shared cache, and -// the _dyld_debugger_notification function we put a breakpoint on give us an -// array of load addresses for solibs loaded and unloaded. The SPI will tell us -// about both dyld and the executable, in addition to all of the usual solibs. - -#ifndef liblldb_DynamicLoaderMacOS_h_ -#define liblldb_DynamicLoaderMacOS_h_ - -#include <mutex> -#include <vector> - -#include "lldb/Target/DynamicLoader.h" -#include "lldb/Target/Process.h" -#include "lldb/Utility/FileSpec.h" -#include "lldb/Utility/StructuredData.h" -#include "lldb/Utility/UUID.h" - -#include "DynamicLoaderDarwin.h" - -class DynamicLoaderMacOS : public lldb_private::DynamicLoaderDarwin { -public: - DynamicLoaderMacOS(lldb_private::Process *process); - - virtual ~DynamicLoaderMacOS() override; - - //------------------------------------------------------------------ - // Static Functions - //------------------------------------------------------------------ - static void Initialize(); - - static void Terminate(); - - static lldb_private::ConstString GetPluginNameStatic(); - - static const char *GetPluginDescriptionStatic(); - - static lldb_private::DynamicLoader * - CreateInstance(lldb_private::Process *process, bool force); - - //------------------------------------------------------------------ - /// Called after attaching a process. - /// - /// Allow DynamicLoader plug-ins to execute some code after - /// attaching to a process. - //------------------------------------------------------------------ - bool ProcessDidExec() override; - - lldb_private::Status CanLoadImage() override; - - bool GetSharedCacheInformation( - lldb::addr_t &base_address, lldb_private::UUID &uuid, - lldb_private::LazyBool &using_shared_cache, - lldb_private::LazyBool &private_shared_cache) override; - - //------------------------------------------------------------------ - // PluginInterface protocol - //------------------------------------------------------------------ - lldb_private::ConstString GetPluginName() override; - - uint32_t GetPluginVersion() override; - -protected: - void PutToLog(lldb_private::Log *log) const; - - void DoInitialImageFetch() override; - - bool NeedToDoInitialImageFetch() override; - - bool DidSetNotificationBreakpoint() override; - - void AddBinaries(const std::vector<lldb::addr_t> &load_addresses); - - void DoClear() override; - - static bool - NotifyBreakpointHit(void *baton, - lldb_private::StoppointCallbackContext *context, - lldb::user_id_t break_id, lldb::user_id_t break_loc_id); - - bool SetNotificationBreakpoint() override; - - void ClearNotificationBreakpoint() override; - - void UpdateImageInfosHeaderAndLoadCommands(ImageInfo::collection &image_infos, - uint32_t infos_count, - bool update_executable); - - lldb::addr_t - GetDyldLockVariableAddressFromModule(lldb_private::Module *module); - - uint32_t m_image_infos_stop_id; // The Stop ID the last time we - // loaded/unloaded images - lldb::user_id_t m_break_id; - mutable std::recursive_mutex m_mutex; - lldb::addr_t m_maybe_image_infos_address; // If dyld is still maintaining the - // all_image_infos address, store it - // here so we can use it to detect - // exec's when talking to - // debugservers that don't support - // the "reason:exec" annotation. - -private: - DISALLOW_COPY_AND_ASSIGN(DynamicLoaderMacOS); -}; - -#endif // liblldb_DynamicLoaderMacOS_h_ diff --git a/source/Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderMacOSXDYLD.cpp b/source/Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderMacOSXDYLD.cpp deleted file mode 100644 index ec459a783f94..000000000000 --- a/source/Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderMacOSXDYLD.cpp +++ /dev/null @@ -1,1207 +0,0 @@ -//===-- DynamicLoaderMacOSXDYLD.cpp -----------------------------*- C++ -*-===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// - -#include "lldb/Breakpoint/StoppointCallbackContext.h" -#include "lldb/Core/Debugger.h" -#include "lldb/Core/Module.h" -#include "lldb/Core/ModuleSpec.h" -#include "lldb/Core/PluginManager.h" -#include "lldb/Core/Section.h" -#include "lldb/Symbol/ClangASTContext.h" -#include "lldb/Symbol/Function.h" -#include "lldb/Symbol/ObjectFile.h" -#include "lldb/Target/ABI.h" -#include "lldb/Target/ObjCLanguageRuntime.h" -#include "lldb/Target/RegisterContext.h" -#include "lldb/Target/StackFrame.h" -#include "lldb/Target/Target.h" -#include "lldb/Target/Thread.h" -#include "lldb/Target/ThreadPlanRunToAddress.h" -#include "lldb/Utility/DataBuffer.h" -#include "lldb/Utility/DataBufferHeap.h" -#include "lldb/Utility/Log.h" -#include "lldb/Utility/State.h" - -#include "DynamicLoaderDarwin.h" -#include "DynamicLoaderMacOSXDYLD.h" - -//#define ENABLE_DEBUG_PRINTF // COMMENT THIS LINE OUT PRIOR TO CHECKIN -#ifdef ENABLE_DEBUG_PRINTF -#include <stdio.h> -#define DEBUG_PRINTF(fmt, ...) printf(fmt, ##__VA_ARGS__) -#else -#define DEBUG_PRINTF(fmt, ...) -#endif - -#ifndef __APPLE__ -#include "Utility/UuidCompatibility.h" -#else -#include <uuid/uuid.h> -#endif - -using namespace lldb; -using namespace lldb_private; - -//---------------------------------------------------------------------- -// Create an instance of this class. This function is filled into the plugin -// info class that gets handed out by the plugin factory and allows the lldb to -// instantiate an instance of this class. -//---------------------------------------------------------------------- -DynamicLoader *DynamicLoaderMacOSXDYLD::CreateInstance(Process *process, - bool force) { - bool create = force; - if (!create) { - create = true; - Module *exe_module = process->GetTarget().GetExecutableModulePointer(); - if (exe_module) { - ObjectFile *object_file = exe_module->GetObjectFile(); - if (object_file) { - create = (object_file->GetStrata() == ObjectFile::eStrataUser); - } - } - - if (create) { - const llvm::Triple &triple_ref = - process->GetTarget().GetArchitecture().GetTriple(); - switch (triple_ref.getOS()) { - case llvm::Triple::Darwin: - case llvm::Triple::MacOSX: - case llvm::Triple::IOS: - case llvm::Triple::TvOS: - case llvm::Triple::WatchOS: - // NEED_BRIDGEOS_TRIPLE case llvm::Triple::BridgeOS: - create = triple_ref.getVendor() == llvm::Triple::Apple; - break; - default: - create = false; - break; - } - } - } - - if (UseDYLDSPI(process)) { - create = false; - } - - if (create) - return new DynamicLoaderMacOSXDYLD(process); - return NULL; -} - -//---------------------------------------------------------------------- -// Constructor -//---------------------------------------------------------------------- -DynamicLoaderMacOSXDYLD::DynamicLoaderMacOSXDYLD(Process *process) - : DynamicLoaderDarwin(process), - m_dyld_all_image_infos_addr(LLDB_INVALID_ADDRESS), - m_dyld_all_image_infos(), m_dyld_all_image_infos_stop_id(UINT32_MAX), - m_break_id(LLDB_INVALID_BREAK_ID), m_mutex(), - m_process_image_addr_is_all_images_infos(false) {} - -//---------------------------------------------------------------------- -// Destructor -//---------------------------------------------------------------------- -DynamicLoaderMacOSXDYLD::~DynamicLoaderMacOSXDYLD() { - if (LLDB_BREAK_ID_IS_VALID(m_break_id)) - m_process->GetTarget().RemoveBreakpointByID(m_break_id); -} - -bool DynamicLoaderMacOSXDYLD::ProcessDidExec() { - std::lock_guard<std::recursive_mutex> baseclass_guard(GetMutex()); - bool did_exec = false; - if (m_process) { - // If we are stopped after an exec, we will have only one thread... - if (m_process->GetThreadList().GetSize() == 1) { - // We know if a process has exec'ed if our "m_dyld_all_image_infos_addr" - // value differs from the Process' image info address. When a process - // execs itself it might cause a change if ASLR is enabled. - const addr_t shlib_addr = m_process->GetImageInfoAddress(); - if (m_process_image_addr_is_all_images_infos && - shlib_addr != m_dyld_all_image_infos_addr) { - // The image info address from the process is the - // 'dyld_all_image_infos' address and it has changed. - did_exec = true; - } else if (!m_process_image_addr_is_all_images_infos && - shlib_addr == m_dyld.address) { - // The image info address from the process is the mach_header address - // for dyld and it has changed. - did_exec = true; - } else { - // ASLR might be disabled and dyld could have ended up in the same - // location. We should try and detect if we are stopped at - // '_dyld_start' - ThreadSP thread_sp(m_process->GetThreadList().GetThreadAtIndex(0)); - if (thread_sp) { - lldb::StackFrameSP frame_sp(thread_sp->GetStackFrameAtIndex(0)); - if (frame_sp) { - const Symbol *symbol = - frame_sp->GetSymbolContext(eSymbolContextSymbol).symbol; - if (symbol) { - if (symbol->GetName() == ConstString("_dyld_start")) - did_exec = true; - } - } - } - } - - if (did_exec) { - m_libpthread_module_wp.reset(); - m_pthread_getspecific_addr.Clear(); - } - } - } - return did_exec; -} - -//---------------------------------------------------------------------- -// Clear out the state of this class. -//---------------------------------------------------------------------- -void DynamicLoaderMacOSXDYLD::DoClear() { - std::lock_guard<std::recursive_mutex> guard(m_mutex); - - if (LLDB_BREAK_ID_IS_VALID(m_break_id)) - m_process->GetTarget().RemoveBreakpointByID(m_break_id); - - m_dyld_all_image_infos_addr = LLDB_INVALID_ADDRESS; - m_dyld_all_image_infos.Clear(); - m_break_id = LLDB_INVALID_BREAK_ID; -} - -//---------------------------------------------------------------------- -// Check if we have found DYLD yet -//---------------------------------------------------------------------- -bool DynamicLoaderMacOSXDYLD::DidSetNotificationBreakpoint() { - return LLDB_BREAK_ID_IS_VALID(m_break_id); -} - -void DynamicLoaderMacOSXDYLD::ClearNotificationBreakpoint() { - if (LLDB_BREAK_ID_IS_VALID(m_break_id)) { - m_process->GetTarget().RemoveBreakpointByID(m_break_id); - } -} - -//---------------------------------------------------------------------- -// Try and figure out where dyld is by first asking the Process if it knows -// (which currently calls down in the lldb::Process to get the DYLD info -// (available on SnowLeopard only). If that fails, then check in the default -// addresses. -//---------------------------------------------------------------------- -void DynamicLoaderMacOSXDYLD::DoInitialImageFetch() { - if (m_dyld_all_image_infos_addr == LLDB_INVALID_ADDRESS) { - // Check the image info addr as it might point to the mach header for dyld, - // or it might point to the dyld_all_image_infos struct - const addr_t shlib_addr = m_process->GetImageInfoAddress(); - if (shlib_addr != LLDB_INVALID_ADDRESS) { - ByteOrder byte_order = - m_process->GetTarget().GetArchitecture().GetByteOrder(); - uint8_t buf[4]; - DataExtractor data(buf, sizeof(buf), byte_order, 4); - Status error; - if (m_process->ReadMemory(shlib_addr, buf, 4, error) == 4) { - lldb::offset_t offset = 0; - uint32_t magic = data.GetU32(&offset); - switch (magic) { - case llvm::MachO::MH_MAGIC: - case llvm::MachO::MH_MAGIC_64: - case llvm::MachO::MH_CIGAM: - case llvm::MachO::MH_CIGAM_64: - m_process_image_addr_is_all_images_infos = false; - ReadDYLDInfoFromMemoryAndSetNotificationCallback(shlib_addr); - return; - - default: - break; - } - } - // Maybe it points to the all image infos? - m_dyld_all_image_infos_addr = shlib_addr; - m_process_image_addr_is_all_images_infos = true; - } - } - - if (m_dyld_all_image_infos_addr != LLDB_INVALID_ADDRESS) { - if (ReadAllImageInfosStructure()) { - if (m_dyld_all_image_infos.dyldImageLoadAddress != LLDB_INVALID_ADDRESS) - ReadDYLDInfoFromMemoryAndSetNotificationCallback( - m_dyld_all_image_infos.dyldImageLoadAddress); - else - ReadDYLDInfoFromMemoryAndSetNotificationCallback( - m_dyld_all_image_infos_addr & 0xfffffffffff00000ull); - return; - } - } - - // Check some default values - Module *executable = m_process->GetTarget().GetExecutableModulePointer(); - - if (executable) { - const ArchSpec &exe_arch = executable->GetArchitecture(); - if (exe_arch.GetAddressByteSize() == 8) { - ReadDYLDInfoFromMemoryAndSetNotificationCallback(0x7fff5fc00000ull); - } else if (exe_arch.GetMachine() == llvm::Triple::arm || - exe_arch.GetMachine() == llvm::Triple::thumb || - exe_arch.GetMachine() == llvm::Triple::aarch64) { - ReadDYLDInfoFromMemoryAndSetNotificationCallback(0x2fe00000); - } else { - ReadDYLDInfoFromMemoryAndSetNotificationCallback(0x8fe00000); - } - } - return; -} - -//---------------------------------------------------------------------- -// Assume that dyld is in memory at ADDR and try to parse it's load commands -//---------------------------------------------------------------------- -bool DynamicLoaderMacOSXDYLD::ReadDYLDInfoFromMemoryAndSetNotificationCallback( - lldb::addr_t addr) { - std::lock_guard<std::recursive_mutex> baseclass_guard(GetMutex()); - DataExtractor data; // Load command data - static ConstString g_dyld_all_image_infos("dyld_all_image_infos"); - if (ReadMachHeader(addr, &m_dyld.header, &data)) { - if (m_dyld.header.filetype == llvm::MachO::MH_DYLINKER) { - m_dyld.address = addr; - ModuleSP dyld_module_sp; - if (ParseLoadCommands(data, m_dyld, &m_dyld.file_spec)) { - if (m_dyld.file_spec) { - UpdateDYLDImageInfoFromNewImageInfo(m_dyld); - } - } - dyld_module_sp = GetDYLDModule(); - - Target &target = m_process->GetTarget(); - - if (m_dyld_all_image_infos_addr == LLDB_INVALID_ADDRESS && - dyld_module_sp.get()) { - const Symbol *symbol = dyld_module_sp->FindFirstSymbolWithNameAndType( - g_dyld_all_image_infos, eSymbolTypeData); - if (symbol) - m_dyld_all_image_infos_addr = symbol->GetLoadAddress(&target); - } - - // Update all image infos - InitializeFromAllImageInfos(); - - // If we didn't have an executable before, but now we do, then the dyld - // module shared pointer might be unique and we may need to add it again - // (since Target::SetExecutableModule() will clear the images). So append - // the dyld module back to the list if it is - /// unique! - if (dyld_module_sp) { - target.GetImages().AppendIfNeeded(dyld_module_sp); - - // At this point we should have read in dyld's module, and so we should - // set breakpoints in it: - ModuleList modules; - modules.Append(dyld_module_sp); - target.ModulesDidLoad(modules); - SetDYLDModule(dyld_module_sp); - } - - return true; - } - } - return false; -} - -bool DynamicLoaderMacOSXDYLD::NeedToDoInitialImageFetch() { - return m_dyld_all_image_infos_addr == LLDB_INVALID_ADDRESS; -} - -//---------------------------------------------------------------------- -// Static callback function that gets called when our DYLD notification -// breakpoint gets hit. We update all of our image infos and then let our super -// class DynamicLoader class decide if we should stop or not (based on global -// preference). -//---------------------------------------------------------------------- -bool DynamicLoaderMacOSXDYLD::NotifyBreakpointHit( - void *baton, StoppointCallbackContext *context, lldb::user_id_t break_id, - lldb::user_id_t break_loc_id) { - // Let the event know that the images have changed - // DYLD passes three arguments to the notification breakpoint. - // Arg1: enum dyld_image_mode mode - 0 = adding, 1 = removing Arg2: uint32_t - // infoCount - Number of shared libraries added Arg3: dyld_image_info - // info[] - Array of structs of the form: - // const struct mach_header - // *imageLoadAddress - // const char *imageFilePath - // uintptr_t imageFileModDate (a time_t) - - DynamicLoaderMacOSXDYLD *dyld_instance = (DynamicLoaderMacOSXDYLD *)baton; - - // First step is to see if we've already initialized the all image infos. If - // we haven't then this function will do so and return true. In the course - // of initializing the all_image_infos it will read the complete current - // state, so we don't need to figure out what has changed from the data - // passed in to us. - - ExecutionContext exe_ctx(context->exe_ctx_ref); - Process *process = exe_ctx.GetProcessPtr(); - - // This is a sanity check just in case this dyld_instance is an old dyld - // plugin's breakpoint still lying around. - if (process != dyld_instance->m_process) - return false; - - if (dyld_instance->InitializeFromAllImageInfos()) - return dyld_instance->GetStopWhenImagesChange(); - - const lldb::ABISP &abi = process->GetABI(); - if (abi) { - // Build up the value array to store the three arguments given above, then - // get the values from the ABI: - - ClangASTContext *clang_ast_context = - process->GetTarget().GetScratchClangASTContext(); - ValueList argument_values; - Value input_value; - - CompilerType clang_void_ptr_type = - clang_ast_context->GetBasicType(eBasicTypeVoid).GetPointerType(); - CompilerType clang_uint32_type = - clang_ast_context->GetBuiltinTypeForEncodingAndBitSize( - lldb::eEncodingUint, 32); - input_value.SetValueType(Value::eValueTypeScalar); - input_value.SetCompilerType(clang_uint32_type); - // input_value.SetContext (Value::eContextTypeClangType, - // clang_uint32_type); - argument_values.PushValue(input_value); - argument_values.PushValue(input_value); - input_value.SetCompilerType(clang_void_ptr_type); - // input_value.SetContext (Value::eContextTypeClangType, - // clang_void_ptr_type); - argument_values.PushValue(input_value); - - if (abi->GetArgumentValues(exe_ctx.GetThreadRef(), argument_values)) { - uint32_t dyld_mode = - argument_values.GetValueAtIndex(0)->GetScalar().UInt(-1); - if (dyld_mode != static_cast<uint32_t>(-1)) { - // Okay the mode was right, now get the number of elements, and the - // array of new elements... - uint32_t image_infos_count = - argument_values.GetValueAtIndex(1)->GetScalar().UInt(-1); - if (image_infos_count != static_cast<uint32_t>(-1)) { - // Got the number added, now go through the array of added elements, - // putting out the mach header address, and adding the image. Note, - // I'm not putting in logging here, since the AddModules & - // RemoveModules functions do all the logging internally. - - lldb::addr_t image_infos_addr = - argument_values.GetValueAtIndex(2)->GetScalar().ULongLong(); - if (dyld_mode == 0) { - // This is add: - dyld_instance->AddModulesUsingImageInfosAddress(image_infos_addr, - image_infos_count); - } else { - // This is remove: - dyld_instance->RemoveModulesUsingImageInfosAddress( - image_infos_addr, image_infos_count); - } - } - } - } - } else { - process->GetTarget().GetDebugger().GetAsyncErrorStream()->Printf( - "No ABI plugin located for triple %s -- shared libraries will not be " - "registered!\n", - process->GetTarget().GetArchitecture().GetTriple().getTriple().c_str()); - } - - // Return true to stop the target, false to just let the target run - return dyld_instance->GetStopWhenImagesChange(); -} - -bool DynamicLoaderMacOSXDYLD::ReadAllImageInfosStructure() { - std::lock_guard<std::recursive_mutex> guard(m_mutex); - - // the all image infos is already valid for this process stop ID - if (m_process->GetStopID() == m_dyld_all_image_infos_stop_id) - return true; - - m_dyld_all_image_infos.Clear(); - if (m_dyld_all_image_infos_addr != LLDB_INVALID_ADDRESS) { - ByteOrder byte_order = - m_process->GetTarget().GetArchitecture().GetByteOrder(); - uint32_t addr_size = - m_process->GetTarget().GetArchitecture().GetAddressByteSize(); - - uint8_t buf[256]; - DataExtractor data(buf, sizeof(buf), byte_order, addr_size); - lldb::offset_t offset = 0; - - const size_t count_v2 = sizeof(uint32_t) + // version - sizeof(uint32_t) + // infoArrayCount - addr_size + // infoArray - addr_size + // notification - addr_size + // processDetachedFromSharedRegion + - // libSystemInitialized + pad - addr_size; // dyldImageLoadAddress - const size_t count_v11 = count_v2 + addr_size + // jitInfo - addr_size + // dyldVersion - addr_size + // errorMessage - addr_size + // terminationFlags - addr_size + // coreSymbolicationShmPage - addr_size + // systemOrderFlag - addr_size + // uuidArrayCount - addr_size + // uuidArray - addr_size + // dyldAllImageInfosAddress - addr_size + // initialImageCount - addr_size + // errorKind - addr_size + // errorClientOfDylibPath - addr_size + // errorTargetDylibPath - addr_size; // errorSymbol - const size_t count_v13 = count_v11 + addr_size + // sharedCacheSlide - sizeof(uuid_t); // sharedCacheUUID - UNUSED_IF_ASSERT_DISABLED(count_v13); - assert(sizeof(buf) >= count_v13); - - Status error; - if (m_process->ReadMemory(m_dyld_all_image_infos_addr, buf, 4, error) == - 4) { - m_dyld_all_image_infos.version = data.GetU32(&offset); - // If anything in the high byte is set, we probably got the byte order - // incorrect (the process might not have it set correctly yet due to - // attaching to a program without a specified file). - if (m_dyld_all_image_infos.version & 0xff000000) { - // We have guessed the wrong byte order. Swap it and try reading the - // version again. - if (byte_order == eByteOrderLittle) - byte_order = eByteOrderBig; - else - byte_order = eByteOrderLittle; - - data.SetByteOrder(byte_order); - offset = 0; - m_dyld_all_image_infos.version = data.GetU32(&offset); - } - } else { - return false; - } - - const size_t count = - (m_dyld_all_image_infos.version >= 11) ? count_v11 : count_v2; - - const size_t bytes_read = - m_process->ReadMemory(m_dyld_all_image_infos_addr, buf, count, error); - if (bytes_read == count) { - offset = 0; - m_dyld_all_image_infos.version = data.GetU32(&offset); - m_dyld_all_image_infos.dylib_info_count = data.GetU32(&offset); - m_dyld_all_image_infos.dylib_info_addr = data.GetPointer(&offset); - m_dyld_all_image_infos.notification = data.GetPointer(&offset); - m_dyld_all_image_infos.processDetachedFromSharedRegion = - data.GetU8(&offset); - m_dyld_all_image_infos.libSystemInitialized = data.GetU8(&offset); - // Adjust for padding. - offset += addr_size - 2; - m_dyld_all_image_infos.dyldImageLoadAddress = data.GetPointer(&offset); - if (m_dyld_all_image_infos.version >= 11) { - offset += addr_size * 8; - uint64_t dyld_all_image_infos_addr = data.GetPointer(&offset); - - // When we started, we were given the actual address of the - // all_image_infos struct (probably via TASK_DYLD_INFO) in memory - - // this address is stored in m_dyld_all_image_infos_addr and is the - // most accurate address we have. - - // We read the dyld_all_image_infos struct from memory; it contains its - // own address. If the address in the struct does not match the actual - // address, the dyld we're looking at has been loaded at a different - // location (slid) from where it intended to load. The addresses in - // the dyld_all_image_infos struct are the original, non-slid - // addresses, and need to be adjusted. Most importantly the address of - // dyld and the notification address need to be adjusted. - - if (dyld_all_image_infos_addr != m_dyld_all_image_infos_addr) { - uint64_t image_infos_offset = - dyld_all_image_infos_addr - - m_dyld_all_image_infos.dyldImageLoadAddress; - uint64_t notification_offset = - m_dyld_all_image_infos.notification - - m_dyld_all_image_infos.dyldImageLoadAddress; - m_dyld_all_image_infos.dyldImageLoadAddress = - m_dyld_all_image_infos_addr - image_infos_offset; - m_dyld_all_image_infos.notification = - m_dyld_all_image_infos.dyldImageLoadAddress + notification_offset; - } - } - m_dyld_all_image_infos_stop_id = m_process->GetStopID(); - return true; - } - } - return false; -} - -bool DynamicLoaderMacOSXDYLD::AddModulesUsingImageInfosAddress( - lldb::addr_t image_infos_addr, uint32_t image_infos_count) { - ImageInfo::collection image_infos; - Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER)); - if (log) - log->Printf("Adding %d modules.\n", image_infos_count); - - std::lock_guard<std::recursive_mutex> guard(m_mutex); - std::lock_guard<std::recursive_mutex> baseclass_guard(GetMutex()); - if (m_process->GetStopID() == m_dyld_image_infos_stop_id) - return true; - - StructuredData::ObjectSP image_infos_json_sp = - m_process->GetLoadedDynamicLibrariesInfos(image_infos_addr, - image_infos_count); - if (image_infos_json_sp.get() && image_infos_json_sp->GetAsDictionary() && - image_infos_json_sp->GetAsDictionary()->HasKey("images") && - image_infos_json_sp->GetAsDictionary() - ->GetValueForKey("images") - ->GetAsArray() && - image_infos_json_sp->GetAsDictionary() - ->GetValueForKey("images") - ->GetAsArray() - ->GetSize() == image_infos_count) { - bool return_value = false; - if (JSONImageInformationIntoImageInfo(image_infos_json_sp, image_infos)) { - UpdateSpecialBinariesFromNewImageInfos(image_infos); - return_value = AddModulesUsingImageInfos(image_infos); - } - m_dyld_image_infos_stop_id = m_process->GetStopID(); - return return_value; - } - - if (!ReadImageInfos(image_infos_addr, image_infos_count, image_infos)) - return false; - - UpdateImageInfosHeaderAndLoadCommands(image_infos, image_infos_count, false); - bool return_value = AddModulesUsingImageInfos(image_infos); - m_dyld_image_infos_stop_id = m_process->GetStopID(); - return return_value; -} - -bool DynamicLoaderMacOSXDYLD::RemoveModulesUsingImageInfosAddress( - lldb::addr_t image_infos_addr, uint32_t image_infos_count) { - ImageInfo::collection image_infos; - Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER)); - - std::lock_guard<std::recursive_mutex> guard(m_mutex); - std::lock_guard<std::recursive_mutex> baseclass_guard(GetMutex()); - if (m_process->GetStopID() == m_dyld_image_infos_stop_id) - return true; - - // First read in the image_infos for the removed modules, and their headers & - // load commands. - if (!ReadImageInfos(image_infos_addr, image_infos_count, image_infos)) { - if (log) - log->PutCString("Failed reading image infos array."); - return false; - } - - if (log) - log->Printf("Removing %d modules.", image_infos_count); - - ModuleList unloaded_module_list; - for (uint32_t idx = 0; idx < image_infos.size(); ++idx) { - if (log) { - log->Printf("Removing module at address=0x%16.16" PRIx64 ".", - image_infos[idx].address); - image_infos[idx].PutToLog(log); - } - - // Remove this image_infos from the m_all_image_infos. We do the - // comparison by address rather than by file spec because we can have many - // modules with the same "file spec" in the case that they are modules - // loaded from memory. - // - // Also copy over the uuid from the old entry to the removed entry so we - // can use it to lookup the module in the module list. - - ImageInfo::collection::iterator pos, end = m_dyld_image_infos.end(); - for (pos = m_dyld_image_infos.begin(); pos != end; pos++) { - if (image_infos[idx].address == (*pos).address) { - image_infos[idx].uuid = (*pos).uuid; - - // Add the module from this image_info to the "unloaded_module_list". - // We'll remove them all at one go later on. - - ModuleSP unload_image_module_sp( - FindTargetModuleForImageInfo(image_infos[idx], false, NULL)); - if (unload_image_module_sp.get()) { - // When we unload, be sure to use the image info from the old list, - // since that has sections correctly filled in. - UnloadModuleSections(unload_image_module_sp.get(), *pos); - unloaded_module_list.AppendIfNeeded(unload_image_module_sp); - } else { - if (log) { - log->Printf("Could not find module for unloading info entry:"); - image_infos[idx].PutToLog(log); - } - } - - // Then remove it from the m_dyld_image_infos: - - m_dyld_image_infos.erase(pos); - break; - } - } - - if (pos == end) { - if (log) { - log->Printf("Could not find image_info entry for unloading image:"); - image_infos[idx].PutToLog(log); - } - } - } - if (unloaded_module_list.GetSize() > 0) { - if (log) { - log->PutCString("Unloaded:"); - unloaded_module_list.LogUUIDAndPaths( - log, "DynamicLoaderMacOSXDYLD::ModulesDidUnload"); - } - m_process->GetTarget().GetImages().Remove(unloaded_module_list); - } - m_dyld_image_infos_stop_id = m_process->GetStopID(); - return true; -} - -bool DynamicLoaderMacOSXDYLD::ReadImageInfos( - lldb::addr_t image_infos_addr, uint32_t image_infos_count, - ImageInfo::collection &image_infos) { - std::lock_guard<std::recursive_mutex> baseclass_guard(GetMutex()); - const ByteOrder endian = GetByteOrderFromMagic(m_dyld.header.magic); - const uint32_t addr_size = m_dyld.GetAddressByteSize(); - - image_infos.resize(image_infos_count); - const size_t count = image_infos.size() * 3 * addr_size; - DataBufferHeap info_data(count, 0); - Status error; - const size_t bytes_read = m_process->ReadMemory( - image_infos_addr, info_data.GetBytes(), info_data.GetByteSize(), error); - if (bytes_read == count) { - lldb::offset_t info_data_offset = 0; - DataExtractor info_data_ref(info_data.GetBytes(), info_data.GetByteSize(), - endian, addr_size); - for (size_t i = 0; - i < image_infos.size() && info_data_ref.ValidOffset(info_data_offset); - i++) { - image_infos[i].address = info_data_ref.GetPointer(&info_data_offset); - lldb::addr_t path_addr = info_data_ref.GetPointer(&info_data_offset); - image_infos[i].mod_date = info_data_ref.GetPointer(&info_data_offset); - - char raw_path[PATH_MAX]; - m_process->ReadCStringFromMemory(path_addr, raw_path, sizeof(raw_path), - error); - // don't resolve the path - if (error.Success()) { - image_infos[i].file_spec.SetFile(raw_path, FileSpec::Style::native); - } - } - return true; - } else { - return false; - } -} - -//---------------------------------------------------------------------- -// If we have found where the "_dyld_all_image_infos" lives in memory, read the -// current info from it, and then update all image load addresses (or lack -// thereof). Only do this if this is the first time we're reading the dyld -// infos. Return true if we actually read anything, and false otherwise. -//---------------------------------------------------------------------- -bool DynamicLoaderMacOSXDYLD::InitializeFromAllImageInfos() { - Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_DYNAMIC_LOADER)); - - std::lock_guard<std::recursive_mutex> guard(m_mutex); - std::lock_guard<std::recursive_mutex> baseclass_guard(GetMutex()); - if (m_process->GetStopID() == m_dyld_image_infos_stop_id || - m_dyld_image_infos.size() != 0) - return false; - - if (ReadAllImageInfosStructure()) { - // Nothing to load or unload? - if (m_dyld_all_image_infos.dylib_info_count == 0) - return true; - - if (m_dyld_all_image_infos.dylib_info_addr == 0) { - // DYLD is updating the images now. So we should say we have no images, - // and then we'll - // figure it out when we hit the added breakpoint. - return false; - } else { - if (!AddModulesUsingImageInfosAddress( - m_dyld_all_image_infos.dylib_info_addr, - m_dyld_all_image_infos.dylib_info_count)) { - DEBUG_PRINTF("%s", "unable to read all data for all_dylib_infos."); - m_dyld_image_infos.clear(); - } - } - - // Now we have one more bit of business. If there is a library left in the - // images for our target that doesn't have a load address, then it must be - // something that we were expecting to load (for instance we read a load - // command for it) but it didn't in fact load - probably because - // DYLD_*_PATH pointed to an equivalent version. We don't want it to stay - // in the target's module list or it will confuse us, so unload it here. - Target &target = m_process->GetTarget(); - const ModuleList &target_modules = target.GetImages(); - ModuleList not_loaded_modules; - std::lock_guard<std::recursive_mutex> guard(target_modules.GetMutex()); - - size_t num_modules = target_modules.GetSize(); - for (size_t i = 0; i < num_modules; i++) { - ModuleSP module_sp = target_modules.GetModuleAtIndexUnlocked(i); - if (!module_sp->IsLoadedInTarget(&target)) { - if (log) { - StreamString s; - module_sp->GetDescription(&s); - log->Printf("Unloading pre-run module: %s.", s.GetData()); - } - not_loaded_modules.Append(module_sp); - } - } - - if (not_loaded_modules.GetSize() != 0) { - target.GetImages().Remove(not_loaded_modules); - } - - return true; - } else - return false; -} - -//---------------------------------------------------------------------- -// Read a mach_header at ADDR into HEADER, and also fill in the load command -// data into LOAD_COMMAND_DATA if it is non-NULL. -// -// Returns true if we succeed, false if we fail for any reason. -//---------------------------------------------------------------------- -bool DynamicLoaderMacOSXDYLD::ReadMachHeader(lldb::addr_t addr, - llvm::MachO::mach_header *header, - DataExtractor *load_command_data) { - DataBufferHeap header_bytes(sizeof(llvm::MachO::mach_header), 0); - Status error; - size_t bytes_read = m_process->ReadMemory(addr, header_bytes.GetBytes(), - header_bytes.GetByteSize(), error); - if (bytes_read == sizeof(llvm::MachO::mach_header)) { - lldb::offset_t offset = 0; - ::memset(header, 0, sizeof(llvm::MachO::mach_header)); - - // Get the magic byte unswapped so we can figure out what we are dealing - // with - DataExtractor data(header_bytes.GetBytes(), header_bytes.GetByteSize(), - endian::InlHostByteOrder(), 4); - header->magic = data.GetU32(&offset); - lldb::addr_t load_cmd_addr = addr; - data.SetByteOrder( - DynamicLoaderMacOSXDYLD::GetByteOrderFromMagic(header->magic)); - switch (header->magic) { - case llvm::MachO::MH_MAGIC: - case llvm::MachO::MH_CIGAM: - data.SetAddressByteSize(4); - load_cmd_addr += sizeof(llvm::MachO::mach_header); - break; - - case llvm::MachO::MH_MAGIC_64: - case llvm::MachO::MH_CIGAM_64: - data.SetAddressByteSize(8); - load_cmd_addr += sizeof(llvm::MachO::mach_header_64); - break; - - default: - return false; - } - - // Read the rest of dyld's mach header - if (data.GetU32(&offset, &header->cputype, - (sizeof(llvm::MachO::mach_header) / sizeof(uint32_t)) - - 1)) { - if (load_command_data == NULL) - return true; // We were able to read the mach_header and weren't asked - // to read the load command bytes - - DataBufferSP load_cmd_data_sp(new DataBufferHeap(header->sizeofcmds, 0)); - - size_t load_cmd_bytes_read = - m_process->ReadMemory(load_cmd_addr, load_cmd_data_sp->GetBytes(), - load_cmd_data_sp->GetByteSize(), error); - - if (load_cmd_bytes_read == header->sizeofcmds) { - // Set the load command data and also set the correct endian swap - // settings and the correct address size - load_command_data->SetData(load_cmd_data_sp, 0, header->sizeofcmds); - load_command_data->SetByteOrder(data.GetByteOrder()); - load_command_data->SetAddressByteSize(data.GetAddressByteSize()); - return true; // We successfully read the mach_header and the load - // command data - } - - return false; // We weren't able to read the load command data - } - } - return false; // We failed the read the mach_header -} - -//---------------------------------------------------------------------- -// Parse the load commands for an image -//---------------------------------------------------------------------- -uint32_t DynamicLoaderMacOSXDYLD::ParseLoadCommands(const DataExtractor &data, - ImageInfo &dylib_info, - FileSpec *lc_id_dylinker) { - lldb::offset_t offset = 0; - uint32_t cmd_idx; - Segment segment; - dylib_info.Clear(true); - - for (cmd_idx = 0; cmd_idx < dylib_info.header.ncmds; cmd_idx++) { - // Clear out any load command specific data from DYLIB_INFO since we are - // about to read it. - - if (data.ValidOffsetForDataOfSize(offset, - sizeof(llvm::MachO::load_command))) { - llvm::MachO::load_command load_cmd; - lldb::offset_t load_cmd_offset = offset; - load_cmd.cmd = data.GetU32(&offset); - load_cmd.cmdsize = data.GetU32(&offset); - switch (load_cmd.cmd) { - case llvm::MachO::LC_SEGMENT: { - segment.name.SetTrimmedCStringWithLength( - (const char *)data.GetData(&offset, 16), 16); - // We are putting 4 uint32_t values 4 uint64_t values so we have to use - // multiple 32 bit gets below. - segment.vmaddr = data.GetU32(&offset); - segment.vmsize = data.GetU32(&offset); - segment.fileoff = data.GetU32(&offset); - segment.filesize = data.GetU32(&offset); - // Extract maxprot, initprot, nsects and flags all at once - data.GetU32(&offset, &segment.maxprot, 4); - dylib_info.segments.push_back(segment); - } break; - - case llvm::MachO::LC_SEGMENT_64: { - segment.name.SetTrimmedCStringWithLength( - (const char *)data.GetData(&offset, 16), 16); - // Extract vmaddr, vmsize, fileoff, and filesize all at once - data.GetU64(&offset, &segment.vmaddr, 4); - // Extract maxprot, initprot, nsects and flags all at once - data.GetU32(&offset, &segment.maxprot, 4); - dylib_info.segments.push_back(segment); - } break; - - case llvm::MachO::LC_ID_DYLINKER: - if (lc_id_dylinker) { - const lldb::offset_t name_offset = - load_cmd_offset + data.GetU32(&offset); - const char *path = data.PeekCStr(name_offset); - lc_id_dylinker->SetFile(path, FileSpec::Style::native); - FileSystem::Instance().Resolve(*lc_id_dylinker); - } - break; - - case llvm::MachO::LC_UUID: - dylib_info.uuid = UUID::fromOptionalData(data.GetData(&offset, 16), 16); - break; - - default: - break; - } - // Set offset to be the beginning of the next load command. - offset = load_cmd_offset + load_cmd.cmdsize; - } - } - - // All sections listed in the dyld image info structure will all either be - // fixed up already, or they will all be off by a single slide amount that is - // determined by finding the first segment that is at file offset zero which - // also has bytes (a file size that is greater than zero) in the object file. - - // Determine the slide amount (if any) - const size_t num_sections = dylib_info.segments.size(); - for (size_t i = 0; i < num_sections; ++i) { - // Iterate through the object file sections to find the first section that - // starts of file offset zero and that has bytes in the file... - if ((dylib_info.segments[i].fileoff == 0 && - dylib_info.segments[i].filesize > 0) || - (dylib_info.segments[i].name == ConstString("__TEXT"))) { - dylib_info.slide = dylib_info.address - dylib_info.segments[i].vmaddr; - // We have found the slide amount, so we can exit this for loop. - break; - } - } - return cmd_idx; -} - -//---------------------------------------------------------------------- -// Read the mach_header and load commands for each image that the -// _dyld_all_image_infos structure points to and cache the results. -//---------------------------------------------------------------------- - -void DynamicLoaderMacOSXDYLD::UpdateImageInfosHeaderAndLoadCommands( - ImageInfo::collection &image_infos, uint32_t infos_count, - bool update_executable) { - uint32_t exe_idx = UINT32_MAX; - // Read any UUID values that we can get - for (uint32_t i = 0; i < infos_count; i++) { - if (!image_infos[i].UUIDValid()) { - DataExtractor data; // Load command data - if (!ReadMachHeader(image_infos[i].address, &image_infos[i].header, - &data)) - continue; - - ParseLoadCommands(data, image_infos[i], NULL); - - if (image_infos[i].header.filetype == llvm::MachO::MH_EXECUTE) - exe_idx = i; - } - } - - Target &target = m_process->GetTarget(); - - if (exe_idx < image_infos.size()) { - const bool can_create = true; - ModuleSP exe_module_sp( - FindTargetModuleForImageInfo(image_infos[exe_idx], can_create, NULL)); - - if (exe_module_sp) { - UpdateImageLoadAddress(exe_module_sp.get(), image_infos[exe_idx]); - - if (exe_module_sp.get() != target.GetExecutableModulePointer()) { - // Don't load dependent images since we are in dyld where we will know - // and find out about all images that are loaded. Also when setting the - // executable module, it will clear the targets module list, and if we - // have an in memory dyld module, it will get removed from the list so - // we will need to add it back after setting the executable module, so - // we first try and see if we already have a weak pointer to the dyld - // module, make it into a shared pointer, then add the executable, then - // re-add it back to make sure it is always in the list. - ModuleSP dyld_module_sp(GetDYLDModule()); - - m_process->GetTarget().SetExecutableModule(exe_module_sp, - eLoadDependentsNo); - - if (dyld_module_sp) { - if (target.GetImages().AppendIfNeeded(dyld_module_sp)) { - std::lock_guard<std::recursive_mutex> baseclass_guard(GetMutex()); - - // Also add it to the section list. - UpdateImageLoadAddress(dyld_module_sp.get(), m_dyld); - } - } - } - } - } -} - -//---------------------------------------------------------------------- -// Dump the _dyld_all_image_infos members and all current image infos that we -// have parsed to the file handle provided. -//---------------------------------------------------------------------- -void DynamicLoaderMacOSXDYLD::PutToLog(Log *log) const { - if (log == NULL) - return; - - std::lock_guard<std::recursive_mutex> guard(m_mutex); - std::lock_guard<std::recursive_mutex> baseclass_guard(GetMutex()); - log->Printf( - "dyld_all_image_infos = { version=%d, count=%d, addr=0x%8.8" PRIx64 - ", notify=0x%8.8" PRIx64 " }", - m_dyld_all_image_infos.version, m_dyld_all_image_infos.dylib_info_count, - (uint64_t)m_dyld_all_image_infos.dylib_info_addr, - (uint64_t)m_dyld_all_image_infos.notification); - size_t i; - const size_t count = m_dyld_image_infos.size(); - if (count > 0) { - log->PutCString("Loaded:"); - for (i = 0; i < count; i++) - m_dyld_image_infos[i].PutToLog(log); - } -} - -bool DynamicLoaderMacOSXDYLD::SetNotificationBreakpoint() { - DEBUG_PRINTF("DynamicLoaderMacOSXDYLD::%s() process state = %s\n", - __FUNCTION__, StateAsCString(m_process->GetState())); - if (m_break_id == LLDB_INVALID_BREAK_ID) { - if (m_dyld_all_image_infos.notification != LLDB_INVALID_ADDRESS) { - Address so_addr; - // Set the notification breakpoint and install a breakpoint callback - // function that will get called each time the breakpoint gets hit. We - // will use this to track when shared libraries get loaded/unloaded. - bool resolved = m_process->GetTarget().ResolveLoadAddress( - m_dyld_all_image_infos.notification, so_addr); - if (!resolved) { - ModuleSP dyld_module_sp = GetDYLDModule(); - if (dyld_module_sp) { - std::lock_guard<std::recursive_mutex> baseclass_guard(GetMutex()); - - UpdateImageLoadAddress(dyld_module_sp.get(), m_dyld); - resolved = m_process->GetTarget().ResolveLoadAddress( - m_dyld_all_image_infos.notification, so_addr); - } - } - - if (resolved) { - Breakpoint *dyld_break = - m_process->GetTarget().CreateBreakpoint(so_addr, true, false).get(); - dyld_break->SetCallback(DynamicLoaderMacOSXDYLD::NotifyBreakpointHit, - this, true); - dyld_break->SetBreakpointKind("shared-library-event"); - m_break_id = dyld_break->GetID(); - } - } - } - return m_break_id != LLDB_INVALID_BREAK_ID; -} - -Status DynamicLoaderMacOSXDYLD::CanLoadImage() { - Status error; - // In order for us to tell if we can load a shared library we verify that the - // dylib_info_addr isn't zero (which means no shared libraries have been set - // yet, or dyld is currently mucking with the shared library list). - if (ReadAllImageInfosStructure()) { - // TODO: also check the _dyld_global_lock_held variable in - // libSystem.B.dylib? - // TODO: check the malloc lock? - // TODO: check the objective C lock? - if (m_dyld_all_image_infos.dylib_info_addr != 0) - return error; // Success - } - - error.SetErrorString("unsafe to load or unload shared libraries"); - return error; -} - -bool DynamicLoaderMacOSXDYLD::GetSharedCacheInformation( - lldb::addr_t &base_address, UUID &uuid, LazyBool &using_shared_cache, - LazyBool &private_shared_cache) { - base_address = LLDB_INVALID_ADDRESS; - uuid.Clear(); - using_shared_cache = eLazyBoolCalculate; - private_shared_cache = eLazyBoolCalculate; - - if (m_process) { - addr_t all_image_infos = m_process->GetImageInfoAddress(); - - // The address returned by GetImageInfoAddress may be the address of dyld - // (don't want) or it may be the address of the dyld_all_image_infos - // structure (want). The first four bytes will be either the version field - // (all_image_infos) or a Mach-O file magic constant. Version 13 and higher - // of dyld_all_image_infos is required to get the sharedCacheUUID field. - - Status err; - uint32_t version_or_magic = - m_process->ReadUnsignedIntegerFromMemory(all_image_infos, 4, -1, err); - if (version_or_magic != static_cast<uint32_t>(-1) && - version_or_magic != llvm::MachO::MH_MAGIC && - version_or_magic != llvm::MachO::MH_CIGAM && - version_or_magic != llvm::MachO::MH_MAGIC_64 && - version_or_magic != llvm::MachO::MH_CIGAM_64 && - version_or_magic >= 13) { - addr_t sharedCacheUUID_address = LLDB_INVALID_ADDRESS; - int wordsize = m_process->GetAddressByteSize(); - if (wordsize == 8) { - sharedCacheUUID_address = - all_image_infos + 160; // sharedCacheUUID <mach-o/dyld_images.h> - } - if (wordsize == 4) { - sharedCacheUUID_address = - all_image_infos + 84; // sharedCacheUUID <mach-o/dyld_images.h> - } - if (sharedCacheUUID_address != LLDB_INVALID_ADDRESS) { - uuid_t shared_cache_uuid; - if (m_process->ReadMemory(sharedCacheUUID_address, shared_cache_uuid, - sizeof(uuid_t), err) == sizeof(uuid_t)) { - uuid = UUID::fromOptionalData(shared_cache_uuid, 16); - if (uuid.IsValid()) { - using_shared_cache = eLazyBoolYes; - } - } - - if (version_or_magic >= 15) { - // The sharedCacheBaseAddress field is the next one in the - // dyld_all_image_infos struct. - addr_t sharedCacheBaseAddr_address = sharedCacheUUID_address + 16; - Status error; - base_address = m_process->ReadUnsignedIntegerFromMemory( - sharedCacheBaseAddr_address, wordsize, LLDB_INVALID_ADDRESS, - error); - if (error.Fail()) - base_address = LLDB_INVALID_ADDRESS; - } - - return true; - } - - // - // add - // NB: sharedCacheBaseAddress is the next field in dyld_all_image_infos - // after - // sharedCacheUUID -- that is, 16 bytes after it, if we wanted to fetch - // it. - } - } - return false; -} - -void DynamicLoaderMacOSXDYLD::Initialize() { - PluginManager::RegisterPlugin(GetPluginNameStatic(), - GetPluginDescriptionStatic(), CreateInstance); -} - -void DynamicLoaderMacOSXDYLD::Terminate() { - PluginManager::UnregisterPlugin(CreateInstance); -} - -lldb_private::ConstString DynamicLoaderMacOSXDYLD::GetPluginNameStatic() { - static ConstString g_name("macosx-dyld"); - return g_name; -} - -const char *DynamicLoaderMacOSXDYLD::GetPluginDescriptionStatic() { - return "Dynamic loader plug-in that watches for shared library loads/unloads " - "in MacOSX user processes."; -} - -//------------------------------------------------------------------ -// PluginInterface protocol -//------------------------------------------------------------------ -lldb_private::ConstString DynamicLoaderMacOSXDYLD::GetPluginName() { - return GetPluginNameStatic(); -} - -uint32_t DynamicLoaderMacOSXDYLD::GetPluginVersion() { return 1; } - -uint32_t DynamicLoaderMacOSXDYLD::AddrByteSize() { - std::lock_guard<std::recursive_mutex> baseclass_guard(GetMutex()); - - switch (m_dyld.header.magic) { - case llvm::MachO::MH_MAGIC: - case llvm::MachO::MH_CIGAM: - return 4; - - case llvm::MachO::MH_MAGIC_64: - case llvm::MachO::MH_CIGAM_64: - return 8; - - default: - break; - } - return 0; -} - -lldb::ByteOrder DynamicLoaderMacOSXDYLD::GetByteOrderFromMagic(uint32_t magic) { - switch (magic) { - case llvm::MachO::MH_MAGIC: - case llvm::MachO::MH_MAGIC_64: - return endian::InlHostByteOrder(); - - case llvm::MachO::MH_CIGAM: - case llvm::MachO::MH_CIGAM_64: - if (endian::InlHostByteOrder() == lldb::eByteOrderBig) - return lldb::eByteOrderLittle; - else - return lldb::eByteOrderBig; - - default: - break; - } - return lldb::eByteOrderInvalid; -} diff --git a/source/Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderMacOSXDYLD.h b/source/Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderMacOSXDYLD.h deleted file mode 100644 index 3dc0f15bddf7..000000000000 --- a/source/Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderMacOSXDYLD.h +++ /dev/null @@ -1,182 +0,0 @@ -//===-- DynamicLoaderMacOSXDYLD.h -------------------------------*- C++ -*-===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// - -// This is the DynamicLoader plugin for Darwin (macOS / iPhoneOS / tvOS / -// watchOS / BridgeOS) -// platforms earlier than 2016, where lldb would read the "dyld_all_image_infos" -// dyld internal structure to understand where things were loaded and the -// solib loaded/unloaded notification function we put a breakpoint on gives us -// an array of (load address, mod time, file path) tuples. -// -// As of late 2016, the new DynamicLoaderMacOS plugin should be used, which uses -// dyld SPI functions to get the same information without reading internal dyld -// data structures. - -#ifndef liblldb_DynamicLoaderMacOSXDYLD_h_ -#define liblldb_DynamicLoaderMacOSXDYLD_h_ - -#include <mutex> -#include <vector> - -#include "lldb/Host/SafeMachO.h" -#include "lldb/Target/DynamicLoader.h" -#include "lldb/Target/Process.h" -#include "lldb/Utility/FileSpec.h" -#include "lldb/Utility/StructuredData.h" -#include "lldb/Utility/UUID.h" - -#include "DynamicLoaderDarwin.h" - -class DynamicLoaderMacOSXDYLD : public lldb_private::DynamicLoaderDarwin { -public: - DynamicLoaderMacOSXDYLD(lldb_private::Process *process); - - virtual ~DynamicLoaderMacOSXDYLD() override; - - //------------------------------------------------------------------ - // Static Functions - //------------------------------------------------------------------ - static void Initialize(); - - static void Terminate(); - - static lldb_private::ConstString GetPluginNameStatic(); - - static const char *GetPluginDescriptionStatic(); - - static lldb_private::DynamicLoader * - CreateInstance(lldb_private::Process *process, bool force); - - //------------------------------------------------------------------ - /// Called after attaching a process. - /// - /// Allow DynamicLoader plug-ins to execute some code after - /// attaching to a process. - //------------------------------------------------------------------ - bool ProcessDidExec() override; - - lldb_private::Status CanLoadImage() override; - - bool GetSharedCacheInformation( - lldb::addr_t &base_address, lldb_private::UUID &uuid, - lldb_private::LazyBool &using_shared_cache, - lldb_private::LazyBool &private_shared_cache) override; - - //------------------------------------------------------------------ - // PluginInterface protocol - //------------------------------------------------------------------ - lldb_private::ConstString GetPluginName() override; - - uint32_t GetPluginVersion() override; - -protected: - void PutToLog(lldb_private::Log *log) const; - - void DoInitialImageFetch() override; - - bool NeedToDoInitialImageFetch() override; - - bool DidSetNotificationBreakpoint() override; - - void DoClear() override; - - bool ReadDYLDInfoFromMemoryAndSetNotificationCallback(lldb::addr_t addr); - - static bool - NotifyBreakpointHit(void *baton, - lldb_private::StoppointCallbackContext *context, - lldb::user_id_t break_id, lldb::user_id_t break_loc_id); - - uint32_t AddrByteSize(); - - bool ReadMachHeader(lldb::addr_t addr, llvm::MachO::mach_header *header, - lldb_private::DataExtractor *load_command_data); - - uint32_t ParseLoadCommands(const lldb_private::DataExtractor &data, - ImageInfo &dylib_info, - lldb_private::FileSpec *lc_id_dylinker); - - struct DYLDAllImageInfos { - uint32_t version; - uint32_t dylib_info_count; // Version >= 1 - lldb::addr_t dylib_info_addr; // Version >= 1 - lldb::addr_t notification; // Version >= 1 - bool processDetachedFromSharedRegion; // Version >= 1 - bool libSystemInitialized; // Version >= 2 - lldb::addr_t dyldImageLoadAddress; // Version >= 2 - - DYLDAllImageInfos() - : version(0), dylib_info_count(0), - dylib_info_addr(LLDB_INVALID_ADDRESS), - notification(LLDB_INVALID_ADDRESS), - processDetachedFromSharedRegion(false), libSystemInitialized(false), - dyldImageLoadAddress(LLDB_INVALID_ADDRESS) {} - - void Clear() { - version = 0; - dylib_info_count = 0; - dylib_info_addr = LLDB_INVALID_ADDRESS; - notification = LLDB_INVALID_ADDRESS; - processDetachedFromSharedRegion = false; - libSystemInitialized = false; - dyldImageLoadAddress = LLDB_INVALID_ADDRESS; - } - - bool IsValid() const { return version >= 1 || version <= 6; } - }; - - static lldb::ByteOrder GetByteOrderFromMagic(uint32_t magic); - - bool SetNotificationBreakpoint() override; - - void ClearNotificationBreakpoint() override; - - // There is a little tricky bit where you might initially attach while dyld is - // updating - // the all_image_infos, and you can't read the infos, so you have to continue - // and pick it - // up when you hit the update breakpoint. At that point, you need to run this - // initialize - // function, but when you do it that way you DON'T need to do the extra work - // you would at - // the breakpoint. - // So this function will only do actual work if the image infos haven't been - // read yet. - // If it does do any work, then it will return true, and false otherwise. - // That way you can - // call it in the breakpoint action, and if it returns true you're done. - bool InitializeFromAllImageInfos(); - - bool ReadAllImageInfosStructure(); - - bool AddModulesUsingImageInfosAddress(lldb::addr_t image_infos_addr, - uint32_t image_infos_count); - - bool RemoveModulesUsingImageInfosAddress(lldb::addr_t image_infos_addr, - uint32_t image_infos_count); - - void UpdateImageInfosHeaderAndLoadCommands(ImageInfo::collection &image_infos, - uint32_t infos_count, - bool update_executable); - - bool ReadImageInfos(lldb::addr_t image_infos_addr, uint32_t image_infos_count, - ImageInfo::collection &image_infos); - - lldb::addr_t m_dyld_all_image_infos_addr; - DYLDAllImageInfos m_dyld_all_image_infos; - uint32_t m_dyld_all_image_infos_stop_id; - lldb::user_id_t m_break_id; - mutable std::recursive_mutex m_mutex; - bool m_process_image_addr_is_all_images_infos; - -private: - DISALLOW_COPY_AND_ASSIGN(DynamicLoaderMacOSXDYLD); -}; - -#endif // liblldb_DynamicLoaderMacOSXDYLD_h_ diff --git a/source/Plugins/DynamicLoader/POSIX-DYLD/CMakeLists.txt b/source/Plugins/DynamicLoader/POSIX-DYLD/CMakeLists.txt deleted file mode 100644 index 409ba92a0e19..000000000000 --- a/source/Plugins/DynamicLoader/POSIX-DYLD/CMakeLists.txt +++ /dev/null @@ -1,15 +0,0 @@ -add_lldb_library(lldbPluginDynamicLoaderPosixDYLD PLUGIN - AuxVector.cpp - DYLDRendezvous.cpp - DynamicLoaderPOSIXDYLD.cpp - - LINK_LIBS - lldbBreakpoint - lldbCore - lldbHost - lldbSymbol - lldbTarget - lldbPluginProcessElfCore - LINK_COMPONENTS - Support - ) diff --git a/source/Plugins/DynamicLoader/Static/CMakeLists.txt b/source/Plugins/DynamicLoader/Static/CMakeLists.txt deleted file mode 100644 index be54a3053470..000000000000 --- a/source/Plugins/DynamicLoader/Static/CMakeLists.txt +++ /dev/null @@ -1,10 +0,0 @@ -add_lldb_library(lldbPluginDynamicLoaderStatic PLUGIN - DynamicLoaderStatic.cpp - - LINK_LIBS - lldbCore - lldbHost - lldbSymbol - lldbTarget - lldbUtility - ) diff --git a/source/Plugins/DynamicLoader/Windows-DYLD/CMakeLists.txt b/source/Plugins/DynamicLoader/Windows-DYLD/CMakeLists.txt deleted file mode 100644 index 7557ada51466..000000000000 --- a/source/Plugins/DynamicLoader/Windows-DYLD/CMakeLists.txt +++ /dev/null @@ -1,9 +0,0 @@ -add_lldb_library(lldbPluginDynamicLoaderWindowsDYLD PLUGIN - DynamicLoaderWindowsDYLD.cpp - - LINK_LIBS - lldbCore - lldbTarget - LINK_COMPONENTS - Support - ) |