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 if (resume_state == eStateStepping) {
56
57 uint32_t flags_index =
60 uint64_t flags_value =
62 const ArchSpec &arch = GetProcess().GetArchitecture();
63 switch (arch.GetMachine()) {
64 case llvm::Triple::x86:
65 case llvm::Triple::x86_64:
66 flags_value |= 0x100; // Set the trap flag on the CPU
67 break;
68 case llvm::Triple::aarch64:
69 case llvm::Triple::arm:
70 case llvm::Triple::thumb:
71 flags_value |= 0x200000; // The SS bit in PState
72 break;
73 default:
74 LLDB_LOG(log, "single stepping unsupported on this architecture");
75 break;
76 }
77 GetRegisterContext().WriteRegisterFromUnsigned(flags_index, flags_value);
78 }
79
80 if (resume_state == eStateStepping || resume_state == eStateRunning) {
81 // Clear any stop info left over from a previous stop.
83
84 DWORD previous_suspend_count = 0;
85 HANDLE thread_handle = m_host_thread.GetNativeThread().GetSystemHandle();
86 do {
87 // ResumeThread returns -1 on error, or the thread's *previous* suspend
88 // count on success. This means that the return value is 1 when the thread
89 // was restarted. Note that DWORD is an unsigned int, so we need to
90 // explicitly compare with -1.
91 previous_suspend_count = ::ResumeThread(thread_handle);
92
93 if (previous_suspend_count == static_cast<DWORD>(-1))
94 return Status(::GetLastError(), eErrorTypeWin32);
95
96 } while (previous_suspend_count > 1);
98 m_single_stepping = resume_state == eStateStepping;
99 }
100
101 return Status();
102}
103
105 Log *log = GetLog(LLDBLog::Thread);
106 static LazyImport<HRESULT(WINAPI *)(HANDLE, PWSTR *)>
107 s_get_thread_description{L"Kernel32.dll", "GetThreadDescription"};
108 if (!s_get_thread_description)
109 return m_name;
110 auto GetThreadDescription = *s_get_thread_description;
111
112 PWSTR pszThreadName;
113 if (SUCCEEDED(GetThreadDescription(
114 m_host_thread.GetNativeThread().GetSystemHandle(), &pszThreadName))) {
115 LLDB_LOGF(log, "GetThreadDescription: %ls", pszThreadName);
116 m_name.clear();
117 llvm::convertUTF16ToUTF8String(
118 llvm::ArrayRef(reinterpret_cast<char *>(pszThreadName),
119 wcslen(pszThreadName) * sizeof(wchar_t)),
120 m_name);
121 ::LocalFree(pszThreadName);
122 }
123
124 return m_name;
125}
126
128 std::string description) {
130 m_stop_info = stop_info;
131 m_stop_description = description;
132}
133
135 std::string &description) {
136 Log *log = GetLog(LLDBLog::Thread);
137
138 switch (m_state) {
139 case eStateStopped:
140 case eStateCrashed:
141 case eStateExited:
142 case eStateSuspended:
143 case eStateUnloaded:
144 stop_info = m_stop_info;
145 description = m_stop_description;
146 return true;
147
148 case eStateInvalid:
149 case eStateConnected:
150 case eStateAttaching:
151 case eStateLaunching:
152 case eStateRunning:
153 case eStateStepping:
154 case eStateDetached:
155 LLDB_LOGF(log, "tid %" PRIu64 " in state %s cannot answer stop reason",
157 return false;
158 }
159 llvm_unreachable("unhandled StateType!");
160}
161
163 uint32_t watch_flags, bool hardware) {
164 if (!hardware)
165 return Status::FromErrorString("not implemented");
167 return Status();
169 if (error.Fail())
170 return error;
171 uint32_t wp_index =
172 m_reg_context_up->SetHardwareWatchpoint(addr, size, watch_flags);
173 if (wp_index == LLDB_INVALID_INDEX32)
174 return Status::FromErrorString("Setting hardware watchpoint failed.");
175 m_watchpoint_index_map.insert({addr, wp_index});
176 return Status();
177}
178
180 auto wp = m_watchpoint_index_map.find(addr);
181 if (wp == m_watchpoint_index_map.end())
182 return Status();
183 uint32_t wp_index = wp->second;
184 m_watchpoint_index_map.erase(wp);
185 if (m_reg_context_up->ClearHardwareWatchpoint(wp_index))
186 return Status();
187 return Status::FromErrorString("Clearing hardware watchpoint failed.");
188}
189
191 size_t size) {
192 return Status::FromErrorString("unimplemented.");
193}
194
198
200 return m_host_thread.GetNativeThread().GetExtendedInfo();
201}
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:886
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)
NativeThreadProtocol(NativeProcessProtocol &process, lldb::tid_t tid)
StructuredData::ObjectSP GetExtendedInfo() const override
Status RemoveWatchpoint(lldb::addr_t addr) override
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
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