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 | |
Zachary Turner | 17f383d | 2014-11-20 22:47:32 +0000 | [diff] [blame] | 10 | #include "lldb/Host/HostInfo.h" |
Zachary Turner | 119767d | 2014-11-17 17:46:43 +0000 | [diff] [blame] | 11 | #include "lldb/Host/HostNativeThreadBase.h" |
| 12 | #include "lldb/Host/windows/HostThreadWindows.h" |
| 13 | #include "lldb/Host/windows/windows.h" |
Zachary Turner | 17f383d | 2014-11-20 22:47:32 +0000 | [diff] [blame] | 14 | #include "lldb/Target/RegisterContext.h" |
Zachary Turner | 6f9e690 | 2017-03-03 20:56:28 +0000 | [diff] [blame] | 15 | #include "lldb/Utility/Log.h" |
| 16 | #include "lldb/Utility/Logging.h" |
Pavel Labath | d821c99 | 2018-08-07 11:07:21 +0000 | [diff] [blame^] | 17 | #include "lldb/Utility/State.h" |
Zachary Turner | 17f383d | 2014-11-20 22:47:32 +0000 | [diff] [blame] | 18 | |
Stella Stamenova | 45d8134 | 2018-07-10 22:05:33 +0000 | [diff] [blame] | 19 | #include "Plugins/Process/Utility/UnwindLLDB.h" |
Zachary Turner | 17f383d | 2014-11-20 22:47:32 +0000 | [diff] [blame] | 20 | #include "ProcessWindows.h" |
Adrian McCarthy | 3ae7492 | 2015-06-01 21:51:50 +0000 | [diff] [blame] | 21 | #include "ProcessWindowsLog.h" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 22 | #include "TargetThreadWindows.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()), |
Stella Stamenova | 45d8134 | 2018-07-10 22:05:33 +0000 | [diff] [blame] | 36 | m_thread_reg_ctx_sp(), m_host_thread(thread) {} |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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) |
Stella Stamenova | 45d8134 | 2018-07-10 22:05:33 +0000 | [diff] [blame] | 52 | m_reg_context_sp = CreateRegisterContextForFrame(nullptr); |
Adrian McCarthy | 4ad5def | 2016-11-23 16:26:37 +0000 | [diff] [blame] | 53 | |
| 54 | return m_reg_context_sp; |
| 55 | } |
| 56 | |
| 57 | RegisterContextSP |
| 58 | TargetThreadWindows::CreateRegisterContextForFrame(StackFrame *frame) { |
Stella Stamenova | 45d8134 | 2018-07-10 22:05:33 +0000 | [diff] [blame] | 59 | RegisterContextSP reg_ctx_sp; |
| 60 | uint32_t concrete_frame_idx = 0; |
| 61 | Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD)); |
Adrian McCarthy | 4ad5def | 2016-11-23 16:26:37 +0000 | [diff] [blame] | 62 | |
Stella Stamenova | 45d8134 | 2018-07-10 22:05:33 +0000 | [diff] [blame] | 63 | if (frame) |
| 64 | concrete_frame_idx = frame->GetConcreteFrameIndex(); |
| 65 | |
| 66 | if (concrete_frame_idx == 0) { |
| 67 | if (!m_thread_reg_ctx_sp) { |
| 68 | ArchSpec arch = HostInfo::GetArchitecture(); |
| 69 | switch (arch.GetMachine()) { |
| 70 | case llvm::Triple::x86: |
Adrian McCarthy | 4ad5def | 2016-11-23 16:26:37 +0000 | [diff] [blame] | 71 | #if defined(_WIN64) |
Stella Stamenova | 45d8134 | 2018-07-10 22:05:33 +0000 | [diff] [blame] | 72 | // FIXME: This is a Wow64 process, create a RegisterContextWindows_Wow64 |
| 73 | LLDB_LOG(log, "This is a Wow64 process, we should create a " |
| 74 | "RegisterContextWindows_Wow64, but we don't."); |
Adrian McCarthy | 4ad5def | 2016-11-23 16:26:37 +0000 | [diff] [blame] | 75 | #else |
Stella Stamenova | 45d8134 | 2018-07-10 22:05:33 +0000 | [diff] [blame] | 76 | m_thread_reg_ctx_sp.reset( |
| 77 | new RegisterContextWindows_x86(*this, concrete_frame_idx)); |
Adrian McCarthy | 4ad5def | 2016-11-23 16:26:37 +0000 | [diff] [blame] | 78 | #endif |
Stella Stamenova | 45d8134 | 2018-07-10 22:05:33 +0000 | [diff] [blame] | 79 | break; |
| 80 | case llvm::Triple::x86_64: |
Adrian McCarthy | 4ad5def | 2016-11-23 16:26:37 +0000 | [diff] [blame] | 81 | #if defined(_WIN64) |
Stella Stamenova | 45d8134 | 2018-07-10 22:05:33 +0000 | [diff] [blame] | 82 | m_thread_reg_ctx_sp.reset( |
| 83 | new RegisterContextWindows_x64(*this, concrete_frame_idx)); |
Adrian McCarthy | 4ad5def | 2016-11-23 16:26:37 +0000 | [diff] [blame] | 84 | #else |
Stella Stamenova | 45d8134 | 2018-07-10 22:05:33 +0000 | [diff] [blame] | 85 | LLDB_LOG(log, "LLDB is 32-bit, but the target process is 64-bit."); |
Adrian McCarthy | 4ad5def | 2016-11-23 16:26:37 +0000 | [diff] [blame] | 86 | #endif |
Stella Stamenova | 45d8134 | 2018-07-10 22:05:33 +0000 | [diff] [blame] | 87 | default: |
| 88 | break; |
| 89 | } |
Adrian McCarthy | 4ad5def | 2016-11-23 16:26:37 +0000 | [diff] [blame] | 90 | } |
Stella Stamenova | 45d8134 | 2018-07-10 22:05:33 +0000 | [diff] [blame] | 91 | reg_ctx_sp = m_thread_reg_ctx_sp; |
| 92 | } else { |
| 93 | Unwind *unwinder = GetUnwinder(); |
| 94 | if (unwinder != nullptr) |
| 95 | reg_ctx_sp = unwinder->CreateRegisterContextForFrame(frame); |
Adrian McCarthy | 4ad5def | 2016-11-23 16:26:37 +0000 | [diff] [blame] | 96 | } |
Stella Stamenova | 45d8134 | 2018-07-10 22:05:33 +0000 | [diff] [blame] | 97 | |
| 98 | return reg_ctx_sp; |
Adrian McCarthy | 4ad5def | 2016-11-23 16:26:37 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 101 | bool TargetThreadWindows::CalculateStopInfo() { |
| 102 | SetStopInfo(m_stop_info_sp); |
| 103 | return true; |
Zachary Turner | 119767d | 2014-11-17 17:46:43 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 106 | Unwind *TargetThreadWindows::GetUnwinder() { |
| 107 | // FIXME: Implement an unwinder based on the Windows unwinder exposed through |
| 108 | // DIA SDK. |
Stella Stamenova | 45d8134 | 2018-07-10 22:05:33 +0000 | [diff] [blame] | 109 | if (!m_unwinder_ap) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 110 | m_unwinder_ap.reset(new UnwindLLDB(*this)); |
| 111 | return m_unwinder_ap.get(); |
Zachary Turner | 119767d | 2014-11-17 17:46:43 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Stella Stamenova | 0fd67b5 | 2018-05-17 21:34:24 +0000 | [diff] [blame] | 114 | Status TargetThreadWindows::DoResume() { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 115 | StateType resume_state = GetTemporaryResumeState(); |
| 116 | StateType current_state = GetState(); |
| 117 | if (resume_state == current_state) |
Stella Stamenova | 0fd67b5 | 2018-05-17 21:34:24 +0000 | [diff] [blame] | 118 | return Status(); |
Zachary Turner | 17f383d | 2014-11-20 22:47:32 +0000 | [diff] [blame] | 119 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 120 | if (resume_state == eStateStepping) { |
| 121 | uint32_t flags_index = |
| 122 | GetRegisterContext()->ConvertRegisterKindToRegisterNumber( |
| 123 | eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FLAGS); |
| 124 | uint64_t flags_value = |
| 125 | GetRegisterContext()->ReadRegisterAsUnsigned(flags_index, 0); |
| 126 | flags_value |= 0x100; // Set the trap flag on the CPU |
| 127 | GetRegisterContext()->WriteRegisterFromUnsigned(flags_index, flags_value); |
| 128 | } |
Zachary Turner | 119767d | 2014-11-17 17:46:43 +0000 | [diff] [blame] | 129 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 130 | if (resume_state == eStateStepping || resume_state == eStateRunning) { |
| 131 | DWORD previous_suspend_count = 0; |
| 132 | HANDLE thread_handle = m_host_thread.GetNativeThread().GetSystemHandle(); |
| 133 | do { |
Stella Stamenova | 45d8134 | 2018-07-10 22:05:33 +0000 | [diff] [blame] | 134 | // ResumeThread returns -1 on error, or the thread's *previous* suspend |
| 135 | // count on success. This means that the return value is 1 when the thread |
| 136 | // was restarted. Note that DWORD is an unsigned int, so we need to |
| 137 | // explicitly compare with -1. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 138 | previous_suspend_count = ::ResumeThread(thread_handle); |
Stella Stamenova | 0fd67b5 | 2018-05-17 21:34:24 +0000 | [diff] [blame] | 139 | |
| 140 | if (previous_suspend_count == (DWORD)-1) |
| 141 | return Status(::GetLastError(), eErrorTypeWin32); |
| 142 | |
| 143 | } while (previous_suspend_count > 1); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 144 | } |
Stella Stamenova | 0fd67b5 | 2018-05-17 21:34:24 +0000 | [diff] [blame] | 145 | |
| 146 | return Status(); |
Zachary Turner | 119767d | 2014-11-17 17:46:43 +0000 | [diff] [blame] | 147 | } |