1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
|
//===-- TargetThreadWindows.cpp--------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "lldb/Host/HostInfo.h"
#include "lldb/Target/Unwind.h"
#include "lldb/Utility/LLDBLog.h"
#include "lldb/Utility/Log.h"
#include "ProcessWindows.h"
#include "TargetThreadWindows.h"
#include "lldb/Host/windows/HostThreadWindows.h"
#include <llvm/Support/ConvertUTF.h>
#if defined(__x86_64__) || defined(_M_AMD64)
#include "x64/RegisterContextWindows_x64.h"
#elif defined(__i386__) || defined(_M_IX86)
#include "x86/RegisterContextWindows_x86.h"
#elif defined(__aarch64__) || defined(_M_ARM64)
#include "arm64/RegisterContextWindows_arm64.h"
#elif defined(__arm__) || defined(_M_ARM)
#include "arm/RegisterContextWindows_arm.h"
#endif
using namespace lldb;
using namespace lldb_private;
using GetThreadDescriptionFunctionPtr =
HRESULT(WINAPI *)(HANDLE hThread, PWSTR *ppszThreadDescription);
TargetThreadWindows::TargetThreadWindows(ProcessWindows &process,
const HostThread &thread)
: Thread(process, thread.GetNativeThread().GetThreadId()),
m_thread_reg_ctx_sp(), m_host_thread(thread) {}
TargetThreadWindows::~TargetThreadWindows() { DestroyThread(); }
void TargetThreadWindows::RefreshStateAfterStop() {
::SuspendThread(m_host_thread.GetNativeThread().GetSystemHandle());
SetState(eStateStopped);
GetRegisterContext()->InvalidateIfNeeded(false);
}
void TargetThreadWindows::WillResume(lldb::StateType resume_state) {}
void TargetThreadWindows::DidStop() {}
RegisterContextSP TargetThreadWindows::GetRegisterContext() {
if (!m_reg_context_sp)
m_reg_context_sp = CreateRegisterContextForFrame(nullptr);
return m_reg_context_sp;
}
RegisterContextSP
TargetThreadWindows::CreateRegisterContextForFrame(StackFrame *frame) {
RegisterContextSP reg_ctx_sp;
uint32_t concrete_frame_idx = 0;
Log *log = GetLog(LLDBLog::Thread);
if (frame)
concrete_frame_idx = frame->GetConcreteFrameIndex();
if (concrete_frame_idx == 0) {
if (!m_thread_reg_ctx_sp) {
ArchSpec arch = HostInfo::GetArchitecture();
switch (arch.GetMachine()) {
case llvm::Triple::arm:
case llvm::Triple::thumb:
#if defined(__arm__) || defined(_M_ARM)
m_thread_reg_ctx_sp.reset(
new RegisterContextWindows_arm(*this, concrete_frame_idx));
#else
LLDB_LOG(log, "debugging foreign targets is currently unsupported");
#endif
break;
case llvm::Triple::aarch64:
#if defined(__aarch64__) || defined(_M_ARM64)
m_thread_reg_ctx_sp.reset(
new RegisterContextWindows_arm64(*this, concrete_frame_idx));
#else
LLDB_LOG(log, "debugging foreign targets is currently unsupported");
#endif
break;
case llvm::Triple::x86:
#if defined(__i386__) || defined(_M_IX86)
m_thread_reg_ctx_sp.reset(
new RegisterContextWindows_x86(*this, concrete_frame_idx));
#else
LLDB_LOG(log, "debugging foreign targets is currently unsupported");
#endif
break;
case llvm::Triple::x86_64:
#if defined(__x86_64__) || defined(_M_AMD64)
m_thread_reg_ctx_sp.reset(
new RegisterContextWindows_x64(*this, concrete_frame_idx));
#else
LLDB_LOG(log, "debugging foreign targets is currently unsupported");
#endif
break;
default:
break;
}
}
reg_ctx_sp = m_thread_reg_ctx_sp;
} else {
reg_ctx_sp = GetUnwinder().CreateRegisterContextForFrame(frame);
}
return reg_ctx_sp;
}
bool TargetThreadWindows::CalculateStopInfo() {
SetStopInfo(m_stop_info_sp);
return true;
}
Status TargetThreadWindows::DoResume() {
StateType resume_state = GetTemporaryResumeState();
StateType current_state = GetState();
if (resume_state == current_state)
return Status();
if (resume_state == eStateStepping) {
Log *log = GetLog(LLDBLog::Thread);
uint32_t flags_index =
GetRegisterContext()->ConvertRegisterKindToRegisterNumber(
eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FLAGS);
uint64_t flags_value =
GetRegisterContext()->ReadRegisterAsUnsigned(flags_index, 0);
ProcessSP process = GetProcess();
const ArchSpec &arch = process->GetTarget().GetArchitecture();
switch (arch.GetMachine()) {
case llvm::Triple::x86:
case llvm::Triple::x86_64:
flags_value |= 0x100; // Set the trap flag on the CPU
break;
case llvm::Triple::aarch64:
case llvm::Triple::arm:
case llvm::Triple::thumb:
flags_value |= 0x200000; // The SS bit in PState
break;
default:
LLDB_LOG(log, "single stepping unsupported on this architecture");
break;
}
GetRegisterContext()->WriteRegisterFromUnsigned(flags_index, flags_value);
}
if (resume_state == eStateStepping || resume_state == eStateRunning) {
DWORD previous_suspend_count = 0;
HANDLE thread_handle = m_host_thread.GetNativeThread().GetSystemHandle();
do {
// ResumeThread returns -1 on error, or the thread's *previous* suspend
// count on success. This means that the return value is 1 when the thread
// was restarted. Note that DWORD is an unsigned int, so we need to
// explicitly compare with -1.
previous_suspend_count = ::ResumeThread(thread_handle);
if (previous_suspend_count == (DWORD)-1)
return Status(::GetLastError(), eErrorTypeWin32);
} while (previous_suspend_count > 1);
}
return Status();
}
const char *TargetThreadWindows::GetName() {
Log *log = GetLog(LLDBLog::Thread);
static GetThreadDescriptionFunctionPtr GetThreadDescription = []() {
HMODULE hModule = ::LoadLibraryW(L"Kernel32.dll");
return hModule
? reinterpret_cast<GetThreadDescriptionFunctionPtr>(
(void *)::GetProcAddress(hModule, "GetThreadDescription"))
: nullptr;
}();
LLDB_LOGF(log, "GetProcAddress: %p",
reinterpret_cast<void *>(GetThreadDescription));
if (!GetThreadDescription)
return m_name.c_str();
PWSTR pszThreadName;
if (SUCCEEDED(GetThreadDescription(
m_host_thread.GetNativeThread().GetSystemHandle(), &pszThreadName))) {
LLDB_LOGF(log, "GetThreadDescription: %ls", pszThreadName);
llvm::convertUTF16ToUTF8String(
llvm::ArrayRef(reinterpret_cast<char *>(pszThreadName),
wcslen(pszThreadName) * sizeof(wchar_t)),
m_name);
::LocalFree(pszThreadName);
}
return m_name.c_str();
}
|