diff options
Diffstat (limited to 'source/Plugins/Process/mach-core')
-rw-r--r-- | source/Plugins/Process/mach-core/CMakeLists.txt | 6 | ||||
-rw-r--r-- | source/Plugins/Process/mach-core/Makefile | 14 | ||||
-rw-r--r-- | source/Plugins/Process/mach-core/ProcessMachCore.cpp | 520 | ||||
-rw-r--r-- | source/Plugins/Process/mach-core/ProcessMachCore.h | 164 | ||||
-rw-r--r-- | source/Plugins/Process/mach-core/ThreadMachCore.cpp | 132 | ||||
-rw-r--r-- | source/Plugins/Process/mach-core/ThreadMachCore.h | 91 |
6 files changed, 927 insertions, 0 deletions
diff --git a/source/Plugins/Process/mach-core/CMakeLists.txt b/source/Plugins/Process/mach-core/CMakeLists.txt new file mode 100644 index 0000000..ac54658 --- /dev/null +++ b/source/Plugins/Process/mach-core/CMakeLists.txt @@ -0,0 +1,6 @@ +include_directories(../Utility) + +add_lldb_library(lldbPluginProcessMachCore + ProcessMachCore.cpp + ThreadMachCore.cpp + ) diff --git a/source/Plugins/Process/mach-core/Makefile b/source/Plugins/Process/mach-core/Makefile new file mode 100644 index 0000000..6db8498 --- /dev/null +++ b/source/Plugins/Process/mach-core/Makefile @@ -0,0 +1,14 @@ +##===- source/Plugins/Process/mach-core/Makefile -----------*- Makefile -*-===## +# +# The LLVM Compiler Infrastructure +# +# This file is distributed under the University of Illinois Open Source +# License. See LICENSE.TXT for details. +# +##===----------------------------------------------------------------------===## + +LLDB_LEVEL := ../../../.. +LIBRARYNAME := lldbPluginProcessMachCore +BUILD_ARCHIVE = 1 + +include $(LLDB_LEVEL)/Makefile diff --git a/source/Plugins/Process/mach-core/ProcessMachCore.cpp b/source/Plugins/Process/mach-core/ProcessMachCore.cpp new file mode 100644 index 0000000..b199ec6 --- /dev/null +++ b/source/Plugins/Process/mach-core/ProcessMachCore.cpp @@ -0,0 +1,520 @@ +//===-- ProcessMachCore.cpp ------------------------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// C Includes +#include <errno.h> +#include <stdlib.h> + +// C++ Includes +#include "llvm/Support/MathExtras.h" +#include <mutex> + +// Other libraries and framework includes +#include "lldb/Core/DataBuffer.h" +#include "lldb/Core/Debugger.h" +#include "lldb/Core/PluginManager.h" +#include "lldb/Core/Module.h" +#include "lldb/Core/ModuleSpec.h" +#include "lldb/Core/Section.h" +#include "lldb/Core/State.h" +#include "lldb/Host/Host.h" +#include "lldb/Symbol/ObjectFile.h" +#include "lldb/Target/Target.h" +#include "lldb/Target/Thread.h" + +// Project includes +#include "ProcessMachCore.h" +#include "ThreadMachCore.h" +#include "StopInfoMachException.h" + +// Needed for the plug-in names for the dynamic loaders. +#include "lldb/Utility/SafeMachO.h" + +#include "Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderMacOSXDYLD.h" +#include "Plugins/DynamicLoader/Darwin-Kernel/DynamicLoaderDarwinKernel.h" +#include "Plugins/ObjectFile/Mach-O/ObjectFileMachO.h" + +using namespace lldb; +using namespace lldb_private; + +ConstString +ProcessMachCore::GetPluginNameStatic() +{ + static ConstString g_name("mach-o-core"); + return g_name; +} + +const char * +ProcessMachCore::GetPluginDescriptionStatic() +{ + return "Mach-O core file debugging plug-in."; +} + +void +ProcessMachCore::Terminate() +{ + PluginManager::UnregisterPlugin (ProcessMachCore::CreateInstance); +} + + +lldb::ProcessSP +ProcessMachCore::CreateInstance (lldb::TargetSP target_sp, Listener &listener, const FileSpec *crash_file) +{ + lldb::ProcessSP process_sp; + if (crash_file) + { + const size_t header_size = sizeof(llvm::MachO::mach_header); + lldb::DataBufferSP data_sp (crash_file->ReadFileContents(0, header_size)); + if (data_sp && data_sp->GetByteSize() == header_size) + { + DataExtractor data(data_sp, lldb::eByteOrderLittle, 4); + + lldb::offset_t data_offset = 0; + llvm::MachO::mach_header mach_header; + if (ObjectFileMachO::ParseHeader(data, &data_offset, mach_header)) + { + if (mach_header.filetype == llvm::MachO::MH_CORE) + process_sp.reset(new ProcessMachCore (target_sp, listener, *crash_file)); + } + } + + } + return process_sp; +} + +bool +ProcessMachCore::CanDebug(lldb::TargetSP target_sp, bool plugin_specified_by_name) +{ + if (plugin_specified_by_name) + return true; + + // For now we are just making sure the file exists for a given module + if (!m_core_module_sp && m_core_file.Exists()) + { + // Don't add the Target's architecture to the ModuleSpec - we may be working + // with a core file that doesn't have the correct cpusubtype in the header + // but we should still try to use it - ModuleSpecList::FindMatchingModuleSpec + // enforces a strict arch mach. + ModuleSpec core_module_spec(m_core_file); + Error error (ModuleList::GetSharedModule (core_module_spec, + m_core_module_sp, + NULL, + NULL, + NULL)); + + if (m_core_module_sp) + { + ObjectFile *core_objfile = m_core_module_sp->GetObjectFile(); + if (core_objfile && core_objfile->GetType() == ObjectFile::eTypeCoreFile) + return true; + } + } + return false; +} + +//---------------------------------------------------------------------- +// ProcessMachCore constructor +//---------------------------------------------------------------------- +ProcessMachCore::ProcessMachCore(lldb::TargetSP target_sp, Listener &listener, const FileSpec &core_file) : + Process (target_sp, listener), + m_core_aranges (), + m_core_module_sp (), + m_core_file (core_file), + m_dyld_addr (LLDB_INVALID_ADDRESS), + m_mach_kernel_addr (LLDB_INVALID_ADDRESS), + m_dyld_plugin_name () +{ +} + +//---------------------------------------------------------------------- +// Destructor +//---------------------------------------------------------------------- +ProcessMachCore::~ProcessMachCore() +{ + Clear(); + // We need to call finalize on the process before destroying ourselves + // to make sure all of the broadcaster cleanup goes as planned. If we + // destruct this class, then Process::~Process() might have problems + // trying to fully destroy the broadcaster. + Finalize(); +} + +//---------------------------------------------------------------------- +// PluginInterface +//---------------------------------------------------------------------- +ConstString +ProcessMachCore::GetPluginName() +{ + return GetPluginNameStatic(); +} + +uint32_t +ProcessMachCore::GetPluginVersion() +{ + return 1; +} + +bool +ProcessMachCore::GetDynamicLoaderAddress (lldb::addr_t addr) +{ + llvm::MachO::mach_header header; + Error error; + if (DoReadMemory (addr, &header, sizeof(header), error) != sizeof(header)) + 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); + } + + // TODO: swap header if needed... + //printf("0x%16.16" PRIx64 ": magic = 0x%8.8x, file_type= %u\n", vaddr, header.magic, header.filetype); + if (header.magic == llvm::MachO::MH_MAGIC || + header.magic == llvm::MachO::MH_MAGIC_64) + { + // Check MH_EXECUTABLE to see if we can find the mach image + // that contains the shared library list. The dynamic loader + // (dyld) is what contains the list for user applications, + // and the mach kernel contains a global that has the list + // of kexts to load + switch (header.filetype) + { + case llvm::MachO::MH_DYLINKER: + //printf("0x%16.16" PRIx64 ": file_type = MH_DYLINKER\n", vaddr); + // Address of dyld "struct mach_header" in the core file + m_dyld_addr = addr; + return true; + + case llvm::MachO::MH_EXECUTE: + //printf("0x%16.16" PRIx64 ": file_type = MH_EXECUTE\n", vaddr); + // Check MH_EXECUTABLE file types to see if the dynamic link object flag + // is NOT set. If it isn't, then we have a mach_kernel. + if ((header.flags & llvm::MachO::MH_DYLDLINK) == 0) + { + // Address of the mach kernel "struct mach_header" in the core file. + m_mach_kernel_addr = addr; + return true; + } + break; + } + } + return false; +} + +//---------------------------------------------------------------------- +// Process Control +//---------------------------------------------------------------------- +Error +ProcessMachCore::DoLoadCore () +{ + Error error; + if (!m_core_module_sp) + { + error.SetErrorString ("invalid core module"); + return error; + } + + ObjectFile *core_objfile = m_core_module_sp->GetObjectFile(); + if (core_objfile == NULL) + { + error.SetErrorString ("invalid core object file"); + return error; + } + + if (core_objfile->GetNumThreadContexts() == 0) + { + error.SetErrorString ("core file doesn't contain any LC_THREAD load commands, or the LC_THREAD architecture is not supported in this lldb"); + return error; + } + + SectionList *section_list = core_objfile->GetSectionList(); + if (section_list == NULL) + { + error.SetErrorString ("core file has no sections"); + return error; + } + + const uint32_t num_sections = section_list->GetNumSections(0); + if (num_sections == 0) + { + error.SetErrorString ("core file has no sections"); + return error; + } + + SetCanJIT(false); + + llvm::MachO::mach_header header; + DataExtractor data (&header, + sizeof(header), + m_core_module_sp->GetArchitecture().GetByteOrder(), + m_core_module_sp->GetArchitecture().GetAddressByteSize()); + + bool ranges_are_sorted = true; + addr_t vm_addr = 0; + for (uint32_t i=0; i<num_sections; ++i) + { + Section *section = section_list->GetSectionAtIndex (i).get(); + if (section) + { + lldb::addr_t section_vm_addr = section->GetFileAddress(); + FileRange file_range (section->GetFileOffset(), section->GetFileSize()); + VMRangeToFileOffset::Entry range_entry (section_vm_addr, + section->GetByteSize(), + file_range); + + if (vm_addr > section_vm_addr) + ranges_are_sorted = false; + vm_addr = section->GetFileAddress(); + VMRangeToFileOffset::Entry *last_entry = m_core_aranges.Back(); +// printf ("LC_SEGMENT[%u] arange=[0x%16.16" PRIx64 " - 0x%16.16" PRIx64 "), frange=[0x%8.8x - 0x%8.8x)\n", +// i, +// range_entry.GetRangeBase(), +// range_entry.GetRangeEnd(), +// range_entry.data.GetRangeBase(), +// range_entry.data.GetRangeEnd()); + + if (last_entry && + last_entry->GetRangeEnd() == range_entry.GetRangeBase() && + last_entry->data.GetRangeEnd() == range_entry.data.GetRangeBase()) + { + last_entry->SetRangeEnd (range_entry.GetRangeEnd()); + last_entry->data.SetRangeEnd (range_entry.data.GetRangeEnd()); + //puts("combine"); + } + else + { + m_core_aranges.Append(range_entry); + } + } + } + if (!ranges_are_sorted) + { + m_core_aranges.Sort(); + } + + if (m_dyld_addr == LLDB_INVALID_ADDRESS || m_mach_kernel_addr == LLDB_INVALID_ADDRESS) + { + // We need to locate the main executable in the memory ranges + // we have in the core file. We need to search for both a user-process dyld binary + // and a kernel binary in memory; we must look at all the pages in the binary so + // we don't miss one or the other. Step through all memory segments searching for + // a kernel binary and for a user process dyld -- we'll decide which to prefer + // later if both are present. + + const size_t num_core_aranges = m_core_aranges.GetSize(); + for (size_t i = 0; + i < num_core_aranges && (m_dyld_addr == LLDB_INVALID_ADDRESS || m_mach_kernel_addr == LLDB_INVALID_ADDRESS); + ++i) + { + const VMRangeToFileOffset::Entry *entry = m_core_aranges.GetEntryAtIndex(i); + lldb::addr_t section_vm_addr_start = entry->GetRangeBase(); + lldb::addr_t section_vm_addr_end = entry->GetRangeEnd(); + for (lldb::addr_t section_vm_addr = section_vm_addr_start; + section_vm_addr < section_vm_addr_end; + section_vm_addr += 0x1000) + { + GetDynamicLoaderAddress (section_vm_addr); + } + } + } + + // If we found both a user-process dyld and a kernel binary, we need to decide + // which to prefer. + if (GetCorefilePreference() == eKernelCorefile) + { + if (m_mach_kernel_addr != LLDB_INVALID_ADDRESS) + { + m_dyld_plugin_name = DynamicLoaderDarwinKernel::GetPluginNameStatic(); + } + else if (m_dyld_addr != LLDB_INVALID_ADDRESS) + { + m_dyld_plugin_name = DynamicLoaderMacOSXDYLD::GetPluginNameStatic(); + } + } + else + { + if (m_dyld_addr != LLDB_INVALID_ADDRESS) + { + m_dyld_plugin_name = DynamicLoaderMacOSXDYLD::GetPluginNameStatic(); + } + else if (m_mach_kernel_addr != LLDB_INVALID_ADDRESS) + { + m_dyld_plugin_name = DynamicLoaderDarwinKernel::GetPluginNameStatic(); + } + } + + // Even if the architecture is set in the target, we need to override + // it to match the core file which is always single arch. + ArchSpec arch (m_core_module_sp->GetArchitecture()); + if (arch.GetCore() == ArchSpec::eCore_x86_32_i486) + { + arch.SetTriple ("i386", GetTarget().GetPlatform().get()); + } + if (arch.IsValid()) + GetTarget().SetArchitecture(arch); + + return error; +} + +lldb_private::DynamicLoader * +ProcessMachCore::GetDynamicLoader () +{ + if (m_dyld_ap.get() == NULL) + m_dyld_ap.reset (DynamicLoader::FindPlugin(this, m_dyld_plugin_name.IsEmpty() ? NULL : m_dyld_plugin_name.GetCString())); + return m_dyld_ap.get(); +} + +bool +ProcessMachCore::UpdateThreadList (ThreadList &old_thread_list, ThreadList &new_thread_list) +{ + if (old_thread_list.GetSize(false) == 0) + { + // Make up the thread the first time this is called so we can setup our one and only + // core thread state. + ObjectFile *core_objfile = m_core_module_sp->GetObjectFile(); + + if (core_objfile) + { + const uint32_t num_threads = core_objfile->GetNumThreadContexts (); + for (lldb::tid_t tid = 0; tid < num_threads; ++tid) + { + ThreadSP thread_sp(new ThreadMachCore (*this, tid)); + new_thread_list.AddThread (thread_sp); + } + } + } + else + { + const uint32_t num_threads = old_thread_list.GetSize(false); + for (uint32_t i=0; i<num_threads; ++i) + new_thread_list.AddThread (old_thread_list.GetThreadAtIndex (i, false)); + } + return new_thread_list.GetSize(false) > 0; +} + +void +ProcessMachCore::RefreshStateAfterStop () +{ + // Let all threads recover from stopping and do any clean up based + // on the previous thread state (if any). + m_thread_list.RefreshStateAfterStop(); + //SetThreadStopInfo (m_last_stop_packet); +} + +Error +ProcessMachCore::DoDestroy () +{ + return Error(); +} + +//------------------------------------------------------------------ +// Process Queries +//------------------------------------------------------------------ + +bool +ProcessMachCore::IsAlive () +{ + return true; +} + +bool +ProcessMachCore::WarnBeforeDetach () const +{ + return false; +} + +//------------------------------------------------------------------ +// Process Memory +//------------------------------------------------------------------ +size_t +ProcessMachCore::ReadMemory (addr_t addr, void *buf, size_t size, Error &error) +{ + // Don't allow the caching that lldb_private::Process::ReadMemory does + // since in core files we have it all cached our our core file anyway. + return DoReadMemory (addr, buf, size, error); +} + +size_t +ProcessMachCore::DoReadMemory (addr_t addr, void *buf, size_t size, Error &error) +{ + ObjectFile *core_objfile = m_core_module_sp->GetObjectFile(); + + if (core_objfile) + { + const VMRangeToFileOffset::Entry *core_memory_entry = m_core_aranges.FindEntryThatContains (addr); + if (core_memory_entry) + { + const addr_t offset = addr - core_memory_entry->GetRangeBase(); + const addr_t bytes_left = core_memory_entry->GetRangeEnd() - addr; + size_t bytes_to_read = size; + if (bytes_to_read > bytes_left) + bytes_to_read = bytes_left; + return core_objfile->CopyData (core_memory_entry->data.GetRangeBase() + offset, bytes_to_read, buf); + } + else + { + error.SetErrorStringWithFormat ("core file does not contain 0x%" PRIx64, addr); + } + } + return 0; +} + +void +ProcessMachCore::Clear() +{ + m_thread_list.Clear(); +} + +void +ProcessMachCore::Initialize() +{ + static std::once_flag g_once_flag; + + std::call_once(g_once_flag, []() { + PluginManager::RegisterPlugin (GetPluginNameStatic(), + GetPluginDescriptionStatic(), + CreateInstance); + }); +} + +addr_t +ProcessMachCore::GetImageInfoAddress() +{ + // If we found both a user-process dyld and a kernel binary, we need to decide + // which to prefer. + if (GetCorefilePreference() == eKernelCorefile) + { + if (m_mach_kernel_addr != LLDB_INVALID_ADDRESS) + { + return m_mach_kernel_addr; + } + return m_dyld_addr; + } + else + { + if (m_dyld_addr != LLDB_INVALID_ADDRESS) + { + return m_dyld_addr; + } + return m_mach_kernel_addr; + } +} + + +lldb_private::ObjectFile * +ProcessMachCore::GetCoreObjectFile () +{ + return m_core_module_sp->GetObjectFile(); +} diff --git a/source/Plugins/Process/mach-core/ProcessMachCore.h b/source/Plugins/Process/mach-core/ProcessMachCore.h new file mode 100644 index 0000000..2de0b77 --- /dev/null +++ b/source/Plugins/Process/mach-core/ProcessMachCore.h @@ -0,0 +1,164 @@ +//===-- ProcessMachCore.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_ProcessMachCore_h_ +#define liblldb_ProcessMachCore_h_ + +// C Includes +// C++ Includes +#include <list> +#include <vector> + +// Other libraries and framework includes +// Project includes +#include "lldb/Core/ConstString.h" +#include "lldb/Core/Error.h" +#include "lldb/Target/Process.h" + +class ThreadKDP; + +class ProcessMachCore : public lldb_private::Process +{ +public: + //------------------------------------------------------------------ + // Constructors and Destructors + //------------------------------------------------------------------ + ProcessMachCore(lldb::TargetSP target_sp, + lldb_private::Listener &listener, + const lldb_private::FileSpec &core_file); + + ~ProcessMachCore() override; + + static lldb::ProcessSP + CreateInstance (lldb::TargetSP target_sp, + lldb_private::Listener &listener, + const lldb_private::FileSpec *crash_file_path); + + static void + Initialize(); + + static void + Terminate(); + + static lldb_private::ConstString + GetPluginNameStatic(); + + static const char * + GetPluginDescriptionStatic(); + + //------------------------------------------------------------------ + // Check if a given Process + //------------------------------------------------------------------ + bool + CanDebug (lldb::TargetSP target_sp, + bool plugin_specified_by_name) override; + + //------------------------------------------------------------------ + // Creating a new process, or attaching to an existing one + //------------------------------------------------------------------ + lldb_private::Error + DoLoadCore () override; + + lldb_private::DynamicLoader * + GetDynamicLoader () override; + + //------------------------------------------------------------------ + // PluginInterface protocol + //------------------------------------------------------------------ + lldb_private::ConstString + GetPluginName() override; + + uint32_t + GetPluginVersion() override; + + //------------------------------------------------------------------ + // Process Control + //------------------------------------------------------------------ + lldb_private::Error + DoDestroy () override; + + void + RefreshStateAfterStop() override; + + //------------------------------------------------------------------ + // Process Queries + //------------------------------------------------------------------ + bool + IsAlive () override; + + bool + WarnBeforeDetach () const override; + + //------------------------------------------------------------------ + // Process Memory + //------------------------------------------------------------------ + size_t + ReadMemory (lldb::addr_t addr, void *buf, size_t size, lldb_private::Error &error) override; + + size_t + DoReadMemory (lldb::addr_t addr, void *buf, size_t size, lldb_private::Error &error) override; + + lldb::addr_t + GetImageInfoAddress () override; + +protected: + friend class ThreadMachCore; + + void + Clear ( ); + + bool + UpdateThreadList (lldb_private::ThreadList &old_thread_list, + lldb_private::ThreadList &new_thread_list) override; + + lldb_private::ObjectFile * + GetCoreObjectFile (); +private: + bool + GetDynamicLoaderAddress (lldb::addr_t addr); + + typedef enum CorefilePreference { eUserProcessCorefile, eKernelCorefile } CorefilePreferences; + + //------------------------------------------------------------------ + /// If a core file can be interpreted multiple ways, this establishes + /// which style wins. + /// + /// If a core file contains both a kernel binary and a user-process + /// dynamic loader, lldb needs to pick one over the other. This could + /// be a kernel corefile that happens to have a coyp of dyld in its + /// memory. Or it could be a user process coredump of lldb while doing + /// kernel debugging - so a copy of the kernel is in its heap. This + /// should become a setting so it can be over-ridden when necessary. + //------------------------------------------------------------------ + CorefilePreference + GetCorefilePreference () + { + // For now, if both user process and kernel binaries a present, + // assume this is a kernel coredump which has a copy of a user + // process dyld in one of its pages. + return eKernelCorefile; + } + + //------------------------------------------------------------------ + // For ProcessMachCore only + //------------------------------------------------------------------ + typedef lldb_private::Range<lldb::addr_t, lldb::addr_t> FileRange; + typedef lldb_private::RangeDataVector<lldb::addr_t, lldb::addr_t, FileRange> VMRangeToFileOffset; + + VMRangeToFileOffset m_core_aranges; + lldb::ModuleSP m_core_module_sp; + lldb_private::FileSpec m_core_file; + lldb::addr_t m_dyld_addr; + lldb::addr_t m_mach_kernel_addr; + lldb_private::ConstString m_dyld_plugin_name; + + DISALLOW_COPY_AND_ASSIGN (ProcessMachCore); +}; + +#endif // liblldb_ProcessMachCore_h_ diff --git a/source/Plugins/Process/mach-core/ThreadMachCore.cpp b/source/Plugins/Process/mach-core/ThreadMachCore.cpp new file mode 100644 index 0000000..2720c91 --- /dev/null +++ b/source/Plugins/Process/mach-core/ThreadMachCore.cpp @@ -0,0 +1,132 @@ +//===-- ThreadMachCore.cpp --------------------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + + +#include "ThreadMachCore.h" + +#include "lldb/Utility/SafeMachO.h" + +#include "lldb/Core/ArchSpec.h" +#include "lldb/Core/DataExtractor.h" +#include "lldb/Core/StreamString.h" +#include "lldb/Core/State.h" +#include "lldb/Symbol/ObjectFile.h" +#include "lldb/Target/Process.h" +#include "lldb/Target/RegisterContext.h" +#include "lldb/Target/StopInfo.h" +#include "lldb/Target/Target.h" +#include "lldb/Target/Unwind.h" +#include "lldb/Breakpoint/Watchpoint.h" + +#include "ProcessMachCore.h" +//#include "RegisterContextKDP_arm.h" +//#include "RegisterContextKDP_i386.h" +//#include "RegisterContextKDP_x86_64.h" + +using namespace lldb; +using namespace lldb_private; + +//---------------------------------------------------------------------- +// Thread Registers +//---------------------------------------------------------------------- + +ThreadMachCore::ThreadMachCore (Process &process, lldb::tid_t tid) : + Thread(process, tid), + m_thread_name (), + m_dispatch_queue_name (), + m_thread_dispatch_qaddr (LLDB_INVALID_ADDRESS), + m_thread_reg_ctx_sp () +{ +} + +ThreadMachCore::~ThreadMachCore () +{ + DestroyThread(); +} + +const char * +ThreadMachCore::GetName () +{ + if (m_thread_name.empty()) + return NULL; + return m_thread_name.c_str(); +} + +void +ThreadMachCore::RefreshStateAfterStop() +{ + // Invalidate all registers in our register context. We don't set "force" to + // true because the stop reply packet might have had some register values + // that were expedited and these will already be copied into the register + // context by the time this function gets called. The KDPRegisterContext + // class has been made smart enough to detect when it needs to invalidate + // which registers are valid by putting hooks in the register read and + // register supply functions where they check the process stop ID and do + // the right thing. + const bool force = false; + GetRegisterContext()->InvalidateIfNeeded (force); +} + +bool +ThreadMachCore::ThreadIDIsValid (lldb::tid_t thread) +{ + return thread != 0; +} + +lldb::RegisterContextSP +ThreadMachCore::GetRegisterContext () +{ + if (m_reg_context_sp.get() == NULL) + m_reg_context_sp = CreateRegisterContextForFrame (NULL); + return m_reg_context_sp; +} + +lldb::RegisterContextSP +ThreadMachCore::CreateRegisterContextForFrame (StackFrame *frame) +{ + lldb::RegisterContextSP reg_ctx_sp; + uint32_t concrete_frame_idx = 0; + + if (frame) + concrete_frame_idx = frame->GetConcreteFrameIndex (); + + if (concrete_frame_idx == 0) + { + if (!m_thread_reg_ctx_sp) + { + ProcessSP process_sp (GetProcess()); + + ObjectFile *core_objfile = static_cast<ProcessMachCore *>(process_sp.get())->GetCoreObjectFile (); + if (core_objfile) + m_thread_reg_ctx_sp = core_objfile->GetThreadContextAtIndex (GetID(), *this); + } + reg_ctx_sp = m_thread_reg_ctx_sp; + } + else + { + Unwind *unwinder = GetUnwinder (); + if (unwinder) + reg_ctx_sp = unwinder->CreateRegisterContextForFrame (frame); + } + return reg_ctx_sp; +} + +bool +ThreadMachCore::CalculateStopInfo () +{ + ProcessSP process_sp (GetProcess()); + if (process_sp) + { + SetStopInfo(StopInfo::CreateStopReasonWithSignal (*this, SIGSTOP)); + return true; + } + return false; +} + + diff --git a/source/Plugins/Process/mach-core/ThreadMachCore.h b/source/Plugins/Process/mach-core/ThreadMachCore.h new file mode 100644 index 0000000..2597354 --- /dev/null +++ b/source/Plugins/Process/mach-core/ThreadMachCore.h @@ -0,0 +1,91 @@ +//===-- ThreadMachCore.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_ThreadMachCore_h_ +#define liblldb_ThreadMachCore_h_ + +// C Includes +// C++ Includes +#include <string> + +// Other libraries and framework includes +// Project includes +#include "lldb/Target/Thread.h" + +class ProcessMachCore; + +class ThreadMachCore : public lldb_private::Thread +{ +public: + ThreadMachCore (lldb_private::Process &process, + lldb::tid_t tid); + + ~ThreadMachCore() override; + + void + RefreshStateAfterStop() override; + + const char * + GetName() override; + + lldb::RegisterContextSP + GetRegisterContext() override; + + lldb::RegisterContextSP + CreateRegisterContextForFrame(lldb_private::StackFrame *frame) override; + + static bool + ThreadIDIsValid (lldb::tid_t thread); + + bool + ShouldStop (bool &step_more); + + const char * + GetBasicInfoAsString (); + + void + SetName(const char *name) override + { + if (name && name[0]) + m_thread_name.assign (name); + else + m_thread_name.clear(); + } + + lldb::addr_t + GetThreadDispatchQAddr () + { + return m_thread_dispatch_qaddr; + } + + void + SetThreadDispatchQAddr (lldb::addr_t thread_dispatch_qaddr) + { + m_thread_dispatch_qaddr = thread_dispatch_qaddr; + } + +protected: + friend class ProcessMachCore; + + //------------------------------------------------------------------ + // Member variables. + //------------------------------------------------------------------ + std::string m_thread_name; + std::string m_dispatch_queue_name; + lldb::addr_t m_thread_dispatch_qaddr; + lldb::RegisterContextSP m_thread_reg_ctx_sp; + + //------------------------------------------------------------------ + // Protected member functions. + //------------------------------------------------------------------ + bool + CalculateStopInfo() override; +}; + +#endif // liblldb_ThreadMachCore_h_ |