LLDB mainline
NativeThreadWindows.cpp
Go to the documentation of this file.
1//===-- NativeThreadWindows.cpp -------------------------------------------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8
11
17#include "lldb/Utility/Log.h"
18#include "lldb/Utility/State.h"
19
20#include <llvm/Support/ConvertUTF.h>
21
22using namespace lldb;
23using namespace lldb_private;
24
26 const HostThread &thread)
27 : NativeThreadProtocol(process, thread.GetNativeThread().GetThreadId()),
28 m_host_thread(thread) {
31 process.GetArchitecture(), *this));
32}
33
35 if (m_state != eStateStopped) {
36 DWORD previous_suspend_count =
37 ::SuspendThread(m_host_thread.GetNativeThread().GetSystemHandle());
38 if (previous_suspend_count == static_cast<DWORD>(-1))
39 return Status(::GetLastError(), eErrorTypeWin32);
40
41 // Invalidate cached register values on every stop.
43
45 }
46 return Status();
47}
48
50 StateType current_state = GetState();
51 if (resume_state == current_state)
52 return Status();
53
54 lldb::addr_t step_start_pc = LLDB_INVALID_ADDRESS;
55 if (resume_state == eStateStepping) {
58 }
59
60 if (resume_state == eStateStepping || resume_state == eStateRunning) {
61 // Clear any stop info left over from a previous stop.
63
64 DWORD previous_suspend_count = 0;
65 HANDLE thread_handle = m_host_thread.GetNativeThread().GetSystemHandle();
66 do {
67 // ResumeThread returns -1 on error, or the thread's *previous* suspend
68 // count on success. This means that the return value is 1 when the thread
69 // was restarted. Note that DWORD is an unsigned int, so we need to
70 // explicitly compare with -1.
71 previous_suspend_count = ::ResumeThread(thread_handle);
72
73 if (previous_suspend_count == static_cast<DWORD>(-1))
74 return Status(::GetLastError(), eErrorTypeWin32);
75
76 } while (previous_suspend_count > 1);
78 m_single_stepping = resume_state == eStateStepping;
79 m_single_step_start_pc = step_start_pc;
80
82 }
83
84 return Status();
85}
86
89
90 uint32_t flags_index =
93 uint64_t flags_value =
95 const ArchSpec &arch = GetProcess().GetArchitecture();
96 switch (arch.GetMachine()) {
97 case llvm::Triple::x86:
98 case llvm::Triple::x86_64:
99 flags_value |= 0x100; // Set the trap flag on the CPU
100 break;
101 case llvm::Triple::aarch64:
102 case llvm::Triple::arm:
103 case llvm::Triple::thumb:
104 flags_value |= 0x200000; // The SS bit in PState
105 break;
106 default:
107 LLDB_LOG(log, "single stepping unsupported on this architecture");
108 break;
109 }
110 GetRegisterContext().WriteRegisterFromUnsigned(flags_index, flags_value);
111}
112
114 lldb::addr_t start_pc =
116 if (start_pc == LLDB_INVALID_ADDRESS ||
117 (m_stop_info.reason != eStopReasonInvalid &&
118 m_stop_info.reason != eStopReasonNone) ||
119 GetRegisterContext().GetPC(start_pc) == start_pc)
120 return false;
122 return true;
123}
124
126 Log *log = GetLog(LLDBLog::Thread);
127 static LazyImport<HRESULT(WINAPI *)(HANDLE, PWSTR *)>
128 s_get_thread_description{L"Kernel32.dll", "GetThreadDescription"};
129 if (!s_get_thread_description)
130 return m_name;
131 auto GetThreadDescription = *s_get_thread_description;
132
133 PWSTR pszThreadName;
134 if (SUCCEEDED(GetThreadDescription(
135 m_host_thread.GetNativeThread().GetSystemHandle(), &pszThreadName))) {
136 LLDB_LOGF(log, "GetThreadDescription: %ls", pszThreadName);
137 m_name.clear();
138 llvm::convertUTF16ToUTF8String(
139 llvm::ArrayRef(reinterpret_cast<char *>(pszThreadName),
140 wcslen(pszThreadName) * sizeof(wchar_t)),
141 m_name);
142 ::LocalFree(pszThreadName);
143 }
144
145 return m_name;
146}
147
149 std::string description) {
151 m_stop_info = stop_info;
152 m_stop_description = description;
153}
154
156 std::string &description) {
157 Log *log = GetLog(LLDBLog::Thread);
158
159 switch (m_state) {
160 case eStateStopped:
161 case eStateCrashed:
162 case eStateExited:
163 case eStateSuspended:
164 case eStateUnloaded:
165 stop_info = m_stop_info;
166 description = m_stop_description;
167 return true;
168
169 case eStateInvalid:
170 case eStateConnected:
171 case eStateAttaching:
172 case eStateLaunching:
173 case eStateRunning:
174 case eStateStepping:
175 case eStateDetached:
176 LLDB_LOGF(log, "tid %" PRIu64 " in state %s cannot answer stop reason",
178 return false;
179 }
180 llvm_unreachable("unhandled StateType!");
181}
182
184 uint32_t watch_flags, bool hardware) {
185 if (!hardware)
186 return Status::FromErrorString("not implemented");
188 return Status();
190 if (error.Fail())
191 return error;
192 uint32_t wp_index =
193 m_reg_context_up->SetHardwareWatchpoint(addr, size, watch_flags);
194 if (wp_index == LLDB_INVALID_INDEX32)
195 return Status::FromErrorString("Setting hardware watchpoint failed.");
196 m_watchpoint_index_map.insert({addr, wp_index});
197 return Status();
198}
199
201 auto wp = m_watchpoint_index_map.find(addr);
202 if (wp == m_watchpoint_index_map.end())
203 return Status();
204 uint32_t wp_index = wp->second;
205 m_watchpoint_index_map.erase(wp);
206 if (m_reg_context_up->ClearHardwareWatchpoint(wp_index))
207 return Status();
208 return Status::FromErrorString("Clearing hardware watchpoint failed.");
209}
210
212 size_t size) {
213 return Status::FromErrorString("unimplemented.");
214}
215
219
221 return m_host_thread.GetNativeThread().GetExtendedInfo();
222}
static llvm::raw_ostream & error(Stream &strm)
#define LLDB_LOG(log,...)
The LLDB_LOG* macros defined below are the way to emit log messages.
Definition Log.h:375
#define LLDB_LOGF(log,...)
Definition Log.h:389
void * HANDLE
An architecture specification class.
Definition ArchSpec.h:32
llvm::Triple::ArchType GetMachine() const
Returns a machine family for the current architecture.
Definition ArchSpec.cpp:702
virtual const ArchSpec & GetArchitecture() const =0
const ArchSpec & GetArchitecture() const override
static std::unique_ptr< NativeRegisterContextWindows > CreateHostNativeRegisterContextWindows(const ArchSpec &target_arch, NativeThreadProtocol &native_thread)
uint32_t ConvertRegisterKindToRegisterNumber(uint32_t kind, uint32_t num) const
lldb::addr_t ReadRegisterAsUnsigned(uint32_t reg, lldb::addr_t fail_value)
Status WriteRegisterFromUnsigned(uint32_t reg, uint64_t uval)
lldb::addr_t GetPC(lldb::addr_t fail_value=LLDB_INVALID_ADDRESS)
NativeThreadProtocol(NativeProcessProtocol &process, lldb::tid_t tid)
StructuredData::ObjectSP GetExtendedInfo() const override
Status RemoveWatchpoint(lldb::addr_t addr) override
bool CompleteSingleStepWithoutTrap()
Called when a stop is reported.
Status SetWatchpoint(lldb::addr_t addr, size_t size, uint32_t watch_flags, bool hardware) override
void SetStopReason(ThreadStopInfo stop_info, std::string description)
Status DoResume(lldb::StateType resume_state)
bool GetStopReason(ThreadStopInfo &stop_info, std::string &description) override
NativeRegisterContextWindows & GetRegisterContext() override
lldb::StateType GetState() override
NativeThreadWindows(NativeProcessWindows &process, const HostThread &thread)
Status SetHardwareBreakpoint(lldb::addr_t addr, size_t size) override
std::unique_ptr< NativeRegisterContextWindows > m_reg_context_up
Status RemoveHardwareBreakpoint(lldb::addr_t addr) override
An error handling class.
Definition Status.h:118
static Status FromErrorString(const char *str)
Definition Status.h:141
std::shared_ptr< Object > ObjectSP
#define LLDB_INVALID_INDEX32
#define LLDB_REGNUM_GENERIC_FLAGS
#define LLDB_INVALID_ADDRESS
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition Log.h:338
const char * StateAsCString(lldb::StateType state)
Converts a StateType to a C string.
Definition State.cpp:14
StateType
Process and Thread States.
@ eStateUnloaded
Process is object is valid, but not currently loaded.
@ eStateConnected
Process is connected to remote debug services, but not launched or attached to anything yet.
@ eStateDetached
Process has been detached and can't be examined.
@ eStateStopped
Process or thread is stopped and can be examined.
@ eStateSuspended
Process or thread is in a suspended state as far as the debugger is concerned while other processes o...
@ eStateRunning
Process or thread is running and can't be examined.
@ eStateLaunching
Process is in the process of launching.
@ eStateAttaching
Process is currently trying to attach.
@ eStateExited
Process has exited and can't be examined.
@ eStateStepping
Process or thread is in the process of stepping and can not be examined.
@ eStateCrashed
Process or thread has crashed and can be examined.
@ eErrorTypeWin32
Standard Win32 error codes.
uint64_t addr_t
Definition lldb-types.h:80
@ eRegisterKindGeneric
insn ptr reg, stack ptr reg, etc not specific to any particular target