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