Zachary Turner | 119767d | 2014-11-17 17:46:43 +0000 | [diff] [blame] | 1 | //===-- TargetThreadWindows.cpp----------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
Adrian McCarthy | 3ae7492 | 2015-06-01 21:51:50 +0000 | [diff] [blame] | 10 | #include "lldb/Core/Log.h" |
| 11 | #include "lldb/Core/Logging.h" |
| 12 | #include "lldb/Core/State.h" |
Zachary Turner | 17f383d | 2014-11-20 22:47:32 +0000 | [diff] [blame] | 13 | #include "lldb/Host/HostInfo.h" |
Zachary Turner | 119767d | 2014-11-17 17:46:43 +0000 | [diff] [blame] | 14 | #include "lldb/Host/HostNativeThreadBase.h" |
| 15 | #include "lldb/Host/windows/HostThreadWindows.h" |
| 16 | #include "lldb/Host/windows/windows.h" |
Zachary Turner | 17f383d | 2014-11-20 22:47:32 +0000 | [diff] [blame] | 17 | #include "lldb/Target/RegisterContext.h" |
| 18 | |
Zachary Turner | 17f383d | 2014-11-20 22:47:32 +0000 | [diff] [blame] | 19 | #include "ProcessWindows.h" |
Adrian McCarthy | 3ae7492 | 2015-06-01 21:51:50 +0000 | [diff] [blame] | 20 | #include "ProcessWindowsLog.h" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 21 | #include "TargetThreadWindows.h" |
Zachary Turner | 17f383d | 2014-11-20 22:47:32 +0000 | [diff] [blame] | 22 | #include "UnwindLLDB.h" |
Zachary Turner | 119767d | 2014-11-17 17:46:43 +0000 | [diff] [blame] | 23 | |
Adrian McCarthy | 4ad5def | 2016-11-23 16:26:37 +0000 | [diff] [blame] | 24 | #if defined(_WIN64) |
Hafiz Abid Qadeer | 65abfb1 | 2016-11-29 09:31:57 +0000 | [diff] [blame^] | 25 | #include "x64/RegisterContextWindows_x64.h" |
Adrian McCarthy | 4ad5def | 2016-11-23 16:26:37 +0000 | [diff] [blame] | 26 | #else |
| 27 | #include "x86/RegisterContextWindows_x86.h" |
| 28 | #endif |
| 29 | |
Zachary Turner | 119767d | 2014-11-17 17:46:43 +0000 | [diff] [blame] | 30 | using namespace lldb; |
| 31 | using namespace lldb_private; |
| 32 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 33 | TargetThreadWindows::TargetThreadWindows(ProcessWindows &process, |
| 34 | const HostThread &thread) |
| 35 | : Thread(process, thread.GetNativeThread().GetThreadId()), |
| 36 | m_host_thread(thread) {} |
| 37 | |
| 38 | TargetThreadWindows::~TargetThreadWindows() { DestroyThread(); } |
| 39 | |
| 40 | void TargetThreadWindows::RefreshStateAfterStop() { |
| 41 | ::SuspendThread(m_host_thread.GetNativeThread().GetSystemHandle()); |
| 42 | SetState(eStateStopped); |
| 43 | GetRegisterContext()->InvalidateIfNeeded(false); |
Zachary Turner | 119767d | 2014-11-17 17:46:43 +0000 | [diff] [blame] | 44 | } |
| 45 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 46 | void TargetThreadWindows::WillResume(lldb::StateType resume_state) {} |
| 47 | |
| 48 | void TargetThreadWindows::DidStop() {} |
| 49 | |
Adrian McCarthy | 4ad5def | 2016-11-23 16:26:37 +0000 | [diff] [blame] | 50 | RegisterContextSP TargetThreadWindows::GetRegisterContext() { |
| 51 | if (!m_reg_context_sp) |
| 52 | m_reg_context_sp = CreateRegisterContextForFrameIndex(0); |
| 53 | |
| 54 | return m_reg_context_sp; |
| 55 | } |
| 56 | |
| 57 | RegisterContextSP |
| 58 | TargetThreadWindows::CreateRegisterContextForFrame(StackFrame *frame) { |
| 59 | return CreateRegisterContextForFrameIndex(frame->GetConcreteFrameIndex()); |
| 60 | } |
| 61 | |
| 62 | RegisterContextSP |
| 63 | TargetThreadWindows::CreateRegisterContextForFrameIndex(uint32_t idx) { |
| 64 | if (!m_reg_context_sp) { |
| 65 | ArchSpec arch = HostInfo::GetArchitecture(); |
| 66 | switch (arch.GetMachine()) { |
| 67 | case llvm::Triple::x86: |
| 68 | #if defined(_WIN64) |
| 69 | // FIXME: This is a Wow64 process, create a RegisterContextWindows_Wow64 |
| 70 | #else |
| 71 | m_reg_context_sp.reset(new RegisterContextWindows_x86(*this, idx)); |
| 72 | #endif |
| 73 | break; |
| 74 | case llvm::Triple::x86_64: |
| 75 | #if defined(_WIN64) |
| 76 | m_reg_context_sp.reset(new RegisterContextWindows_x64(*this, idx)); |
| 77 | #else |
| 78 | // LLDB is 32-bit, but the target process is 64-bit. We probably can't debug |
| 79 | // this. |
| 80 | #endif |
| 81 | default: |
| 82 | break; |
| 83 | } |
| 84 | } |
| 85 | return m_reg_context_sp; |
| 86 | } |
| 87 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 88 | bool TargetThreadWindows::CalculateStopInfo() { |
| 89 | SetStopInfo(m_stop_info_sp); |
| 90 | return true; |
Zachary Turner | 119767d | 2014-11-17 17:46:43 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 93 | Unwind *TargetThreadWindows::GetUnwinder() { |
| 94 | // FIXME: Implement an unwinder based on the Windows unwinder exposed through |
| 95 | // DIA SDK. |
| 96 | if (m_unwinder_ap.get() == NULL) |
| 97 | m_unwinder_ap.reset(new UnwindLLDB(*this)); |
| 98 | return m_unwinder_ap.get(); |
Zachary Turner | 119767d | 2014-11-17 17:46:43 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 101 | bool TargetThreadWindows::DoResume() { |
| 102 | StateType resume_state = GetTemporaryResumeState(); |
| 103 | StateType current_state = GetState(); |
| 104 | if (resume_state == current_state) |
Zachary Turner | 17f383d | 2014-11-20 22:47:32 +0000 | [diff] [blame] | 105 | return true; |
Zachary Turner | 17f383d | 2014-11-20 22:47:32 +0000 | [diff] [blame] | 106 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 107 | if (resume_state == eStateStepping) { |
| 108 | uint32_t flags_index = |
| 109 | GetRegisterContext()->ConvertRegisterKindToRegisterNumber( |
| 110 | eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FLAGS); |
| 111 | uint64_t flags_value = |
| 112 | GetRegisterContext()->ReadRegisterAsUnsigned(flags_index, 0); |
| 113 | flags_value |= 0x100; // Set the trap flag on the CPU |
| 114 | GetRegisterContext()->WriteRegisterFromUnsigned(flags_index, flags_value); |
| 115 | } |
Zachary Turner | 119767d | 2014-11-17 17:46:43 +0000 | [diff] [blame] | 116 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 117 | if (resume_state == eStateStepping || resume_state == eStateRunning) { |
| 118 | DWORD previous_suspend_count = 0; |
| 119 | HANDLE thread_handle = m_host_thread.GetNativeThread().GetSystemHandle(); |
| 120 | do { |
| 121 | previous_suspend_count = ::ResumeThread(thread_handle); |
| 122 | } while (previous_suspend_count > 0); |
| 123 | } |
| 124 | return true; |
Zachary Turner | 119767d | 2014-11-17 17:46:43 +0000 | [diff] [blame] | 125 | } |