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
33
35 if (m_state != eStateStopped) {
36 DWORD previous_suspend_count =
37 ::SuspendThread(m_host_thread.GetNativeThread().GetSystemHandle());
38 if (previous_suspend_count == (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 =
63 const ArchSpec &arch = process.GetArchitecture();
64 switch (arch.GetMachine()) {
65 case llvm::Triple::x86:
66 case llvm::Triple::x86_64:
67 flags_value |= 0x100; // Set the trap flag on the CPU
68 break;
69 case llvm::Triple::aarch64:
70 case llvm::Triple::arm:
71 case llvm::Triple::thumb:
72 flags_value |= 0x200000; // The SS bit in PState
73 break;
74 default:
75 LLDB_LOG(log, "single stepping unsupported on this architecture");
76 break;
77 }
78 GetRegisterContext().WriteRegisterFromUnsigned(flags_index, flags_value);
79 }
80
81 if (resume_state == eStateStepping || resume_state == eStateRunning) {
82 DWORD previous_suspend_count = 0;
83 HANDLE thread_handle = m_host_thread.GetNativeThread().GetSystemHandle();
84 do {
85 // ResumeThread returns -1 on error, or the thread's *previous* suspend
86 // count on success. This means that the return value is 1 when the thread
87 // was restarted. Note that DWORD is an unsigned int, so we need to
88 // explicitly compare with -1.
89 previous_suspend_count = ::ResumeThread(thread_handle);
90
91 if (previous_suspend_count == (DWORD)-1)
92 return Status(::GetLastError(), eErrorTypeWin32);
93
94 } while (previous_suspend_count > 1);
96 }
97
98 return Status();
99}
100
102 if (!m_name.empty())
103 return m_name;
104
105 // Name is not a property of the Windows thread. Create one with the
106 // process's.
108 ProcessInstanceInfo process_info;
109 if (Host::GetProcessInfo(process.GetID(), process_info)) {
110 std::string process_name(process_info.GetName());
111 m_name = process_name;
112 }
113 return m_name;
114}
115
117 std::string description) {
119 m_stop_info = stop_info;
120 m_stop_description = description;
121}
122
124 std::string &description) {
125 Log *log = GetLog(LLDBLog::Thread);
126
127 switch (m_state) {
128 case eStateStopped:
129 case eStateCrashed:
130 case eStateExited:
131 case eStateSuspended:
132 case eStateUnloaded:
133 stop_info = m_stop_info;
134 description = m_stop_description;
135 return true;
136
137 case eStateInvalid:
138 case eStateConnected:
139 case eStateAttaching:
140 case eStateLaunching:
141 case eStateRunning:
142 case eStateStepping:
143 case eStateDetached:
144 if (log) {
145 log->Printf("NativeThreadWindows::%s tid %" PRIu64
146 " in state %s cannot answer stop reason",
147 __FUNCTION__, GetID(), StateAsCString(m_state));
148 }
149 return false;
150 }
151 llvm_unreachable("unhandled StateType!");
152}
153
155 uint32_t watch_flags, bool hardware) {
156 if (!hardware)
157 return Status::FromErrorString("not implemented");
159 return Status();
161 if (error.Fail())
162 return error;
163 uint32_t wp_index =
164 m_reg_context_up->SetHardwareWatchpoint(addr, size, watch_flags);
165 if (wp_index == LLDB_INVALID_INDEX32)
166 return Status::FromErrorString("Setting hardware watchpoint failed.");
167 m_watchpoint_index_map.insert({addr, wp_index});
168 return Status();
169}
170
172 auto wp = m_watchpoint_index_map.find(addr);
173 if (wp == m_watchpoint_index_map.end())
174 return Status();
175 uint32_t wp_index = wp->second;
176 m_watchpoint_index_map.erase(wp);
177 if (m_reg_context_up->ClearHardwareWatchpoint(wp_index))
178 return Status();
179 return Status::FromErrorString("Clearing hardware watchpoint failed.");
180}
181
183 size_t size) {
184 return Status::FromErrorString("unimplemented.");
185}
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:364
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
static bool GetProcessInfo(lldb::pid_t pid, ProcessInstanceInfo &proc_info)
Definition aix/Host.cpp:211
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
const char * GetName() const
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