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 
9 #include "NativeThreadWindows.h"
10 #include "NativeProcessWindows.h"
11 
12 #include "lldb/Host/HostThread.h"
15 #include "lldb/Target/Process.h"
16 #include "lldb/Utility/LLDBLog.h"
17 #include "lldb/Utility/Log.h"
18 #include "lldb/Utility/State.h"
19 
20 #include "lldb/lldb-forward.h"
21 
22 using namespace lldb;
23 using namespace lldb_private;
24 
25 NativeThreadWindows::NativeThreadWindows(NativeProcessWindows &process,
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 =
37  ::SuspendThread(m_host_thread.GetNativeThread().GetSystemHandle());
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) {
52  Log *log = GetLog(LLDBLog::Thread);
53 
54  uint32_t flags_index =
57  uint64_t flags_value =
59  NativeProcessProtocol &process = GetProcess();
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.
104  NativeProcessProtocol &process = GetProcess();
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");
155  if (m_state == eStateLaunching)
156  return Status();
157  Status error = RemoveWatchpoint(addr);
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 }
lldb_private::ProcessInstanceInfo
Definition: ProcessInfo.h:108
lldb_private::NativeThreadWindows::SetStopReason
void SetStopReason(ThreadStopInfo stop_info, std::string description)
Definition: NativeThreadWindows.cpp:113
lldb_private::ArchSpec
Definition: ArchSpec.h:33
lldb_private::HostThread::GetNativeThread
HostNativeThread & GetNativeThread()
Definition: HostThread.cpp:32
lldb_private::ArchSpec::GetMachine
llvm::Triple::ArchType GetMachine() const
Returns a machine family for the current architecture.
Definition: ArchSpec.cpp:668
lldb_private::NativeThreadProtocol
Definition: NativeThreadProtocol.h:24
lldb_private::NativeRegisterContext::WriteRegisterFromUnsigned
Status WriteRegisterFromUnsigned(uint32_t reg, uint64_t uval)
Definition: NativeRegisterContext.cpp:222
lldb_private::HostThread
Definition: HostThread.h:29
lldb_private::NativeThreadWindows::SetHardwareBreakpoint
Status SetHardwareBreakpoint(lldb::addr_t addr, size_t size) override
Definition: NativeThreadWindows.cpp:179
lldb_private::NativeThreadWindows::DoStop
Status DoStop()
Definition: NativeThreadWindows.cpp:34
lldb::eRegisterKindGeneric
@ eRegisterKindGeneric
insn ptr reg, stack ptr reg, etc not specific to any particular target
Definition: lldb-enumerations.h:230
lldb::eStateAttaching
@ eStateAttaching
Process is currently trying to attach.
Definition: lldb-enumerations.h:78
lldb_private::NativeThreadProtocol::GetProcess
NativeProcessProtocol & GetProcess()
Definition: NativeThreadProtocol.h:41
lldb_private::StateAsCString
const char * StateAsCString(lldb::StateType state)
Converts a StateType to a C string.
Definition: State.cpp:14
lldb::addr_t
uint64_t addr_t
Definition: lldb-types.h:83
lldb_private::NativeThreadWindows::GetStopReason
bool GetStopReason(ThreadStopInfo &stop_info, std::string &description) override
Definition: NativeThreadWindows.cpp:120
lldb::eErrorTypeWin32
@ eErrorTypeWin32
Standard Win32 error codes.
Definition: lldb-enumerations.h:312
lldb::eStateExited
@ eStateExited
Process has exited and can't be examined.
Definition: lldb-enumerations.h:90
Process.h
lldb_private::NativeThreadWindows::m_stop_info
ThreadStopInfo m_stop_info
Definition: NativeThreadWindows.h:58
lldb_private::NativeRegisterContext::ReadRegisterAsUnsigned
lldb::addr_t ReadRegisterAsUnsigned(uint32_t reg, lldb::addr_t fail_value)
Definition: NativeRegisterContext.cpp:189
lldb::eStateStopped
@ eStateStopped
Process or thread is stopped and can be examined.
Definition: lldb-enumerations.h:84
lldb_private::NativeThreadWindows::m_name
std::string m_name
Definition: NativeThreadWindows.h:57
LLDB_REGNUM_GENERIC_FLAGS
#define LLDB_REGNUM_GENERIC_FLAGS
Definition: lldb-defines.h:55
lldb_private::Log::Printf
void Printf(const char *format,...) __attribute__((format(printf
Prefer using LLDB_LOGF whenever possible.
Definition: Log.cpp:137
error
static llvm::raw_ostream & error(Stream &strm)
Definition: CommandReturnObject.cpp:17
HostThread.h
lldb::eStateCrashed
@ eStateCrashed
Process or thread has crashed and can be examined.
Definition: lldb-enumerations.h:88
lldb_private::NativeRegisterContext::ConvertRegisterKindToRegisterNumber
uint32_t ConvertRegisterKindToRegisterNumber(uint32_t kind, uint32_t num) const
Definition: NativeRegisterContext.cpp:424
Log.h
lldb_private::NativeThreadWindows::m_stop_description
std::string m_stop_description
Definition: NativeThreadWindows.h:59
lldb::eStateConnected
@ eStateConnected
Process is connected to remote debug services, but not launched or attached to anything yet.
Definition: lldb-enumerations.h:76
lldb_private::NativeThreadWindows::GetName
std::string GetName() override
Definition: NativeThreadWindows.cpp:98
lldb::eStateLaunching
@ eStateLaunching
Process is in the process of launching.
Definition: lldb-enumerations.h:79
lldb_private::LLDBLog::Thread
@ Thread
lldb_private::NativeProcessProtocol::GetID
lldb::pid_t GetID() const
Definition: NativeProcessProtocol.h:183
lldb_private::ProcessInfo::GetName
const char * GetName() const
Definition: ProcessInfo.cpp:42
lldb_private::NativeThreadWindows::m_reg_context_up
std::unique_ptr< NativeRegisterContextWindows > m_reg_context_up
Definition: NativeThreadWindows.h:60
lldb::eStateDetached
@ eStateDetached
Process has been detached and can't be examined.
Definition: lldb-enumerations.h:89
lldb_private::NativeThreadWindows::RemoveWatchpoint
Status RemoveWatchpoint(lldb::addr_t addr) override
Definition: NativeThreadWindows.cpp:168
lldb_private::NativeThreadWindows::GetRegisterContext
NativeRegisterContextWindows & GetRegisterContext() override
Definition: NativeThreadWindows.h:35
string
string(SUBSTRING ${p} 10 -1 pStripped) if($
Definition: Plugins/CMakeLists.txt:40
lldb_private::HostNativeThreadBase::GetSystemHandle
lldb::thread_t GetSystemHandle() const
Definition: HostNativeThreadBase.cpp:24
lldb::eStateUnloaded
@ eStateUnloaded
Process is object is valid, but not currently loaded.
Definition: lldb-enumerations.h:75
lldb_private::ThreadStopInfo
Definition: Debug.h:131
lldb_private::NativeThreadWindows::m_state
lldb::StateType m_state
Definition: NativeThreadWindows.h:56
lldb_private::NativeThreadWindows::GetState
lldb::StateType GetState() override
Definition: NativeThreadWindows.h:33
lldb_private::NativeProcessWindows::GetArchitecture
const ArchSpec & GetArchitecture() const override
Definition: NativeProcessWindows.h:79
lldb::eStateStepping
@ eStateStepping
Process or thread is in the process of stepping and can not be examined.
Definition: lldb-enumerations.h:86
lldb::eStateSuspended
@ eStateSuspended
Process or thread is in a suspended state as far as the debugger is concerned while other processes o...
Definition: lldb-enumerations.h:91
lldb_private::Status
Definition: Status.h:44
lldb_private::NativeThreadWindows::m_watchpoint_index_map
IndexMap m_watchpoint_index_map
Definition: NativeThreadWindows.h:64
lldb_private::NativeRegisterContextWindows::CreateHostNativeRegisterContextWindows
static std::unique_ptr< NativeRegisterContextWindows > CreateHostNativeRegisterContextWindows(const ArchSpec &target_arch, NativeThreadProtocol &native_thread)
uint32_t
NativeProcessWindows.h
lldb_private::NativeThreadWindows::SetWatchpoint
Status SetWatchpoint(lldb::addr_t addr, size_t size, uint32_t watch_flags, bool hardware) override
Definition: NativeThreadWindows.cpp:151
lldb_private::NativeProcessProtocol
Definition: NativeProcessProtocol.h:48
lldb_private::NativeThreadProtocol::GetID
lldb::tid_t GetID() const
Definition: NativeThreadProtocol.h:39
lldb_private::Host::GetProcessInfo
static bool GetProcessInfo(lldb::pid_t pid, ProcessInstanceInfo &proc_info)
Definition: freebsd/Host.cpp:230
lldb_private::NativeThreadWindows::DoResume
Status DoResume(lldb::StateType resume_state)
Definition: NativeThreadWindows.cpp:46
windows.h
LLDB_LOG
#define LLDB_LOG(log,...)
The LLDB_LOG* macros defined below are the way to emit log messages.
Definition: Log.h:336
lldb-forward.h
lldb_private::NativeProcessWindows
Definition: NativeProcessWindows.h:30
lldb_private::NativeThreadWindows::RemoveHardwareBreakpoint
Status RemoveHardwareBreakpoint(lldb::addr_t addr) override
Definition: NativeThreadWindows.cpp:184
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb::eStateInvalid
@ eStateInvalid
Definition: lldb-enumerations.h:74
State.h
LLDB_INVALID_INDEX32
#define LLDB_INVALID_INDEX32
Definition: lldb-defines.h:75
lldb::StateType
StateType
Process and Thread States.
Definition: lldb-enumerations.h:73
lldb_private::Log
Definition: Log.h:115
lldb_private::NativeThreadWindows::m_host_thread
HostThread m_host_thread
Definition: NativeThreadWindows.h:66
lldb_private::GetLog
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition: Log.h:308
lldb
Definition: SBAddress.h:15
lldb::eStateRunning
@ eStateRunning
Process or thread is running and can't be examined.
Definition: lldb-enumerations.h:85
LLDBLog.h
HostThreadWindows.h
lldb_private::NativeProcessProtocol::GetArchitecture
virtual const ArchSpec & GetArchitecture() const =0
NativeThreadWindows.h