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
16#include "lldb/Target/Process.h"
18#include "lldb/Utility/Log.h"
19#include "lldb/Utility/State.h"
20
21#include "lldb/lldb-forward.h"
22#include <llvm/Support/ConvertUTF.h>
23
24using namespace lldb;
25using namespace lldb_private;
26
35
37 if (m_state != eStateStopped) {
38 DWORD previous_suspend_count =
39 ::SuspendThread(m_host_thread.GetNativeThread().GetSystemHandle());
40 if (previous_suspend_count == static_cast<DWORD>(-1))
41 return Status(::GetLastError(), eErrorTypeWin32);
42
43 // Invalidate cached register values on every stop.
45
47 }
48 return Status();
49}
50
52 StateType current_state = GetState();
53 if (resume_state == current_state)
54 return Status();
55
56 if (resume_state == eStateStepping) {
58
59 uint32_t flags_index =
62 uint64_t flags_value =
65 const ArchSpec &arch = process.GetArchitecture();
66 switch (arch.GetMachine()) {
67 case llvm::Triple::x86:
68 case llvm::Triple::x86_64:
69 flags_value |= 0x100; // Set the trap flag on the CPU
70 break;
71 case llvm::Triple::aarch64:
72 case llvm::Triple::arm:
73 case llvm::Triple::thumb:
74 flags_value |= 0x200000; // The SS bit in PState
75 break;
76 default:
77 LLDB_LOG(log, "single stepping unsupported on this architecture");
78 break;
79 }
80 GetRegisterContext().WriteRegisterFromUnsigned(flags_index, flags_value);
81 }
82
83 if (resume_state == eStateStepping || resume_state == eStateRunning) {
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 }
99
100 return Status();
101}
102
104 Log *log = GetLog(LLDBLog::Thread);
105 static LazyImport<HRESULT(WINAPI *)(HANDLE, PWSTR *)>
106 s_get_thread_description{L"Kernel32.dll", "GetThreadDescription"};
107 if (!s_get_thread_description)
108 return m_name;
109 auto GetThreadDescription = *s_get_thread_description;
110
111 PWSTR pszThreadName;
112 if (SUCCEEDED(GetThreadDescription(
113 m_host_thread.GetNativeThread().GetSystemHandle(), &pszThreadName))) {
114 LLDB_LOGF(log, "GetThreadDescription: %ls", pszThreadName);
115 m_name.clear();
116 llvm::convertUTF16ToUTF8String(
117 llvm::ArrayRef(reinterpret_cast<char *>(pszThreadName),
118 wcslen(pszThreadName) * sizeof(wchar_t)),
119 m_name);
120 ::LocalFree(pszThreadName);
121 }
122
123 return m_name;
124}
125
127 std::string description) {
129 m_stop_info = stop_info;
130 m_stop_description = description;
131}
132
134 std::string &description) {
135 Log *log = GetLog(LLDBLog::Thread);
136
137 switch (m_state) {
138 case eStateStopped:
139 case eStateCrashed:
140 case eStateExited:
141 case eStateSuspended:
142 case eStateUnloaded:
143 stop_info = m_stop_info;
144 description = m_stop_description;
145 return true;
146
147 case eStateInvalid:
148 case eStateConnected:
149 case eStateAttaching:
150 case eStateLaunching:
151 case eStateRunning:
152 case eStateStepping:
153 case eStateDetached:
154 if (log) {
155 log->Printf("NativeThreadWindows::%s tid %" PRIu64
156 " in state %s cannot answer stop reason",
157 __FUNCTION__, GetID(), StateAsCString(m_state));
158 }
159 return false;
160 }
161 llvm_unreachable("unhandled StateType!");
162}
163
165 uint32_t watch_flags, bool hardware) {
166 if (!hardware)
167 return Status::FromErrorString("not implemented");
169 return Status();
171 if (error.Fail())
172 return error;
173 uint32_t wp_index =
174 m_reg_context_up->SetHardwareWatchpoint(addr, size, watch_flags);
175 if (wp_index == LLDB_INVALID_INDEX32)
176 return Status::FromErrorString("Setting hardware watchpoint failed.");
177 m_watchpoint_index_map.insert({addr, wp_index});
178 return Status();
179}
180
182 auto wp = m_watchpoint_index_map.find(addr);
183 if (wp == m_watchpoint_index_map.end())
184 return Status();
185 uint32_t wp_index = wp->second;
186 m_watchpoint_index_map.erase(wp);
187 if (m_reg_context_up->ClearHardwareWatchpoint(wp_index))
188 return Status();
189 return Status::FromErrorString("Clearing hardware watchpoint failed.");
190}
191
193 size_t size) {
194 return Status::FromErrorString("unimplemented.");
195}
196
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:364
#define LLDB_LOGF(log,...)
Definition Log.h:378
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:682
void void Printf(const char *format,...) __attribute__((format(printf
Prefer using LLDB_LOGF whenever possible.
Definition Log.cpp:156
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)
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
#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:327
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