diff options
author | Dimitry Andric <dim@FreeBSD.org> | 2016-07-23 20:50:09 +0000 |
---|---|---|
committer | Dimitry Andric <dim@FreeBSD.org> | 2016-07-23 20:50:09 +0000 |
commit | f3fbd1c0586ff6ec7895991e6c28f61a503c36a8 (patch) | |
tree | 48d008fd3df8c0e73271a4b18474e0aac6dbfe33 /source/Plugins/Process/Windows | |
parent | 2fc5d2d1dfaf623ce4e24cd8590565902f8c557c (diff) | |
download | src-vendor/lldb/lldb-release_39-r276489.tar.gz src-vendor/lldb/lldb-release_39-r276489.zip |
Vendor import of lldb release_39 branch r276489:vendor/lldb/lldb-release_39-r276489
Diffstat (limited to 'source/Plugins/Process/Windows')
11 files changed, 574 insertions, 349 deletions
diff --git a/source/Plugins/Process/Windows/Common/NtStructures.h b/source/Plugins/Process/Windows/Common/NtStructures.h new file mode 100644 index 000000000000..6c688d9068d5 --- /dev/null +++ b/source/Plugins/Process/Windows/Common/NtStructures.h @@ -0,0 +1,32 @@ +//===-- NtStructures.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_Plugins_Process_Windows_Common_NtStructures_h_ +#define liblldb_Plugins_Process_Windows_Common_NtStructures_h_ + +#include "lldb/Host/windows/windows.h" + +// This describes the layout of a TEB (Thread Environment Block) for a 64-bit +// process. It's adapted from the 32-bit TEB in winternl.h. Currently, we care +// only about the position of the TlsSlots. +struct TEB64 +{ + ULONG64 Reserved1[12]; + ULONG64 ProcessEnvironmentBlock; + ULONG64 Reserved2[399]; + BYTE Reserved3[1952]; + ULONG64 TlsSlots[64]; + BYTE Reserved4[8]; + ULONG64 Reserved5[26]; + ULONG64 ReservedForOle; // Windows 2000 only + ULONG64 Reserved6[4]; + ULONG64 TlsExpansionSlots; +}; + +#endif diff --git a/source/Plugins/Process/Windows/Common/ProcessWindows.cpp b/source/Plugins/Process/Windows/Common/ProcessWindows.cpp index 0e6900d8fb7f..2c3f9fbecf92 100644 --- a/source/Plugins/Process/Windows/Common/ProcessWindows.cpp +++ b/source/Plugins/Process/Windows/Common/ProcessWindows.cpp @@ -29,8 +29,8 @@ namespace lldb_private //------------------------------------------------------------------------------ // Constructors and destructors. -ProcessWindows::ProcessWindows(lldb::TargetSP target_sp, Listener &listener) - : lldb_private::Process(target_sp, listener) +ProcessWindows::ProcessWindows(lldb::TargetSP target_sp, lldb::ListenerSP listener_sp) + : lldb_private::Process(target_sp, listener_sp) { } diff --git a/source/Plugins/Process/Windows/Common/ProcessWindows.h b/source/Plugins/Process/Windows/Common/ProcessWindows.h index 2a437c0ca909..0ee42e2ae1f1 100644 --- a/source/Plugins/Process/Windows/Common/ProcessWindows.h +++ b/source/Plugins/Process/Windows/Common/ProcessWindows.h @@ -25,7 +25,7 @@ public: // Constructors and destructors //------------------------------------------------------------------ ProcessWindows(lldb::TargetSP target_sp, - lldb_private::Listener &listener); + lldb::ListenerSP listener_sp); ~ProcessWindows(); diff --git a/source/Plugins/Process/Windows/Common/x64/RegisterContextWindows_x64.cpp b/source/Plugins/Process/Windows/Common/x64/RegisterContextWindows_x64.cpp index 103cff4a2a56..3a9c31a0b776 100644 --- a/source/Plugins/Process/Windows/Common/x64/RegisterContextWindows_x64.cpp +++ b/source/Plugins/Process/Windows/Common/x64/RegisterContextWindows_x64.cpp @@ -136,6 +136,8 @@ RegisterInfo g_register_infos[] = { nullptr}, }; +static size_t k_num_register_infos = llvm::array_lengthof(g_register_infos); + // Array of lldb register numbers used to define the set of all General Purpose Registers uint32_t g_gpr_reg_indices[] = {eRegisterIndexRax, eRegisterIndexRbx, eRegisterIndexRcx, eRegisterIndexRdx, eRegisterIndexRdi, eRegisterIndexRsi, eRegisterIndexR8, eRegisterIndexR9, @@ -169,7 +171,9 @@ RegisterContextWindows_x64::GetRegisterCount() const RegisterInfo * RegisterContextWindows_x64::GetRegisterInfoAtIndex(size_t reg) { - return &g_register_infos[reg]; + if (reg < k_num_register_infos) + return &g_register_infos[reg]; + return NULL; } size_t diff --git a/source/Plugins/Process/Windows/Common/x86/RegisterContextWindows_x86.cpp b/source/Plugins/Process/Windows/Common/x86/RegisterContextWindows_x86.cpp index e57e1effec9c..11733eee7cb4 100644 --- a/source/Plugins/Process/Windows/Common/x86/RegisterContextWindows_x86.cpp +++ b/source/Plugins/Process/Windows/Common/x86/RegisterContextWindows_x86.cpp @@ -64,6 +64,7 @@ RegisterInfo g_register_infos[] = { DEFINE_GPR(eip, "pc"), { ehframe_eip_i386, dwarf_eip_i386, LLDB_REGNUM_GENERIC_PC, LLDB_INVALID_REGNUM, lldb_eip_i386 }, nullptr, nullptr}, { DEFINE_GPR_BIN(eflags, "flags"), { ehframe_eflags_i386, dwarf_eflags_i386, LLDB_REGNUM_GENERIC_FLAGS, LLDB_INVALID_REGNUM, lldb_eflags_i386}, nullptr, nullptr}, }; +static size_t k_num_register_infos = llvm::array_lengthof(g_register_infos); // Array of lldb register numbers used to define the set of all General Purpose Registers uint32_t g_gpr_reg_indices[] = @@ -106,7 +107,9 @@ RegisterContextWindows_x86::GetRegisterCount() const RegisterInfo * RegisterContextWindows_x86::GetRegisterInfoAtIndex(size_t reg) { - return &g_register_infos[reg]; + if (reg < k_num_register_infos) + return &g_register_infos[reg]; + return NULL; } size_t @@ -131,48 +134,42 @@ RegisterContextWindows_x86::ReadRegister(const RegisterInfo *reg_info, RegisterV switch (reg) { case lldb_eax_i386: - WINLOG_IFALL(WINDOWS_LOG_REGISTERS, "Read value 0x%x from EAX", m_context.Eax); - reg_value.SetUInt32(m_context.Eax); - break; + return ReadRegisterHelper(CONTEXT_INTEGER, "EAX", m_context.Eax, reg_value); case lldb_ebx_i386: - WINLOG_IFALL(WINDOWS_LOG_REGISTERS, "Read value 0x%x from EBX", m_context.Ebx); - reg_value.SetUInt32(m_context.Ebx); - break; + return ReadRegisterHelper(CONTEXT_INTEGER, "EBX", m_context.Ebx, reg_value); case lldb_ecx_i386: - WINLOG_IFALL(WINDOWS_LOG_REGISTERS, "Read value 0x%x from ECX", m_context.Ecx); - reg_value.SetUInt32(m_context.Ecx); - break; + return ReadRegisterHelper(CONTEXT_INTEGER, "ECX", m_context.Ecx, reg_value); case lldb_edx_i386: - WINLOG_IFALL(WINDOWS_LOG_REGISTERS, "Read value 0x%x from EDX", m_context.Edx); - reg_value.SetUInt32(m_context.Edx); - break; + return ReadRegisterHelper(CONTEXT_INTEGER, "EDX", m_context.Edx, reg_value); case lldb_edi_i386: - WINLOG_IFALL(WINDOWS_LOG_REGISTERS, "Read value 0x%x from EDI", m_context.Edi); - reg_value.SetUInt32(m_context.Edi); - break; + return ReadRegisterHelper(CONTEXT_INTEGER, "EDI", m_context.Edi, reg_value); case lldb_esi_i386: - WINLOG_IFALL(WINDOWS_LOG_REGISTERS, "Read value 0x%x from ESI", m_context.Esi); - reg_value.SetUInt32(m_context.Esi); - break; + return ReadRegisterHelper(CONTEXT_INTEGER, "ESI", m_context.Esi, reg_value); case lldb_ebp_i386: - WINLOG_IFALL(WINDOWS_LOG_REGISTERS, "Read value 0x%x from EBP", m_context.Ebp); - reg_value.SetUInt32(m_context.Ebp); - break; + return ReadRegisterHelper(CONTEXT_CONTROL, "EBP", m_context.Ebp, reg_value); case lldb_esp_i386: - WINLOG_IFALL(WINDOWS_LOG_REGISTERS, "Read value 0x%x from ESP", m_context.Esp); - reg_value.SetUInt32(m_context.Esp); - break; + return ReadRegisterHelper(CONTEXT_CONTROL, "ESP", m_context.Esp, reg_value); case lldb_eip_i386: - WINLOG_IFALL(WINDOWS_LOG_REGISTERS, "Read value 0x%x from EIP", m_context.Eip); - reg_value.SetUInt32(m_context.Eip); - break; + return ReadRegisterHelper(CONTEXT_CONTROL, "EIP", m_context.Eip, reg_value); case lldb_eflags_i386: - WINLOG_IFALL(WINDOWS_LOG_REGISTERS, "Read value 0x%x from EFLAGS", m_context.EFlags); - reg_value.SetUInt32(m_context.EFlags); - break; + return ReadRegisterHelper(CONTEXT_CONTROL, "EFLAGS", m_context.EFlags, reg_value); default: WINWARN_IFALL(WINDOWS_LOG_REGISTERS, "Requested unknown register %u", reg); break; } + return false; +} + +bool +RegisterContextWindows_x86::ReadRegisterHelper(DWORD flags_required, const char *reg_name, DWORD value, + RegisterValue ®_value) const +{ + if ((m_context.ContextFlags & flags_required) != flags_required) + { + WINLOG_IFALL(WINDOWS_LOG_REGISTERS, "Thread context doesn't have %s", reg_name); + return false; + } + WINLOG_IFALL(WINDOWS_LOG_REGISTERS, "Read value 0x%x from %s", value, reg_name); + reg_value.SetUInt32(value); return true; } diff --git a/source/Plugins/Process/Windows/Common/x86/RegisterContextWindows_x86.h b/source/Plugins/Process/Windows/Common/x86/RegisterContextWindows_x86.h index 7d854ef64a5c..6c29d54dcae2 100644 --- a/source/Plugins/Process/Windows/Common/x86/RegisterContextWindows_x86.h +++ b/source/Plugins/Process/Windows/Common/x86/RegisterContextWindows_x86.h @@ -41,6 +41,9 @@ class RegisterContextWindows_x86 : public RegisterContextWindows bool ReadRegister(const RegisterInfo *reg_info, RegisterValue ®_value) override; +private: + bool + ReadRegisterHelper(DWORD flags_required, const char *reg_name, DWORD value, RegisterValue ®_value) const; }; } diff --git a/source/Plugins/Process/Windows/Live/DebuggerThread.cpp b/source/Plugins/Process/Windows/Live/DebuggerThread.cpp index d058a412c896..2823474cbd5e 100644 --- a/source/Plugins/Process/Windows/Live/DebuggerThread.cpp +++ b/source/Plugins/Process/Windows/Live/DebuggerThread.cpp @@ -27,6 +27,7 @@ #include "Plugins/Process/Windows/Common/ProcessWindowsLog.h" #include "llvm/ADT/STLExtras.h" +#include "llvm/Support/ConvertUTF.h" #include "llvm/Support/raw_ostream.h" using namespace lldb; @@ -378,7 +379,7 @@ DebuggerThread::HandleExceptionEvent(const EXCEPTION_DEBUG_INFO &info, DWORD thr { WINLOG_IFANY(WINDOWS_LOG_EVENT | WINDOWS_LOG_EXCEPTION | WINDOWS_LOG_PROCESS, "Breakpoint exception is cue to detach from process 0x%x", - m_pid_to_detach); + m_pid_to_detach.load()); ::DebugActiveProcessStop(m_pid_to_detach); m_detached = true; } @@ -484,13 +485,15 @@ DebuggerThread::HandleLoadDllEvent(const LOAD_DLL_DEBUG_INFO &info, DWORD thread return DBG_CONTINUE; } - std::vector<char> buffer(1); - DWORD required_size = GetFinalPathNameByHandle(info.hFile, &buffer[0], 0, VOLUME_NAME_DOS); + std::vector<wchar_t> buffer(1); + DWORD required_size = GetFinalPathNameByHandleW(info.hFile, &buffer[0], 0, VOLUME_NAME_DOS); if (required_size > 0) { buffer.resize(required_size + 1); - required_size = GetFinalPathNameByHandle(info.hFile, &buffer[0], required_size + 1, VOLUME_NAME_DOS); - llvm::StringRef path_str(&buffer[0]); + required_size = GetFinalPathNameByHandleW(info.hFile, &buffer[0], required_size, VOLUME_NAME_DOS); + std::string path_str_utf8; + llvm::convertWideToUTF8(buffer.data(), path_str_utf8); + llvm::StringRef path_str = path_str_utf8; const char *path = path_str.data(); if (path_str.startswith("\\\\?\\")) path += 4; diff --git a/source/Plugins/Process/Windows/Live/ProcessWindowsLive.cpp b/source/Plugins/Process/Windows/Live/ProcessWindowsLive.cpp index 855289d67bc7..300e0caa4378 100644 --- a/source/Plugins/Process/Windows/Live/ProcessWindowsLive.cpp +++ b/source/Plugins/Process/Windows/Live/ProcessWindowsLive.cpp @@ -47,6 +47,7 @@ #include "ProcessWindowsLive.h" #include "TargetThreadWindowsLive.h" +#include "llvm/Support/ConvertUTF.h" #include "llvm/Support/Format.h" #include "llvm/Support/raw_ostream.h" @@ -61,17 +62,19 @@ namespace std::string GetProcessExecutableName(HANDLE process_handle) { - std::vector<char> file_name; + std::vector<wchar_t> file_name; DWORD file_name_size = MAX_PATH; // first guess, not an absolute limit DWORD copied = 0; do { file_name_size *= 2; file_name.resize(file_name_size); - copied = ::GetModuleFileNameEx(process_handle, NULL, file_name.data(), file_name_size); + copied = ::GetModuleFileNameExW(process_handle, NULL, file_name.data(), file_name_size); } while (copied >= file_name_size); file_name.resize(copied); - return std::string(file_name.begin(), file_name.end()); + std::string result; + llvm::convertWideToUTF8(file_name.data(), result); + return result; } std::string @@ -121,9 +124,9 @@ class ProcessWindowsData // Static functions. ProcessSP -ProcessWindowsLive::CreateInstance(lldb::TargetSP target_sp, Listener &listener, const FileSpec *) +ProcessWindowsLive::CreateInstance(lldb::TargetSP target_sp, lldb::ListenerSP listener_sp, const FileSpec *) { - return ProcessSP(new ProcessWindowsLive(target_sp, listener)); + return ProcessSP(new ProcessWindowsLive(target_sp, listener_sp)); } void @@ -142,8 +145,8 @@ ProcessWindowsLive::Initialize() //------------------------------------------------------------------------------ // Constructors and destructors. -ProcessWindowsLive::ProcessWindowsLive(lldb::TargetSP target_sp, Listener &listener) - : lldb_private::ProcessWindows(target_sp, listener) +ProcessWindowsLive::ProcessWindowsLive(lldb::TargetSP target_sp, lldb::ListenerSP listener_sp) + : lldb_private::ProcessWindows(target_sp, listener_sp) { } @@ -189,7 +192,7 @@ ProcessWindowsLive::DisableBreakpointSite(BreakpointSite *bp_site) { WINLOG_IFALL(WINDOWS_LOG_BREAKPOINTS, "DisableBreakpointSite called with bp_site 0x%p " "(id=%d, addr=0x%x)", - bp_site->GetID(), bp_site->GetLoadAddress()); + bp_site, bp_site->GetID(), bp_site->GetLoadAddress()); Error error = DisableSoftwareBreakpoint(bp_site); @@ -554,11 +557,25 @@ ProcessWindowsLive::RefreshStateAfterStop() { case EXCEPTION_SINGLE_STEP: { - stop_info = StopInfo::CreateStopReasonToTrace(*stop_thread); - stop_thread->SetStopInfo(stop_info); - WINLOG_IFANY(WINDOWS_LOG_EXCEPTION | WINDOWS_LOG_STEP, "RefreshStateAfterStop single stepping thread %u", - stop_thread->GetID()); - stop_thread->SetStopInfo(stop_info); + RegisterContextSP register_context = stop_thread->GetRegisterContext(); + const uint64_t pc = register_context->GetPC(); + BreakpointSiteSP site(GetBreakpointSiteList().FindByAddress(pc)); + if (site && site->ValidForThisThread(stop_thread.get())) + { + WINLOG_IFANY(WINDOWS_LOG_BREAKPOINTS | WINDOWS_LOG_EXCEPTION | WINDOWS_LOG_STEP, + "Single-stepped onto a breakpoint in process %I64u at " + "address 0x%I64x with breakpoint site %d", + m_session_data->m_debugger->GetProcess().GetProcessId(), pc, site->GetID()); + stop_info = StopInfo::CreateStopReasonWithBreakpointSiteID(*stop_thread, site->GetID()); + stop_thread->SetStopInfo(stop_info); + } + else + { + WINLOG_IFANY(WINDOWS_LOG_EXCEPTION | WINDOWS_LOG_STEP, + "RefreshStateAfterStop single stepping thread %u", stop_thread->GetID()); + stop_info = StopInfo::CreateStopReasonToTrace(*stop_thread); + stop_thread->SetStopInfo(stop_info); + } return; } @@ -731,6 +748,7 @@ ProcessWindowsLive::GetMemoryRegionInfo(lldb::addr_t vm_addr, MemoryRegionInfo & { Error error; llvm::sys::ScopedLock lock(m_mutex); + info.Clear(); if (!m_session_data) { @@ -738,7 +756,6 @@ ProcessWindowsLive::GetMemoryRegionInfo(lldb::addr_t vm_addr, MemoryRegionInfo & WINERR_IFALL(WINDOWS_LOG_MEMORY, error.AsCString()); return error; } - HostProcess process = m_session_data->m_debugger->GetProcess(); lldb::process_t handle = process.GetNativeProcess().GetSystemHandle(); if (handle == nullptr || handle == LLDB_INVALID_PROCESS) @@ -755,22 +772,67 @@ ProcessWindowsLive::GetMemoryRegionInfo(lldb::addr_t vm_addr, MemoryRegionInfo & SIZE_T result = ::VirtualQueryEx(handle, addr, &mem_info, sizeof(mem_info)); if (result == 0) { - error.SetError(::GetLastError(), eErrorTypeWin32); - WINERR_IFALL(WINDOWS_LOG_MEMORY, - "VirtualQueryEx returned error %u while getting memory region info for address 0x%I64x", - error.GetError(), vm_addr); - return error; + if (::GetLastError() == ERROR_INVALID_PARAMETER) + { + // ERROR_INVALID_PARAMETER is returned if VirtualQueryEx is called with an address + // past the highest accessible address. We should return a range from the vm_addr + // to LLDB_INVALID_ADDRESS + info.GetRange().SetRangeBase(vm_addr); + info.GetRange().SetRangeEnd(LLDB_INVALID_ADDRESS); + info.SetReadable(MemoryRegionInfo::eNo); + info.SetExecutable(MemoryRegionInfo::eNo); + info.SetWritable(MemoryRegionInfo::eNo); + info.SetMapped(MemoryRegionInfo::eNo); + return error; + } + else + { + error.SetError(::GetLastError(), eErrorTypeWin32); + WINERR_IFALL(WINDOWS_LOG_MEMORY, + "VirtualQueryEx returned error %u while getting memory region info for address 0x%I64x", + error.GetError(), vm_addr); + return error; + } + } + + // Protect bits are only valid for MEM_COMMIT regions. + if (mem_info.State == MEM_COMMIT) { + const bool readable = IsPageReadable(mem_info.Protect); + const bool executable = IsPageExecutable(mem_info.Protect); + const bool writable = IsPageWritable(mem_info.Protect); + info.SetReadable(readable ? MemoryRegionInfo::eYes : MemoryRegionInfo::eNo); + info.SetExecutable(executable ? MemoryRegionInfo::eYes : MemoryRegionInfo::eNo); + info.SetWritable(writable ? MemoryRegionInfo::eYes : MemoryRegionInfo::eNo); + } + else + { + info.SetReadable(MemoryRegionInfo::eNo); + info.SetExecutable(MemoryRegionInfo::eNo); + info.SetWritable(MemoryRegionInfo::eNo); + } + + // AllocationBase is defined for MEM_COMMIT and MEM_RESERVE but not MEM_FREE. + if (mem_info.State != MEM_FREE) { + info.GetRange().SetRangeBase(reinterpret_cast<addr_t>(mem_info.AllocationBase)); + info.GetRange().SetRangeEnd(reinterpret_cast<addr_t>(mem_info.BaseAddress) + mem_info.RegionSize); + info.SetMapped(MemoryRegionInfo::eYes); + } + else + { + // In the unmapped case we need to return the distance to the next block of memory. + // VirtualQueryEx nearly does that except that it gives the distance from the start + // of the page containing vm_addr. + SYSTEM_INFO data; + GetSystemInfo(&data); + DWORD page_offset = vm_addr % data.dwPageSize; + info.GetRange().SetRangeBase(vm_addr); + info.GetRange().SetByteSize(mem_info.RegionSize - page_offset); + info.SetMapped(MemoryRegionInfo::eNo); } - const bool readable = IsPageReadable(mem_info.Protect); - const bool executable = IsPageExecutable(mem_info.Protect); - const bool writable = IsPageWritable(mem_info.Protect); - info.SetReadable(readable ? MemoryRegionInfo::eYes : MemoryRegionInfo::eNo); - info.SetExecutable(executable ? MemoryRegionInfo::eYes : MemoryRegionInfo::eNo); - info.SetWritable(writable ? MemoryRegionInfo::eYes : MemoryRegionInfo::eNo); error.SetError(::GetLastError(), eErrorTypeWin32); WINLOGV_IFALL(WINDOWS_LOG_MEMORY, "Memory region info for address 0x%I64u: readable=%s, executable=%s, writable=%s", - BOOL_STR(readable), BOOL_STR(executable), BOOL_STR(writable)); + BOOL_STR(info.GetReadable()), BOOL_STR(info.GetExecutable()), BOOL_STR(info.GetWritable())); return error; } @@ -803,7 +865,7 @@ ProcessWindowsLive::OnExitProcess(uint32_t exit_code) target->ModulesDidUnload(unloaded_modules, true); } - SetProcessExitStatus(nullptr, GetID(), true, 0, exit_code); + SetProcessExitStatus(GetID(), true, 0, exit_code); SetPrivateState(eStateExited); } diff --git a/source/Plugins/Process/Windows/Live/ProcessWindowsLive.h b/source/Plugins/Process/Windows/Live/ProcessWindowsLive.h index 2429f873c823..657877f529b2 100644 --- a/source/Plugins/Process/Windows/Live/ProcessWindowsLive.h +++ b/source/Plugins/Process/Windows/Live/ProcessWindowsLive.h @@ -43,7 +43,7 @@ public: //------------------------------------------------------------------ static lldb::ProcessSP CreateInstance(lldb::TargetSP target_sp, - lldb_private::Listener &listener, + lldb::ListenerSP listener_sp, const lldb_private::FileSpec *); static void @@ -62,7 +62,7 @@ public: // Constructors and destructors //------------------------------------------------------------------ ProcessWindowsLive(lldb::TargetSP target_sp, - lldb_private::Listener &listener); + lldb::ListenerSP listener_sp); ~ProcessWindowsLive(); diff --git a/source/Plugins/Process/Windows/MiniDump/ProcessWinMiniDump.cpp b/source/Plugins/Process/Windows/MiniDump/ProcessWinMiniDump.cpp index fbc96f085ed4..05839667688f 100644 --- a/source/Plugins/Process/Windows/MiniDump/ProcessWinMiniDump.cpp +++ b/source/Plugins/Process/Windows/MiniDump/ProcessWinMiniDump.cpp @@ -35,137 +35,131 @@ #include "llvm/Support/Format.h" #include "llvm/Support/raw_ostream.h" +#include "Plugins/Process/Windows/Common/NtStructures.h" +#include "Plugins/Process/Windows/Common/ProcessWindowsLog.h" + #include "ExceptionRecord.h" #include "ThreadWinMiniDump.h" using namespace lldb_private; -namespace +// Implementation class for ProcessWinMiniDump encapsulates the Windows-specific +// code, keeping non-portable types out of the header files. +// TODO(amccarth): Determine if we need a mutex for access. Given that this is +// postmortem debugging, I don't think so. +class ProcessWinMiniDump::Impl { +public: + Impl(const FileSpec &core_file, ProcessWinMiniDump *self); + ~Impl(); -// Getting a string out of a mini dump is a chore. You're usually given a -// relative virtual address (RVA), which points to a counted string that's in -// Windows Unicode (UTF-16). This wrapper handles all the redirection and -// returns a UTF-8 copy of the string. -std::string -GetMiniDumpString(const void *base_addr, const RVA rva) -{ - std::string result; - if (!base_addr) + Error + DoLoadCore(); + + bool + UpdateThreadList(ThreadList &old_thread_list, ThreadList &new_thread_list); + + void + RefreshStateAfterStop(); + + size_t + DoReadMemory(lldb::addr_t addr, void *buf, size_t size, Error &error); + + Error + GetMemoryRegionInfo(lldb::addr_t load_addr, lldb_private::MemoryRegionInfo &info); + +private: + // Describes a range of memory captured in the mini dump. + struct Range { - return result; - } - auto md_string = reinterpret_cast<const MINIDUMP_STRING *>(static_cast<const char *>(base_addr) + rva); - auto source_start = reinterpret_cast<const UTF16 *>(md_string->Buffer); - const auto source_length = ::wcslen(md_string->Buffer); - const auto source_end = source_start + source_length; - result.resize(4*source_length); // worst case length - auto result_start = reinterpret_cast<UTF8 *>(&result[0]); - const auto result_end = result_start + result.size(); - ConvertUTF16toUTF8(&source_start, source_end, &result_start, result_end, strictConversion); - const auto result_size = std::distance(reinterpret_cast<UTF8 *>(&result[0]), result_start); - result.resize(result_size); // shrink to actual length - return result; -} + lldb::addr_t start; // virtual address of the beginning of the range + size_t size; // size of the range in bytes + const uint8_t *ptr; // absolute pointer to the first byte of the range + }; -} // anonymous namespace + // If the mini dump has a memory range that contains the desired address, it + // returns true with the details of the range in *range_out. Otherwise, it + // returns false. + bool + FindMemoryRange(lldb::addr_t addr, Range *range_out) const; -// Encapsulates the private data for ProcessWinMiniDump. -// TODO(amccarth): Determine if we need a mutex for access. -class ProcessWinMiniDump::Data -{ -public: - Data(); - ~Data(); + lldb_private::Error + MapMiniDumpIntoMemory(); + lldb_private::ArchSpec + DetermineArchitecture(); + + void + ReadExceptionRecord(); + + void + ReadMiscInfo(); + + void + ReadModuleList(); + + // A thin wrapper around WinAPI's MiniDumpReadDumpStream to avoid redundant + // checks. If there's a failure (e.g., if the requested stream doesn't exist), + // the function returns nullptr and sets *size_out to 0. + void * + FindDumpStream(unsigned stream_number, size_t *size_out) const; + + // Getting a string out of a mini dump is a chore. You're usually given a + // relative virtual address (RVA), which points to a counted string that's in + // Windows Unicode (UTF-16). This wrapper handles all the redirection and + // returns a UTF-8 copy of the string. + std::string + GetMiniDumpString(RVA rva) const; + + ProcessWinMiniDump *m_self; // non-owning back pointer FileSpec m_core_file; HANDLE m_dump_file; // handle to the open minidump file HANDLE m_mapping; // handle to the file mapping for the minidump file void * m_base_addr; // base memory address of the minidump std::shared_ptr<ExceptionRecord> m_exception_sp; + bool m_is_wow64; // minidump is of a 32-bit process captured with a 64-bit debugger }; -ConstString -ProcessWinMiniDump::GetPluginNameStatic() +ProcessWinMiniDump::Impl::Impl(const FileSpec &core_file, ProcessWinMiniDump *self) + : m_self(self), + m_core_file(core_file), + m_dump_file(INVALID_HANDLE_VALUE), + m_mapping(NULL), + m_base_addr(nullptr), + m_exception_sp(), + m_is_wow64(false) { - static ConstString g_name("win-minidump"); - return g_name; } -const char * -ProcessWinMiniDump::GetPluginDescriptionStatic() +ProcessWinMiniDump::Impl::~Impl() { - return "Windows minidump plug-in."; -} - -void -ProcessWinMiniDump::Terminate() -{ - PluginManager::UnregisterPlugin(ProcessWinMiniDump::CreateInstance); -} - - -lldb::ProcessSP -ProcessWinMiniDump::CreateInstance(lldb::TargetSP target_sp, Listener &listener, const FileSpec *crash_file) -{ - lldb::ProcessSP process_sp; - if (crash_file) + if (m_base_addr) { - process_sp.reset(new ProcessWinMiniDump(target_sp, listener, *crash_file)); + ::UnmapViewOfFile(m_base_addr); + m_base_addr = nullptr; + } + if (m_mapping) + { + ::CloseHandle(m_mapping); + m_mapping = NULL; + } + if (m_dump_file != INVALID_HANDLE_VALUE) + { + ::CloseHandle(m_dump_file); + m_dump_file = INVALID_HANDLE_VALUE; } - return process_sp; -} - -bool -ProcessWinMiniDump::CanDebug(lldb::TargetSP target_sp, bool plugin_specified_by_name) -{ - // TODO(amccarth): Eventually, this needs some actual logic. - return true; -} - -ProcessWinMiniDump::ProcessWinMiniDump(lldb::TargetSP target_sp, Listener &listener, - const FileSpec &core_file) : - ProcessWindows(target_sp, listener), - m_data_up(new Data) -{ - m_data_up->m_core_file = core_file; -} - -ProcessWinMiniDump::~ProcessWinMiniDump() -{ - 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(); -} - -ConstString -ProcessWinMiniDump::GetPluginName() -{ - return GetPluginNameStatic(); -} - -uint32_t -ProcessWinMiniDump::GetPluginVersion() -{ - return 1; } - Error -ProcessWinMiniDump::DoLoadCore() +ProcessWinMiniDump::Impl::DoLoadCore() { - Error error; - - error = MapMiniDumpIntoMemory(m_data_up->m_core_file.GetCString()); + Error error = MapMiniDumpIntoMemory(); if (error.Fail()) { return error; } - GetTarget().SetArchitecture(DetermineArchitecture()); + m_self->GetTarget().SetArchitecture(DetermineArchitecture()); ReadMiscInfo(); // notably for process ID ReadModuleList(); ReadExceptionRecord(); @@ -174,16 +168,8 @@ ProcessWinMiniDump::DoLoadCore() } -DynamicLoader * -ProcessWinMiniDump::GetDynamicLoader() -{ - if (m_dyld_ap.get() == NULL) - m_dyld_ap.reset (DynamicLoader::FindPlugin(this, DynamicLoaderWindowsDYLD::GetPluginNameStatic().GetCString())); - return m_dyld_ap.get(); -} - bool -ProcessWinMiniDump::UpdateThreadList(ThreadList &old_thread_list, ThreadList &new_thread_list) +ProcessWinMiniDump::Impl::UpdateThreadList(ThreadList &old_thread_list, ThreadList &new_thread_list) { size_t size = 0; auto thread_list_ptr = static_cast<const MINIDUMP_THREAD_LIST *>(FindDumpStream(ThreadListStream, &size)); @@ -192,10 +178,50 @@ ProcessWinMiniDump::UpdateThreadList(ThreadList &old_thread_list, ThreadList &ne const ULONG32 thread_count = thread_list_ptr->NumberOfThreads; for (ULONG32 i = 0; i < thread_count; ++i) { const auto &mini_dump_thread = thread_list_ptr->Threads[i]; - auto thread_sp = std::make_shared<ThreadWinMiniDump>(*this, mini_dump_thread.ThreadId); + auto thread_sp = std::make_shared<ThreadWinMiniDump>(*m_self, mini_dump_thread.ThreadId); if (mini_dump_thread.ThreadContext.DataSize >= sizeof(CONTEXT)) { - const CONTEXT *context = reinterpret_cast<const CONTEXT *>(static_cast<const char *>(m_data_up->m_base_addr) + mini_dump_thread.ThreadContext.Rva); + const CONTEXT *context = reinterpret_cast<const CONTEXT *>(static_cast<const char *>(m_base_addr) + + mini_dump_thread.ThreadContext.Rva); + + if (m_is_wow64) + { + // On Windows, a 32-bit process can run on a 64-bit machine under WOW64. + // If the minidump was captured with a 64-bit debugger, then the CONTEXT + // we just grabbed from the mini_dump_thread is the one for the 64-bit + // "native" process rather than the 32-bit "guest" process we care about. + // In this case, we can get the 32-bit CONTEXT from the TEB (Thread + // Environment Block) of the 64-bit process. + Error error; + TEB64 wow64teb = {0}; + m_self->ReadMemory(mini_dump_thread.Teb, &wow64teb, sizeof(wow64teb), error); + if (error.Success()) + { + // Slot 1 of the thread-local storage in the 64-bit TEB points to a structure + // that includes the 32-bit CONTEXT (after a ULONG). + // See: https://msdn.microsoft.com/en-us/library/ms681670.aspx + const size_t addr = wow64teb.TlsSlots[1]; + Range range = {0}; + if (FindMemoryRange(addr, &range)) + { + lldbassert(range.start <= addr); + const size_t offset = addr - range.start + sizeof(ULONG); + if (offset < range.size) + { + const size_t overlap = range.size - offset; + if (overlap >= sizeof(CONTEXT)) + { + context = reinterpret_cast<const CONTEXT *>(range.ptr + offset); + } + } + } + } + + // NOTE: We don't currently use the TEB for anything else. If we need it in + // the future, the 32-bit TEB is located according to the address stored in the + // first slot of the 64-bit TEB (wow64teb.Reserved1[0]). + } + thread_sp->SetContext(context); } new_thread_list.AddThread(thread_sp); @@ -206,54 +232,24 @@ ProcessWinMiniDump::UpdateThreadList(ThreadList &old_thread_list, ThreadList &ne } void -ProcessWinMiniDump::RefreshStateAfterStop() +ProcessWinMiniDump::Impl::RefreshStateAfterStop() { - if (!m_data_up) return; - if (!m_data_up->m_exception_sp) return; + if (!m_exception_sp) + return; - auto active_exception = m_data_up->m_exception_sp; + auto active_exception = m_exception_sp; std::string desc; llvm::raw_string_ostream desc_stream(desc); - desc_stream << "Exception " - << llvm::format_hex(active_exception->GetExceptionCode(), 8) - << " encountered at address " - << llvm::format_hex(active_exception->GetExceptionAddress(), 8); - m_thread_list.SetSelectedThreadByID(active_exception->GetThreadID()); - auto stop_thread = m_thread_list.GetSelectedThread(); + desc_stream << "Exception " << llvm::format_hex(active_exception->GetExceptionCode(), 8) + << " encountered at address " << llvm::format_hex(active_exception->GetExceptionAddress(), 8); + m_self->m_thread_list.SetSelectedThreadByID(active_exception->GetThreadID()); + auto stop_thread = m_self->m_thread_list.GetSelectedThread(); auto stop_info = StopInfo::CreateStopReasonWithException(*stop_thread, desc_stream.str().c_str()); stop_thread->SetStopInfo(stop_info); } -Error -ProcessWinMiniDump::DoDestroy() -{ - return Error(); -} - -bool -ProcessWinMiniDump::IsAlive() -{ - return true; -} - -bool -ProcessWinMiniDump::WarnBeforeDetach () const -{ - // Since this is post-mortem debugging, there's no need to warn the user - // that quitting the debugger will terminate the process. - return false; -} - size_t -ProcessWinMiniDump::ReadMemory(lldb::addr_t addr, void *buf, size_t size, Error &error) -{ - // Don't allow the caching that lldb_private::Process::ReadMemory does - // since we have it all cached our our dump file anyway. - return DoReadMemory(addr, buf, size, error); -} - -size_t -ProcessWinMiniDump::DoReadMemory(lldb::addr_t addr, void *buf, size_t size, Error &error) +ProcessWinMiniDump::Impl::DoReadMemory(lldb::addr_t addr, void *buf, size_t size, Error &error) { // I don't have a sense of how frequently this is called or how many memory // ranges a mini dump typically has, so I'm not sure if searching for the @@ -277,10 +273,11 @@ ProcessWinMiniDump::DoReadMemory(lldb::addr_t addr, void *buf, size_t size, Erro } Error -ProcessWinMiniDump::GetMemoryRegionInfo(lldb::addr_t load_addr, lldb_private::MemoryRegionInfo &info) +ProcessWinMiniDump::Impl::GetMemoryRegionInfo(lldb::addr_t load_addr, lldb_private::MemoryRegionInfo &info) { Error error; size_t size; + info.Clear(); const auto list = reinterpret_cast<const MINIDUMP_MEMORY_INFO_LIST *>(FindDumpStream(MemoryInfoListStream, &size)); if (list == nullptr || size < sizeof(MINIDUMP_MEMORY_INFO_LIST)) { @@ -300,6 +297,8 @@ ProcessWinMiniDump::GetMemoryRegionInfo(lldb::addr_t load_addr, lldb_private::Me return error; } + const MINIDUMP_MEMORY_INFO *next_entry = nullptr; + for (int i = 0; i < list->NumberOfEntries; ++i) { const auto entry = reinterpret_cast<const MINIDUMP_MEMORY_INFO *>(reinterpret_cast<const char *>(list) + @@ -308,80 +307,46 @@ ProcessWinMiniDump::GetMemoryRegionInfo(lldb::addr_t load_addr, lldb_private::Me const auto tail = head + entry->RegionSize; if (head <= load_addr && load_addr < tail) { + info.GetRange().SetRangeBase((entry->State != MEM_FREE) ? head : load_addr); + info.GetRange().SetRangeEnd(tail); info.SetReadable(IsPageReadable(entry->Protect) ? MemoryRegionInfo::eYes : MemoryRegionInfo::eNo); info.SetWritable(IsPageWritable(entry->Protect) ? MemoryRegionInfo::eYes : MemoryRegionInfo::eNo); info.SetExecutable(IsPageExecutable(entry->Protect) ? MemoryRegionInfo::eYes : MemoryRegionInfo::eNo); + info.SetMapped((entry->State != MEM_FREE) ? MemoryRegionInfo::eYes : MemoryRegionInfo::eNo); return error; } + else if (head > load_addr && (next_entry == nullptr || head < next_entry->BaseAddress) ) + { + // In case there is no region containing load_addr keep track of the nearest region + // after load_addr so we can return the distance to it. + next_entry = entry; + } } + + // No containing region found. Create an unmapped region that extends to the next region + // or LLDB_INVALID_ADDRESS + info.GetRange().SetRangeBase(load_addr); + info.GetRange().SetRangeEnd((next_entry != nullptr)?next_entry->BaseAddress:LLDB_INVALID_ADDRESS); + info.SetReadable(MemoryRegionInfo::eNo); + info.SetWritable(MemoryRegionInfo::eNo); + info.SetExecutable(MemoryRegionInfo::eNo); + info.SetMapped(MemoryRegionInfo::eNo); + // Note that the memory info list doesn't seem to contain ranges in kernel space, // so if you're walking a stack that has kernel frames, the stack may appear // truncated. - error.SetErrorString("address is not in a known range"); return error; } -void -ProcessWinMiniDump::Clear() -{ - m_thread_list.Clear(); -} - -void -ProcessWinMiniDump::Initialize() -{ - static std::once_flag g_once_flag; - - std::call_once(g_once_flag, []() - { - PluginManager::RegisterPlugin(GetPluginNameStatic(), - GetPluginDescriptionStatic(), - CreateInstance); - }); -} - -ArchSpec -ProcessWinMiniDump::GetArchitecture() -{ - // TODO - return ArchSpec(); -} - - -ProcessWinMiniDump::Data::Data() : - m_dump_file(INVALID_HANDLE_VALUE), - m_mapping(NULL), - m_base_addr(nullptr) -{ -} - -ProcessWinMiniDump::Data::~Data() -{ - if (m_base_addr) - { - ::UnmapViewOfFile(m_base_addr); - m_base_addr = nullptr; - } - if (m_mapping) - { - ::CloseHandle(m_mapping); - m_mapping = NULL; - } - if (m_dump_file != INVALID_HANDLE_VALUE) - { - ::CloseHandle(m_dump_file); - m_dump_file = INVALID_HANDLE_VALUE; - } -} - bool -ProcessWinMiniDump::FindMemoryRange(lldb::addr_t addr, Range *range_out) const +ProcessWinMiniDump::Impl::FindMemoryRange(lldb::addr_t addr, Range *range_out) const { size_t stream_size = 0; auto mem_list_stream = static_cast<const MINIDUMP_MEMORY_LIST *>(FindDumpStream(MemoryListStream, &stream_size)); if (mem_list_stream) { - for (ULONG32 i = 0; i < mem_list_stream->NumberOfMemoryRanges; ++i) { + for (ULONG32 i = 0; i < mem_list_stream->NumberOfMemoryRanges; ++i) + { const MINIDUMP_MEMORY_DESCRIPTOR &mem_desc = mem_list_stream->MemoryRanges[i]; const MINIDUMP_LOCATION_DESCRIPTOR &loc_desc = mem_desc.Memory; const lldb::addr_t range_start = mem_desc.StartOfMemoryRange; @@ -390,7 +355,7 @@ ProcessWinMiniDump::FindMemoryRange(lldb::addr_t addr, Range *range_out) const { range_out->start = range_start; range_out->size = range_size; - range_out->ptr = reinterpret_cast<const uint8_t *>(m_data_up->m_base_addr) + loc_desc.Rva; + range_out->ptr = reinterpret_cast<const uint8_t *>(m_base_addr) + loc_desc.Rva; return true; } } @@ -411,7 +376,7 @@ ProcessWinMiniDump::FindMemoryRange(lldb::addr_t addr, Range *range_out) const { range_out->start = range_start; range_out->size = range_size; - range_out->ptr = reinterpret_cast<const uint8_t *>(m_data_up->m_base_addr) + base_rva; + range_out->ptr = reinterpret_cast<const uint8_t *>(m_base_addr) + base_rva; return true; } base_rva += range_size; @@ -421,31 +386,34 @@ ProcessWinMiniDump::FindMemoryRange(lldb::addr_t addr, Range *range_out) const return false; } - Error -ProcessWinMiniDump::MapMiniDumpIntoMemory(const char *file) +ProcessWinMiniDump::Impl::MapMiniDumpIntoMemory() { Error error; - - m_data_up->m_dump_file = ::CreateFile(file, GENERIC_READ, FILE_SHARE_READ, - NULL, OPEN_EXISTING, - FILE_ATTRIBUTE_NORMAL, NULL); - if (m_data_up->m_dump_file == INVALID_HANDLE_VALUE) + const char *file = m_core_file.GetCString(); + std::wstring wfile; + if (!llvm::ConvertUTF8toWide(file, wfile)) + { + error.SetErrorString("Error converting path to UTF-16"); + return error; + } + m_dump_file = + ::CreateFileW(wfile.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); + if (m_dump_file == INVALID_HANDLE_VALUE) { error.SetError(::GetLastError(), lldb::eErrorTypeWin32); return error; } - m_data_up->m_mapping = ::CreateFileMapping(m_data_up->m_dump_file, NULL, - PAGE_READONLY, 0, 0, NULL); - if (m_data_up->m_mapping == NULL) + m_mapping = ::CreateFileMappingW(m_dump_file, NULL, PAGE_READONLY, 0, 0, NULL); + if (m_mapping == NULL) { error.SetError(::GetLastError(), lldb::eErrorTypeWin32); return error; } - m_data_up->m_base_addr = ::MapViewOfFile(m_data_up->m_mapping, FILE_MAP_READ, 0, 0, 0); - if (m_data_up->m_base_addr == NULL) + m_base_addr = ::MapViewOfFile(m_mapping, FILE_MAP_READ, 0, 0, 0); + if (m_base_addr == nullptr) { error.SetError(::GetLastError(), lldb::eErrorTypeWin32); return error; @@ -454,9 +422,8 @@ ProcessWinMiniDump::MapMiniDumpIntoMemory(const char *file) return error; } - ArchSpec -ProcessWinMiniDump::DetermineArchitecture() +ProcessWinMiniDump::Impl::DetermineArchitecture() { size_t size = 0; auto system_info_ptr = static_cast<const MINIDUMP_SYSTEM_INFO *>(FindDumpStream(SystemInfoStream, &size)); @@ -465,9 +432,17 @@ ProcessWinMiniDump::DetermineArchitecture() switch (system_info_ptr->ProcessorArchitecture) { case PROCESSOR_ARCHITECTURE_INTEL: - return ArchSpec(eArchTypeCOFF, IMAGE_FILE_MACHINE_I386, LLDB_INVALID_CPUTYPE); + if (system_info_ptr->ProcessorLevel == 6) + { + return ArchSpec("i686-pc-windows"); + } + else + { + return ArchSpec("i386-pc-windows"); + } + break; case PROCESSOR_ARCHITECTURE_AMD64: - return ArchSpec(eArchTypeCOFF, IMAGE_FILE_MACHINE_AMD64, LLDB_INVALID_CPUTYPE); + return ArchSpec("x86_64-pc-windows"); default: break; } @@ -477,18 +452,24 @@ ProcessWinMiniDump::DetermineArchitecture() } void -ProcessWinMiniDump::ReadExceptionRecord() +ProcessWinMiniDump::Impl::ReadExceptionRecord() { size_t size = 0; auto exception_stream_ptr = static_cast<MINIDUMP_EXCEPTION_STREAM*>(FindDumpStream(ExceptionStream, &size)); if (exception_stream_ptr) { - m_data_up->m_exception_sp.reset(new ExceptionRecord(exception_stream_ptr->ExceptionRecord, exception_stream_ptr->ThreadId)); + m_exception_sp.reset( + new ExceptionRecord(exception_stream_ptr->ExceptionRecord, exception_stream_ptr->ThreadId)); + } + else + { + WINLOG_IFALL(WINDOWS_LOG_PROCESS, "Minidump has no exception record."); + // TODO: See if we can recover the exception from the TEB. } } void -ProcessWinMiniDump::ReadMiscInfo() +ProcessWinMiniDump::Impl::ReadMiscInfo() { size_t size = 0; const auto misc_info_ptr = static_cast<MINIDUMP_MISC_INFO*>(FindDumpStream(MiscInfoStream, &size)); @@ -498,12 +479,12 @@ ProcessWinMiniDump::ReadMiscInfo() if ((misc_info_ptr->Flags1 & MINIDUMP_MISC1_PROCESS_ID) != 0) { // This misc info record has the process ID. - SetID(misc_info_ptr->ProcessId); + m_self->SetID(misc_info_ptr->ProcessId); } } void -ProcessWinMiniDump::ReadModuleList() +ProcessWinMiniDump::Impl::ReadModuleList() { size_t size = 0; auto module_list_ptr = static_cast<MINIDUMP_MODULE_LIST*>(FindDumpStream(ModuleListStream, &size)); @@ -515,36 +496,215 @@ ProcessWinMiniDump::ReadModuleList() for (ULONG32 i = 0; i < module_list_ptr->NumberOfModules; ++i) { const auto &module = module_list_ptr->Modules[i]; - const auto file_name = GetMiniDumpString(m_data_up->m_base_addr, module.ModuleNameRva); - ModuleSpec module_spec = FileSpec(file_name, true); + const auto file_name = GetMiniDumpString(module.ModuleNameRva); + const auto file_spec = FileSpec(file_name, true); + if (FileSpec::Compare(file_spec, FileSpec("wow64.dll", false), false) == 0) + { + WINLOG_IFALL(WINDOWS_LOG_PROCESS, "Minidump is for a WOW64 process."); + m_is_wow64 = true; + } + ModuleSpec module_spec = file_spec; - lldb::ModuleSP module_sp = GetTarget().GetSharedModule(module_spec); + lldb::ModuleSP module_sp = m_self->GetTarget().GetSharedModule(module_spec); if (!module_sp) { continue; } bool load_addr_changed = false; - module_sp->SetLoadAddress(GetTarget(), module.BaseOfImage, false, load_addr_changed); + module_sp->SetLoadAddress(m_self->GetTarget(), module.BaseOfImage, false, load_addr_changed); } } void * -ProcessWinMiniDump::FindDumpStream(unsigned stream_number, size_t *size_out) const +ProcessWinMiniDump::Impl::FindDumpStream(unsigned stream_number, size_t *size_out) const { void *stream = nullptr; *size_out = 0; - assert(m_data_up != nullptr); - assert(m_data_up->m_base_addr != 0); - MINIDUMP_DIRECTORY *dir = nullptr; - if (::MiniDumpReadDumpStream(m_data_up->m_base_addr, stream_number, &dir, nullptr, nullptr) && - dir != nullptr && dir->Location.DataSize > 0) + if (::MiniDumpReadDumpStream(m_base_addr, stream_number, &dir, nullptr, nullptr) && dir != nullptr && + dir->Location.DataSize > 0) { assert(dir->StreamType == stream_number); *size_out = dir->Location.DataSize; - stream = static_cast<void*>(static_cast<char*>(m_data_up->m_base_addr) + dir->Location.Rva); + stream = static_cast<void *>(static_cast<char *>(m_base_addr) + dir->Location.Rva); } return stream; } + +std::string +ProcessWinMiniDump::Impl::GetMiniDumpString(RVA rva) const +{ + std::string result; + if (!m_base_addr) + { + return result; + } + auto md_string = reinterpret_cast<const MINIDUMP_STRING *>(static_cast<const char *>(m_base_addr) + rva); + auto source_start = reinterpret_cast<const UTF16 *>(md_string->Buffer); + const auto source_length = ::wcslen(md_string->Buffer); + const auto source_end = source_start + source_length; + result.resize(UNI_MAX_UTF8_BYTES_PER_CODE_POINT * source_length); // worst case length + auto result_start = reinterpret_cast<UTF8 *>(&result[0]); + const auto result_end = result_start + result.size(); + ConvertUTF16toUTF8(&source_start, source_end, &result_start, result_end, strictConversion); + const auto result_size = std::distance(reinterpret_cast<UTF8 *>(&result[0]), result_start); + result.resize(result_size); // shrink to actual length + return result; +} + +ConstString +ProcessWinMiniDump::GetPluginNameStatic() +{ + static ConstString g_name("win-minidump"); + return g_name; +} + +const char * +ProcessWinMiniDump::GetPluginDescriptionStatic() +{ + return "Windows minidump plug-in."; +} + +void +ProcessWinMiniDump::Terminate() +{ + PluginManager::UnregisterPlugin(ProcessWinMiniDump::CreateInstance); +} + +lldb::ProcessSP +ProcessWinMiniDump::CreateInstance(lldb::TargetSP target_sp, lldb::ListenerSP listener_sp, const FileSpec *crash_file) +{ + lldb::ProcessSP process_sp; + if (crash_file) + { + process_sp.reset(new ProcessWinMiniDump(target_sp, listener_sp, *crash_file)); + } + return process_sp; +} + +bool +ProcessWinMiniDump::CanDebug(lldb::TargetSP target_sp, bool plugin_specified_by_name) +{ + // TODO(amccarth): Eventually, this needs some actual logic. + return true; +} + +ProcessWinMiniDump::ProcessWinMiniDump(lldb::TargetSP target_sp, lldb::ListenerSP listener_sp, const FileSpec &core_file) + : ProcessWindows(target_sp, listener_sp), m_impl_up(new Impl(core_file, this)) +{ +} + +ProcessWinMiniDump::~ProcessWinMiniDump() +{ + 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(); +} + +ConstString +ProcessWinMiniDump::GetPluginName() +{ + return GetPluginNameStatic(); +} + +uint32_t +ProcessWinMiniDump::GetPluginVersion() +{ + return 1; +} + +Error +ProcessWinMiniDump::DoLoadCore() +{ + return m_impl_up->DoLoadCore(); +} + +DynamicLoader * +ProcessWinMiniDump::GetDynamicLoader() +{ + if (m_dyld_ap.get() == NULL) + m_dyld_ap.reset(DynamicLoader::FindPlugin(this, DynamicLoaderWindowsDYLD::GetPluginNameStatic().GetCString())); + return m_dyld_ap.get(); +} + +bool +ProcessWinMiniDump::UpdateThreadList(ThreadList &old_thread_list, ThreadList &new_thread_list) +{ + return m_impl_up->UpdateThreadList(old_thread_list, new_thread_list); +} + +void +ProcessWinMiniDump::RefreshStateAfterStop() +{ + if (!m_impl_up) + return; + return m_impl_up->RefreshStateAfterStop(); +} + +Error +ProcessWinMiniDump::DoDestroy() +{ + return Error(); +} + +bool +ProcessWinMiniDump::IsAlive() +{ + return true; +} + +bool +ProcessWinMiniDump::WarnBeforeDetach() const +{ + // Since this is post-mortem debugging, there's no need to warn the user + // that quitting the debugger will terminate the process. + return false; +} + +size_t +ProcessWinMiniDump::ReadMemory(lldb::addr_t addr, void *buf, size_t size, Error &error) +{ + // Don't allow the caching that lldb_private::Process::ReadMemory does + // since we have it all cached our our dump file anyway. + return DoReadMemory(addr, buf, size, error); +} + +size_t +ProcessWinMiniDump::DoReadMemory(lldb::addr_t addr, void *buf, size_t size, Error &error) +{ + return m_impl_up->DoReadMemory(addr, buf, size, error); +} + +Error +ProcessWinMiniDump::GetMemoryRegionInfo(lldb::addr_t load_addr, lldb_private::MemoryRegionInfo &info) +{ + return m_impl_up->GetMemoryRegionInfo(load_addr, info); +} + +void +ProcessWinMiniDump::Clear() +{ + m_thread_list.Clear(); +} + +void +ProcessWinMiniDump::Initialize() +{ + static std::once_flag g_once_flag; + + std::call_once(g_once_flag, []() { + PluginManager::RegisterPlugin(GetPluginNameStatic(), GetPluginDescriptionStatic(), CreateInstance); + }); +} + +ArchSpec +ProcessWinMiniDump::GetArchitecture() +{ + // TODO + return ArchSpec(); +} diff --git a/source/Plugins/Process/Windows/MiniDump/ProcessWinMiniDump.h b/source/Plugins/Process/Windows/MiniDump/ProcessWinMiniDump.h index 12864be37127..3e1ac4bffbe3 100644 --- a/source/Plugins/Process/Windows/MiniDump/ProcessWinMiniDump.h +++ b/source/Plugins/Process/Windows/MiniDump/ProcessWinMiniDump.h @@ -26,7 +26,7 @@ class ProcessWinMiniDump : public lldb_private::ProcessWindows public: static lldb::ProcessSP CreateInstance (lldb::TargetSP target_sp, - lldb_private::Listener &listener, + lldb::ListenerSP listener_sp, const lldb_private::FileSpec *crash_file_path); static void @@ -42,7 +42,7 @@ class ProcessWinMiniDump : public lldb_private::ProcessWindows GetPluginDescriptionStatic(); ProcessWinMiniDump(lldb::TargetSP target_sp, - lldb_private::Listener &listener, + lldb::ListenerSP listener_sp, const lldb_private::FileSpec &core_file); virtual @@ -96,45 +96,9 @@ class ProcessWinMiniDump : public lldb_private::ProcessWindows lldb_private::ThreadList &new_thread_list) override; private: - // Describes a range of memory captured in the mini dump. - struct Range { - lldb::addr_t start; // virtual address of the beginning of the range - size_t size; // size of the range in bytes - const uint8_t *ptr; // absolute pointer to the first byte of the range - }; - - // If the mini dump has a memory range that contains the desired address, it - // returns true with the details of the range in *range_out. Otherwise, it - // returns false. - bool - FindMemoryRange(lldb::addr_t addr, Range *range_out) const; - - lldb_private::Error - MapMiniDumpIntoMemory(const char *file); - - lldb_private::ArchSpec - DetermineArchitecture(); - - void - ReadExceptionRecord(); - - void - ReadMiscInfo(); - - void - ReadModuleList(); - - // A thin wrapper around WinAPI's MiniDumpReadDumpStream to avoid redundant - // checks. If there's a failure (e.g., if the requested stream doesn't exist), - // the function returns nullptr and sets *size_out to 0. - void * - FindDumpStream(unsigned stream_number, size_t *size_out) const; - - // Isolate the data to keep Windows-specific types out of this header. Can't - // use the typical pimpl idiom because the implementation of this class also - // needs access to public and protected members of the base class. - class Data; - std::unique_ptr<Data> m_data_up; + // Keep Windows-specific types out of this header. + class Impl; + std::unique_ptr<Impl> m_impl_up; }; #endif // liblldb_ProcessWinMiniDump_h_ |