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/Log.h"
17 #include "lldb/Utility/State.h"
18 
19 #include "lldb/lldb-forward.h"
20 
21 using namespace lldb;
22 using namespace lldb_private;
23 
24 NativeThreadWindows::NativeThreadWindows(NativeProcessWindows &process,
25  const HostThread &thread)
26  : NativeThreadProtocol(process, thread.GetNativeThread().GetThreadId()),
27  m_stop_info(), m_stop_description(), m_host_thread(thread) {
30  process.GetArchitecture(), *this));
31 }
32 
34  if (m_state != eStateStopped) {
35  DWORD previous_suspend_count =
36  ::SuspendThread(m_host_thread.GetNativeThread().GetSystemHandle());
37  if (previous_suspend_count == (DWORD)-1)
38  return Status(::GetLastError(), eErrorTypeWin32);
39 
41  }
42  return Status();
43 }
44 
46  StateType current_state = GetState();
47  if (resume_state == current_state)
48  return Status();
49 
50  if (resume_state == eStateStepping) {
51  uint32_t flags_index =
54  uint64_t flags_value =
56  flags_value |= 0x100; // Set the trap flag on the CPU
57  GetRegisterContext().WriteRegisterFromUnsigned(flags_index, flags_value);
58  }
59 
60  if (resume_state == eStateStepping || resume_state == eStateRunning) {
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 == (DWORD)-1)
71  return Status(::GetLastError(), eErrorTypeWin32);
72 
73  } while (previous_suspend_count > 1);
75  }
76 
77  return Status();
78 }
79 
81  if (!m_name.empty())
82  return m_name;
83 
84  // Name is not a property of the Windows thread. Create one with the
85  // process's.
86  NativeProcessProtocol &process = GetProcess();
87  ProcessInstanceInfo process_info;
88  if (Host::GetProcessInfo(process.GetID(), process_info)) {
89  std::string process_name(process_info.GetName());
90  m_name = process_name;
91  }
92  return m_name;
93 }
94 
96  std::string description) {
98  m_stop_info = stop_info;
99  m_stop_description = description;
100 }
101 
103  std::string &description) {
105 
106  switch (m_state) {
107  case eStateStopped:
108  case eStateCrashed:
109  case eStateExited:
110  case eStateSuspended:
111  case eStateUnloaded:
112  stop_info = m_stop_info;
113  description = m_stop_description;
114  return true;
115 
116  case eStateInvalid:
117  case eStateConnected:
118  case eStateAttaching:
119  case eStateLaunching:
120  case eStateRunning:
121  case eStateStepping:
122  case eStateDetached:
123  if (log) {
124  log->Printf("NativeThreadWindows::%s tid %" PRIu64
125  " in state %s cannot answer stop reason",
126  __FUNCTION__, GetID(), StateAsCString(m_state));
127  }
128  return false;
129  }
130  llvm_unreachable("unhandled StateType!");
131 }
132 
134  uint32_t watch_flags, bool hardware) {
135  if (!hardware)
136  return Status("not implemented");
137  if (m_state == eStateLaunching)
138  return Status();
139  Status error = RemoveWatchpoint(addr);
140  if (error.Fail())
141  return error;
142  uint32_t wp_index =
143  m_reg_context_up->SetHardwareWatchpoint(addr, size, watch_flags);
144  if (wp_index == LLDB_INVALID_INDEX32)
145  return Status("Setting hardware watchpoint failed.");
146  m_watchpoint_index_map.insert({addr, wp_index});
147  return Status();
148 }
149 
151  auto wp = m_watchpoint_index_map.find(addr);
152  if (wp == m_watchpoint_index_map.end())
153  return Status();
154  uint32_t wp_index = wp->second;
155  m_watchpoint_index_map.erase(wp);
156  if (m_reg_context_up->ClearHardwareWatchpoint(wp_index))
157  return Status();
158  return Status("Clearing hardware watchpoint failed.");
159 }
160 
162  size_t size) {
163  return Status("unimplemented.");
164 }
165 
167  return Status("unimplemented.");
168 }
lldb_private::ProcessInstanceInfo
Definition: ProcessInfo.h:108
lldb_private::NativeThreadWindows::SetStopReason
void SetStopReason(ThreadStopInfo stop_info, std::string description)
Definition: NativeThreadWindows.cpp:95
lldb_private::HostThread::GetNativeThread
HostNativeThread & GetNativeThread()
Definition: HostThread.cpp:32
lldb_private::NativeThreadProtocol
Definition: NativeThreadProtocol.h:20
lldb_private::NativeRegisterContext::WriteRegisterFromUnsigned
Status WriteRegisterFromUnsigned(uint32_t reg, uint64_t uval)
Definition: NativeRegisterContext.cpp:211
lldb_private::HostThread
Definition: HostThread.h:29
lldb_private::NativeThreadWindows::SetHardwareBreakpoint
Status SetHardwareBreakpoint(lldb::addr_t addr, size_t size) override
Definition: NativeThreadWindows.cpp:161
lldb_private::NativeThreadWindows::DoStop
Status DoStop()
Definition: NativeThreadWindows.cpp:33
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:37
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:102
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:178
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:67
lldb_private::Log::Printf
void Printf(const char *format,...) __attribute__((format(printf
Prefer using LLDB_LOGF whenever possible.
Definition: Log.cpp:121
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:413
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:80
lldb::eStateLaunching
@ eStateLaunching
Process is in the process of launching.
Definition: lldb-enumerations.h:79
lldb_private::NativeProcessProtocol::GetID
lldb::pid_t GetID() const
Definition: NativeProcessProtocol.h:166
lldb_private::ProcessInfo::GetName
const char * GetName() const
Definition: ProcessInfo.cpp:43
lldb_private::NativeThreadWindows::m_reg_context_up
std::unique_ptr< NativeRegisterContextWindows > m_reg_context_up
Definition: NativeThreadWindows.h:60
lldb_private::GetLogIfAllCategoriesSet
Log * GetLogIfAllCategoriesSet(uint32_t mask)
Definition: Logging.cpp:58
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:150
lldb_private::NativeThreadWindows::GetRegisterContext
NativeRegisterContextWindows & GetRegisterContext() override
Definition: NativeThreadWindows.h:35
string
string(SUBSTRING ${p} 10 -1 pStripped) if($
Definition: Plugins/CMakeLists.txt:38
lldb_private::HostNativeThreadBase::GetSystemHandle
lldb::thread_t GetSystemHandle() const
Definition: HostNativeThreadBase.cpp:26
lldb::eStateUnloaded
@ eStateUnloaded
Process is object is valid, but not currently loaded.
Definition: lldb-enumerations.h:75
lldb_private::ThreadStopInfo
Definition: Debug.h:133
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:133
lldb_private::NativeProcessProtocol
Definition: NativeProcessProtocol.h:47
lldb_private::NativeThreadProtocol::GetID
lldb::tid_t GetID() const
Definition: NativeThreadProtocol.h:35
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:45
windows.h
lldb-forward.h
lldb_private::NativeProcessWindows
Definition: NativeProcessWindows.h:30
lldb_private::NativeThreadWindows::RemoveHardwareBreakpoint
Status RemoveHardwareBreakpoint(lldb::addr_t addr) override
Definition: NativeThreadWindows.cpp:166
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:86
lldb::StateType
StateType
Process and Thread States.
Definition: lldb-enumerations.h:73
lldb_private::Log
Definition: Log.h:49
lldb_private::NativeThreadWindows::m_host_thread
HostThread m_host_thread
Definition: NativeThreadWindows.h:66
lldb
Definition: SBAddress.h:15
LIBLLDB_LOG_THREAD
#define LIBLLDB_LOG_THREAD
Definition: Logging.h:16
lldb::eStateRunning
@ eStateRunning
Process or thread is running and can't be examined.
Definition: lldb-enumerations.h:85
HostThreadWindows.h
NativeThreadWindows.h