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