LLDB  mainline
NativeThreadNetBSD.cpp
Go to the documentation of this file.
1 //===-- NativeThreadNetBSD.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 "NativeThreadNetBSD.h"
11 
12 #include "NativeProcessNetBSD.h"
13 
18 #include "lldb/Utility/State.h"
19 #include "llvm/Support/Errno.h"
20 
21 // clang-format off
22 #include <sys/types.h>
23 #include <sys/ptrace.h>
24 // clang-format on
25 
26 #include <sstream>
27 
28 // clang-format off
29 #include <sys/types.h>
30 #include <sys/sysctl.h>
31 // clang-format on
32 
33 using namespace lldb;
34 using namespace lldb_private;
35 using namespace lldb_private::process_netbsd;
36 
37 NativeThreadNetBSD::NativeThreadNetBSD(NativeProcessNetBSD &process,
38  lldb::tid_t tid)
39  : NativeThreadProtocol(process, tid), m_state(StateType::eStateInvalid),
40  m_stop_info(), m_reg_context_up(
41 NativeRegisterContextNetBSD::CreateHostNativeRegisterContextNetBSD(process.GetArchitecture(), *this)
42 ), m_stop_description() {}
43 
46  nullptr, GetID());
47  if (!ret.Success())
48  return ret;
50  nullptr, GetID());
51  if (ret.Success())
52  SetRunning();
53  return ret;
54 }
55 
58  nullptr, GetID());
59  if (!ret.Success())
60  return ret;
62  nullptr, GetID());
63  if (ret.Success())
64  SetStepping();
65  return ret;
66 }
67 
70  nullptr, GetID());
71  if (ret.Success())
72  SetStopped();
73  return ret;
74 }
75 
77  const siginfo_t *info) {
78  Log *log = GetLog(POSIXLog::Thread);
79  LLDB_LOG(log, "tid = {0} in called with signal {1}", GetID(), signo);
80 
81  SetStopped();
82 
84  m_stop_info.signo = signo;
85 
86  m_stop_description.clear();
87  if (info) {
88  switch (signo) {
89  case SIGSEGV:
90  case SIGBUS:
91  case SIGFPE:
92  case SIGILL:
93  const auto reason = GetCrashReason(*info);
95  break;
96  }
97  }
98 }
99 
101  SetStopped();
104 }
105 
107  SetStopped();
110 }
111 
113  SetStopped();
116 }
117 
119  lldbassert(wp_index != LLDB_INVALID_INDEX32 && "wp_index cannot be invalid");
120 
121  std::ostringstream ostr;
122  ostr << GetRegisterContext().GetWatchpointAddress(wp_index) << " ";
123  ostr << wp_index;
124 
125  ostr << " " << GetRegisterContext().GetWatchpointHitAddress(wp_index);
126 
127  SetStopped();
128  m_stop_description = ostr.str();
131 }
132 
134  lldb::tid_t child_tid) {
135  SetStopped();
136 
139  m_stop_info.details.fork.child_pid = child_pid;
140  m_stop_info.details.fork.child_tid = child_tid;
141 }
142 
144  lldb::tid_t child_tid) {
145  SetStopped();
146 
149  m_stop_info.details.fork.child_pid = child_pid;
150  m_stop_info.details.fork.child_tid = child_tid;
151 }
152 
154  SetStopped();
155 
158 }
159 
161  SetStopped();
162 
164  m_stop_info.signo = 0;
165 }
166 
168  const StateType new_state = StateType::eStateStopped;
169  m_state = new_state;
170  m_stop_description.clear();
171 }
172 
176 }
177 
181 }
182 
184  Log *log = GetLog(POSIXLog::Thread);
185 
186 #ifdef PT_LWPSTATUS
187  struct ptrace_lwpstatus info = {};
188  info.pl_lwpid = m_tid;
190  PT_LWPSTATUS, static_cast<int>(m_process.GetID()), &info, sizeof(info));
191  if (error.Fail()) {
192  return "";
193  }
194  return info.pl_name;
195 #else
196  std::vector<struct kinfo_lwp> infos;
197  int mib[5] = {CTL_KERN, KERN_LWP, static_cast<int>(m_process.GetID()),
198  sizeof(struct kinfo_lwp), 0};
199  size_t size;
200 
201  if (::sysctl(mib, 5, nullptr, &size, nullptr, 0) == -1 || size == 0) {
202  LLDB_LOG(log, "sysctl() for LWP info size failed: {0}",
203  llvm::sys::StrError());
204  return "";
205  }
206 
207  mib[4] = size / sizeof(size_t);
208  infos.resize(size / sizeof(struct kinfo_lwp));
209 
210  if (sysctl(mib, 5, infos.data(), &size, NULL, 0) == -1 || size == 0) {
211  LLDB_LOG(log, "sysctl() for LWP info failed: {0}", llvm::sys::StrError());
212  return "";
213  }
214 
215  size_t nlwps = size / sizeof(struct kinfo_lwp);
216  for (size_t i = 0; i < nlwps; i++) {
217  if (static_cast<lldb::tid_t>(infos[i].l_lid) == m_tid) {
218  return infos[i].l_name;
219  }
220  }
221 
222  LLDB_LOG(log, "unable to find lwp {0} in LWP infos", m_tid);
223  return "";
224 #endif
225 }
226 
228 
230  std::string &description) {
231  Log *log = GetLog(POSIXLog::Thread);
232  description.clear();
233 
234  switch (m_state) {
235  case eStateStopped:
236  case eStateCrashed:
237  case eStateExited:
238  case eStateSuspended:
239  case eStateUnloaded:
240  stop_info = m_stop_info;
241  description = m_stop_description;
242 
243  return true;
244 
245  case eStateInvalid:
246  case eStateConnected:
247  case eStateAttaching:
248  case eStateLaunching:
249  case eStateRunning:
250  case eStateStepping:
251  case eStateDetached:
252  LLDB_LOG(log, "tid = {0} in state {1} cannot answer stop reason", GetID(),
254  return false;
255  }
256  llvm_unreachable("unhandled StateType!");
257 }
258 
260  assert(m_reg_context_up);
261  return *m_reg_context_up;
262 }
263 
265  uint32_t watch_flags, bool hardware) {
266  assert(m_state == eStateStopped);
267  if (!hardware)
268  return Status("not implemented");
269  Status error = RemoveWatchpoint(addr);
270  if (error.Fail())
271  return error;
272  uint32_t wp_index =
273  GetRegisterContext().SetHardwareWatchpoint(addr, size, watch_flags);
274  if (wp_index == LLDB_INVALID_INDEX32)
275  return Status("Setting hardware watchpoint failed.");
276  m_watchpoint_index_map.insert({addr, wp_index});
277  return Status();
278 }
279 
281  auto wp = m_watchpoint_index_map.find(addr);
282  if (wp == m_watchpoint_index_map.end())
283  return Status();
284  uint32_t wp_index = wp->second;
285  m_watchpoint_index_map.erase(wp);
286  if (GetRegisterContext().ClearHardwareWatchpoint(wp_index))
287  return Status();
288  return Status("Clearing hardware watchpoint failed.");
289 }
290 
292  size_t size) {
293  assert(m_state == eStateStopped);
295  if (error.Fail())
296  return error;
297 
298  uint32_t bp_index = GetRegisterContext().SetHardwareBreakpoint(addr, size);
299 
300  if (bp_index == LLDB_INVALID_INDEX32)
301  return Status("Setting hardware breakpoint failed.");
302 
303  m_hw_break_index_map.insert({addr, bp_index});
304  return Status();
305 }
306 
308  auto bp = m_hw_break_index_map.find(addr);
309  if (bp == m_hw_break_index_map.end())
310  return Status();
311 
312  uint32_t bp_index = bp->second;
313  if (GetRegisterContext().ClearHardwareBreakpoint(bp_index)) {
314  m_hw_break_index_map.erase(bp);
315  return Status();
316  }
317 
318  return Status("Clearing hardware breakpoint failed.");
319 }
320 
324  source.GetRegisterContext());
325  if (!s) {
328  }
329  return s;
330 }
lldb_private::process_netbsd::NativeThreadNetBSD::SetStoppedByVFork
void SetStoppedByVFork(lldb::pid_t child_pid, lldb::tid_t child_tid)
Definition: NativeThreadNetBSD.cpp:143
lldb_private::NativeThreadProtocol
Definition: NativeThreadProtocol.h:24
lldb::eStopReasonVFork
@ eStopReasonVFork
Definition: lldb-enumerations.h:253
lldb_private::process_netbsd::NativeThreadNetBSD::RemoveHardwareBreakpoint
Status RemoveHardwareBreakpoint(lldb::addr_t addr) override
Definition: NativeThreadNetBSD.cpp:307
lldb_private::NativeRegisterContext::SetHardwareBreakpoint
virtual uint32_t SetHardwareBreakpoint(lldb::addr_t addr, size_t size)
Definition: NativeRegisterContext.cpp:249
lldb_private::process_netbsd::NativeThreadNetBSD::SetRunning
void SetRunning()
Definition: NativeThreadNetBSD.cpp:173
lldb::eStateAttaching
@ eStateAttaching
Process is currently trying to attach.
Definition: lldb-enumerations.h:78
lldb_private::process_netbsd::NativeThreadNetBSD::SetStepping
void SetStepping()
Definition: NativeThreadNetBSD.cpp:178
lldb_private::process_netbsd::NativeThreadNetBSD::GetStopReason
bool GetStopReason(ThreadStopInfo &stop_info, std::string &description) override
Definition: NativeThreadNetBSD.cpp:229
lldb_private::StateAsCString
const char * StateAsCString(lldb::StateType state)
Converts a StateType to a C string.
Definition: State.cpp:14
lldb_private::ThreadStopInfo::details
union lldb_private::ThreadStopInfo::@13 details
lldb_private::process_netbsd::NativeThreadNetBSD::SetStoppedByWatchpoint
void SetStoppedByWatchpoint(uint32_t wp_index)
Definition: NativeThreadNetBSD.cpp:118
RegisterValue.h
lldb_private::process_netbsd::NativeRegisterContextNetBSD::CopyHardwareWatchpointsFrom
virtual llvm::Error CopyHardwareWatchpointsFrom(NativeRegisterContextNetBSD &source)=0
lldb::addr_t
uint64_t addr_t
Definition: lldb-types.h:83
lldb_private::process_netbsd::NativeThreadNetBSD::RemoveWatchpoint
Status RemoveWatchpoint(lldb::addr_t addr) override
Definition: NativeThreadNetBSD.cpp:280
lldb::eStopReasonTrace
@ eStopReasonTrace
Definition: lldb-enumerations.h:242
lldb_private::process_netbsd::NativeProcessNetBSD
Definition: NativeProcessNetBSD.h:28
lldb_private::process_netbsd::NativeThreadNetBSD::m_hw_break_index_map
WatchpointIndexMap m_hw_break_index_map
Definition: NativeThreadNetBSD.h:79
lldb_private::process_netbsd::NativeThreadNetBSD::SetStoppedByFork
void SetStoppedByFork(lldb::pid_t child_pid, lldb::tid_t child_tid)
Definition: NativeThreadNetBSD.cpp:133
lldb::eStateExited
@ eStateExited
Process has exited and can't be examined.
Definition: lldb-enumerations.h:90
lldb_private::process_netbsd::NativeThreadNetBSD::GetRegisterContext
NativeRegisterContextNetBSD & GetRegisterContext() override
Definition: NativeThreadNetBSD.cpp:259
lldb_private::ThreadStopInfo::fork
struct lldb_private::ThreadStopInfo::@13::@15 fork
lldb::eStateStopped
@ eStateStopped
Process or thread is stopped and can be examined.
Definition: lldb-enumerations.h:84
lldb_private::process_netbsd::NativeThreadNetBSD::SetStoppedByTrace
void SetStoppedByTrace()
Definition: NativeThreadNetBSD.cpp:106
error
static llvm::raw_ostream & error(Stream &strm)
Definition: CommandReturnObject.cpp:17
lldb_private::Status::Success
bool Success() const
Test for success condition.
Definition: Status.cpp:287
lldb::eStateCrashed
@ eStateCrashed
Process or thread has crashed and can be examined.
Definition: lldb-enumerations.h:88
LLDBAssert.h
lldb_private::NativeRegisterContext::GetWatchpointHitAddress
virtual lldb::addr_t GetWatchpointHitAddress(uint32_t wp_index)
Definition: NativeRegisterContext.cpp:309
lldb_private::process_netbsd::NativeThreadNetBSD::CopyWatchpointsFrom
llvm::Error CopyWatchpointsFrom(NativeThreadNetBSD &source)
Definition: NativeThreadNetBSD.cpp:322
lldb::eStopReasonWatchpoint
@ eStopReasonWatchpoint
Definition: lldb-enumerations.h:244
lldb::eStopReasonNone
@ eStopReasonNone
Definition: lldb-enumerations.h:241
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::process_netbsd::NativeThreadNetBSD::SetWatchpoint
Status SetWatchpoint(lldb::addr_t addr, size_t size, uint32_t watch_flags, bool hardware) override
Definition: NativeThreadNetBSD.cpp:264
NativeRegisterContextNetBSD.h
lldb_private::process_netbsd::NativeThreadNetBSD::SetStoppedByVForkDone
void SetStoppedByVForkDone()
Definition: NativeThreadNetBSD.cpp:153
SIGTRAP
#define SIGTRAP
Definition: windows/PosixApi.h:30
lldb::eStateLaunching
@ eStateLaunching
Process is in the process of launching.
Definition: lldb-enumerations.h:79
NativeThreadNetBSD.h
lldb_private::process_netbsd::NativeThreadNetBSD::SetHardwareBreakpoint
Status SetHardwareBreakpoint(lldb::addr_t addr, size_t size) override
Definition: NativeThreadNetBSD.cpp:291
lldb_private::ThreadStopInfo::reason
lldb::StopReason reason
Definition: Debug.h:132
lldb_private::NativeProcessProtocol::GetID
lldb::pid_t GetID() const
Definition: NativeProcessProtocol.h:183
lldb_private::process_netbsd::NativeThreadNetBSD::m_stop_description
std::string m_stop_description
Definition: NativeThreadNetBSD.h:76
lldb_private::process_netbsd::NativeRegisterContextNetBSD
Definition: NativeRegisterContextNetBSD.h:21
ProcessPOSIXLog.h
lldb::eStateDetached
@ eStateDetached
Process has been detached and can't be examined.
Definition: lldb-enumerations.h:89
lldb_private::process_netbsd::NativeThreadNetBSD::SingleStep
Status SingleStep()
Definition: NativeThreadNetBSD.cpp:56
string
string(SUBSTRING ${p} 10 -1 pStripped) if($
Definition: Plugins/CMakeLists.txt:40
lldb_private::process_netbsd
Definition: NativeProcessNetBSD.h:20
lldb::eStateUnloaded
@ eStateUnloaded
Process is object is valid, but not currently loaded.
Definition: lldb-enumerations.h:75
lldb_private::process_netbsd::NativeThreadNetBSD::Resume
Status Resume()
Definition: NativeThreadNetBSD.cpp:44
lldb_private::ThreadStopInfo
Definition: Debug.h:131
lldb_private::process_netbsd::NativeThreadNetBSD::m_reg_context_up
std::unique_ptr< NativeRegisterContextNetBSD > m_reg_context_up
Definition: NativeThreadNetBSD.h:75
lldb_private::process_netbsd::NativeThreadNetBSD
Definition: NativeThreadNetBSD.h:25
lldb_private::process_netbsd::NativeThreadNetBSD::m_watchpoint_index_map
WatchpointIndexMap m_watchpoint_index_map
Definition: NativeThreadNetBSD.h:78
lldb_private::process_netbsd::NativeThreadNetBSD::SetStopped
void SetStopped()
Definition: NativeThreadNetBSD.cpp:167
lldb::eStateStepping
@ eStateStepping
Process or thread is in the process of stepping and can not be examined.
Definition: lldb-enumerations.h:86
lldbassert
#define lldbassert(x)
Definition: LLDBAssert.h:15
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::eStopReasonSignal
@ eStopReasonSignal
Definition: lldb-enumerations.h:245
GetCrashReasonString
std::string GetCrashReasonString(CrashReason reason, const siginfo_t &info)
Definition: CrashReason.cpp:144
lldb::eStopReasonExec
@ eStopReasonExec
Program was re-exec'ed.
Definition: lldb-enumerations.h:247
lldb_private::Status
Definition: Status.h:44
uint32_t
lldb_private::process_netbsd::NativeThreadNetBSD::Suspend
Status Suspend()
Definition: NativeThreadNetBSD.cpp:68
lldb_private::process_netbsd::NativeThreadNetBSD::SetStoppedWithNoReason
void SetStoppedWithNoReason()
Definition: NativeThreadNetBSD.cpp:160
lldb::pid_t
uint64_t pid_t
Definition: lldb-types.h:85
lldb_private::process_netbsd::NativeThreadNetBSD::SetStoppedBySignal
void SetStoppedBySignal(uint32_t signo, const siginfo_t *info=nullptr)
Definition: NativeThreadNetBSD.cpp:76
lldb_private::NativeThreadProtocol::GetID
lldb::tid_t GetID() const
Definition: NativeThreadProtocol.h:39
lldb_private::process_netbsd::NativeThreadNetBSD::GetState
lldb::StateType GetState() override
Definition: NativeThreadNetBSD.cpp:227
lldb_private::process_netbsd::NativeThreadNetBSD::m_stop_info
ThreadStopInfo m_stop_info
Definition: NativeThreadNetBSD.h:74
lldb_private::process_netbsd::NativeThreadNetBSD::m_state
lldb::StateType m_state
Definition: NativeThreadNetBSD.h:73
lldb_private::ThreadStopInfo::signo
uint32_t signo
Definition: Debug.h:133
lldb::eStopReasonBreakpoint
@ eStopReasonBreakpoint
Definition: lldb-enumerations.h:243
LLDB_LOG
#define LLDB_LOG(log,...)
The LLDB_LOG* macros defined below are the way to emit log messages.
Definition: Log.h:336
lldb_private::POSIXLog::Thread
@ Thread
lldb_private::process_netbsd::NativeProcessNetBSD::PtraceWrapper
static Status PtraceWrapper(int req, lldb::pid_t pid, void *addr=nullptr, int data=0, int *result=nullptr)
Definition: NativeProcessNetBSD.cpp:384
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
Error
llvm::Error Error
Definition: UdtRecordCompleter.cpp:30
lldb_private::NativeThreadProtocol::m_process
NativeProcessProtocol & m_process
Definition: NativeThreadProtocol.h:60
lldb_private::process_netbsd::NativeThreadNetBSD::SetStoppedByBreakpoint
void SetStoppedByBreakpoint()
Definition: NativeThreadNetBSD.cpp:100
lldb::eStateInvalid
@ eStateInvalid
Definition: lldb-enumerations.h:74
State.h
lldb_private::process_netbsd::NativeThreadNetBSD::SetStoppedByExec
void SetStoppedByExec()
Definition: NativeThreadNetBSD.cpp:112
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
NativeProcessNetBSD.h
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_private::NativeRegisterContext::SetHardwareWatchpoint
virtual uint32_t SetHardwareWatchpoint(lldb::addr_t addr, size_t size, uint32_t watch_flags)
Definition: NativeRegisterContext.cpp:270
lldb::eStopReasonVForkDone
@ eStopReasonVForkDone
Definition: lldb-enumerations.h:254
lldb
Definition: SBAddress.h:15
lldb::eStateRunning
@ eStateRunning
Process or thread is running and can't be examined.
Definition: lldb-enumerations.h:85
lldb::eStopReasonFork
@ eStopReasonFork
Definition: lldb-enumerations.h:252
lldb_private::NativeRegisterContext::GetWatchpointAddress
virtual lldb::addr_t GetWatchpointAddress(uint32_t wp_index)
Definition: NativeRegisterContext.cpp:305
lldb::tid_t
uint64_t tid_t
Definition: lldb-types.h:86
CrashReason.h
lldb_private::NativeThreadProtocol::m_tid
lldb::tid_t m_tid
Definition: NativeThreadProtocol.h:61
lldb_private::process_netbsd::NativeThreadNetBSD::GetName
std::string GetName() override
Definition: NativeThreadNetBSD.cpp:183
GetCrashReason
CrashReason GetCrashReason(const siginfo_t &info)
Definition: CrashReason.cpp:341