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
15#include "lldb/Target/Process.h"
17#include "lldb/Utility/Log.h"
18#include "lldb/Utility/State.h"
19
20#include "lldb/lldb-forward.h"
21
22using namespace lldb;
23using namespace lldb_private;
24
26 const HostThread &thread)
27 : NativeThreadProtocol(process, thread.GetNativeThread().GetThreadId()),
28 m_stop_info(), m_stop_description(), m_host_thread(thread) {
31 process.GetArchitecture(), *this));
32}
33
35 if (m_state != eStateStopped) {
36 DWORD previous_suspend_count =
38 if (previous_suspend_count == (DWORD)-1)
39 return Status(::GetLastError(), eErrorTypeWin32);
40
42 }
43 return Status();
44}
45
47 StateType current_state = GetState();
48 if (resume_state == current_state)
49 return Status();
50
51 if (resume_state == eStateStepping) {
53
54 uint32_t flags_index =
57 uint64_t flags_value =
60 const ArchSpec &arch = process.GetArchitecture();
61 switch (arch.GetMachine()) {
62 case llvm::Triple::x86:
63 case llvm::Triple::x86_64:
64 flags_value |= 0x100; // Set the trap flag on the CPU
65 break;
66 case llvm::Triple::aarch64:
67 case llvm::Triple::arm:
68 case llvm::Triple::thumb:
69 flags_value |= 0x200000; // The SS bit in PState
70 break;
71 default:
72 LLDB_LOG(log, "single stepping unsupported on this architecture");
73 break;
74 }
75 GetRegisterContext().WriteRegisterFromUnsigned(flags_index, flags_value);
76 }
77
78 if (resume_state == eStateStepping || resume_state == eStateRunning) {
79 DWORD previous_suspend_count = 0;
80 HANDLE thread_handle = m_host_thread.GetNativeThread().GetSystemHandle();
81 do {
82 // ResumeThread returns -1 on error, or the thread's *previous* suspend
83 // count on success. This means that the return value is 1 when the thread
84 // was restarted. Note that DWORD is an unsigned int, so we need to
85 // explicitly compare with -1.
86 previous_suspend_count = ::ResumeThread(thread_handle);
87
88 if (previous_suspend_count == (DWORD)-1)
89 return Status(::GetLastError(), eErrorTypeWin32);
90
91 } while (previous_suspend_count > 1);
93 }
94
95 return Status();
96}
97
99 if (!m_name.empty())
100 return m_name;
101
102 // Name is not a property of the Windows thread. Create one with the
103 // process's.
105 ProcessInstanceInfo process_info;
106 if (Host::GetProcessInfo(process.GetID(), process_info)) {
107 std::string process_name(process_info.GetName());
108 m_name = process_name;
109 }
110 return m_name;
111}
112
114 std::string description) {
116 m_stop_info = stop_info;
117 m_stop_description = description;
118}
119
121 std::string &description) {
122 Log *log = GetLog(LLDBLog::Thread);
123
124 switch (m_state) {
125 case eStateStopped:
126 case eStateCrashed:
127 case eStateExited:
128 case eStateSuspended:
129 case eStateUnloaded:
130 stop_info = m_stop_info;
131 description = m_stop_description;
132 return true;
133
134 case eStateInvalid:
135 case eStateConnected:
136 case eStateAttaching:
137 case eStateLaunching:
138 case eStateRunning:
139 case eStateStepping:
140 case eStateDetached:
141 if (log) {
142 log->Printf("NativeThreadWindows::%s tid %" PRIu64
143 " in state %s cannot answer stop reason",
144 __FUNCTION__, GetID(), StateAsCString(m_state));
145 }
146 return false;
147 }
148 llvm_unreachable("unhandled StateType!");
149}
150
152 uint32_t watch_flags, bool hardware) {
153 if (!hardware)
154 return Status("not implemented");
156 return Status();
158 if (error.Fail())
159 return error;
160 uint32_t wp_index =
161 m_reg_context_up->SetHardwareWatchpoint(addr, size, watch_flags);
162 if (wp_index == LLDB_INVALID_INDEX32)
163 return Status("Setting hardware watchpoint failed.");
164 m_watchpoint_index_map.insert({addr, wp_index});
165 return Status();
166}
167
169 auto wp = m_watchpoint_index_map.find(addr);
170 if (wp == m_watchpoint_index_map.end())
171 return Status();
172 uint32_t wp_index = wp->second;
173 m_watchpoint_index_map.erase(wp);
174 if (m_reg_context_up->ClearHardwareWatchpoint(wp_index))
175 return Status();
176 return Status("Clearing hardware watchpoint failed.");
177}
178
180 size_t size) {
181 return Status("unimplemented.");
182}
183
185 return Status("unimplemented.");
186}
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:359
An architecture specification class.
Definition: ArchSpec.h:31
llvm::Triple::ArchType GetMachine() const
Returns a machine family for the current architecture.
Definition: ArchSpec.cpp:683
HostNativeThread & GetNativeThread()
Definition: HostThread.cpp:32
static bool GetProcessInfo(lldb::pid_t pid, ProcessInstanceInfo &proc_info)
void void Printf(const char *format,...) __attribute__((format(printf
Prefer using LLDB_LOGF whenever possible.
Definition: Log.cpp:146
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)
NativeProcessProtocol & GetProcess()
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
const char * GetName() const
Definition: ProcessInfo.cpp:45
An error handling class.
Definition: Status.h:44
#define LLDB_INVALID_INDEX32
Definition: lldb-defines.h:83
#define LLDB_REGNUM_GENERIC_FLAGS
Definition: lldb-defines.h:60
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:331
const char * StateAsCString(lldb::StateType state)
Converts a StateType to a C string.
Definition: State.cpp:14
Definition: SBAddress.h:15
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