1 //===-- NativeThreadNetBSD.cpp --------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "NativeThreadNetBSD.h"
10 #include "NativeRegisterContextNetBSD.h"
12 #include "NativeProcessNetBSD.h"
14 #include "Plugins/Process/POSIX/CrashReason.h"
15 #include "Plugins/Process/POSIX/ProcessPOSIXLog.h"
16 #include "lldb/Utility/LLDBAssert.h"
17 #include "lldb/Utility/RegisterValue.h"
18 #include "lldb/Utility/State.h"
19 #include "llvm/Support/Errno.h"
22 #include <sys/types.h>
23 #include <sys/ptrace.h>
29 #include <sys/types.h>
30 #include <sys/sysctl.h>
34 using namespace lldb_private
;
35 using namespace lldb_private::process_netbsd
;
37 NativeThreadNetBSD::NativeThreadNetBSD(NativeProcessNetBSD
&process
,
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() {}
44 Status
NativeThreadNetBSD::Resume() {
45 Status ret
= NativeProcessNetBSD::PtraceWrapper(PT_RESUME
, m_process
.GetID(),
49 ret
= NativeProcessNetBSD::PtraceWrapper(PT_CLEARSTEP
, m_process
.GetID(),
56 Status
NativeThreadNetBSD::SingleStep() {
57 Status ret
= NativeProcessNetBSD::PtraceWrapper(PT_RESUME
, m_process
.GetID(),
61 ret
= NativeProcessNetBSD::PtraceWrapper(PT_SETSTEP
, m_process
.GetID(),
68 Status
NativeThreadNetBSD::Suspend() {
69 Status ret
= NativeProcessNetBSD::PtraceWrapper(PT_SUSPEND
, m_process
.GetID(),
76 void NativeThreadNetBSD::SetStoppedBySignal(uint32_t signo
,
77 const siginfo_t
*info
) {
78 Log
*log
= GetLog(POSIXLog::Thread
);
79 LLDB_LOG(log
, "tid = {0} in called with signal {1}", GetID(), signo
);
83 m_stop_info
.reason
= StopReason::eStopReasonSignal
;
84 m_stop_info
.signo
= signo
;
86 m_stop_description
.clear();
93 m_stop_description
= GetCrashReasonString(*info
);
99 void NativeThreadNetBSD::SetStoppedByBreakpoint() {
101 m_stop_info
.reason
= StopReason::eStopReasonBreakpoint
;
102 m_stop_info
.signo
= SIGTRAP
;
105 void NativeThreadNetBSD::SetStoppedByTrace() {
107 m_stop_info
.reason
= StopReason::eStopReasonTrace
;
108 m_stop_info
.signo
= SIGTRAP
;
111 void NativeThreadNetBSD::SetStoppedByExec() {
113 m_stop_info
.reason
= StopReason::eStopReasonExec
;
114 m_stop_info
.signo
= SIGTRAP
;
117 void NativeThreadNetBSD::SetStoppedByWatchpoint(uint32_t wp_index
) {
118 lldbassert(wp_index
!= LLDB_INVALID_INDEX32
&& "wp_index cannot be invalid");
120 std::ostringstream ostr
;
121 ostr
<< GetRegisterContext().GetWatchpointAddress(wp_index
) << " ";
124 ostr
<< " " << GetRegisterContext().GetWatchpointHitAddress(wp_index
);
127 m_stop_description
= ostr
.str();
128 m_stop_info
.reason
= StopReason::eStopReasonWatchpoint
;
129 m_stop_info
.signo
= SIGTRAP
;
132 void NativeThreadNetBSD::SetStoppedByFork(lldb::pid_t child_pid
,
133 lldb::tid_t child_tid
) {
136 m_stop_info
.reason
= StopReason::eStopReasonFork
;
137 m_stop_info
.signo
= SIGTRAP
;
138 m_stop_info
.details
.fork
.child_pid
= child_pid
;
139 m_stop_info
.details
.fork
.child_tid
= child_tid
;
142 void NativeThreadNetBSD::SetStoppedByVFork(lldb::pid_t child_pid
,
143 lldb::tid_t child_tid
) {
146 m_stop_info
.reason
= StopReason::eStopReasonVFork
;
147 m_stop_info
.signo
= SIGTRAP
;
148 m_stop_info
.details
.fork
.child_pid
= child_pid
;
149 m_stop_info
.details
.fork
.child_tid
= child_tid
;
152 void NativeThreadNetBSD::SetStoppedByVForkDone() {
155 m_stop_info
.reason
= StopReason::eStopReasonVForkDone
;
156 m_stop_info
.signo
= SIGTRAP
;
159 void NativeThreadNetBSD::SetStoppedWithNoReason() {
162 m_stop_info
.reason
= StopReason::eStopReasonNone
;
163 m_stop_info
.signo
= 0;
166 void NativeThreadNetBSD::SetStopped() {
167 const StateType new_state
= StateType::eStateStopped
;
169 m_stop_description
.clear();
172 void NativeThreadNetBSD::SetRunning() {
173 m_state
= StateType::eStateRunning
;
174 m_stop_info
.reason
= StopReason::eStopReasonNone
;
177 void NativeThreadNetBSD::SetStepping() {
178 m_state
= StateType::eStateStepping
;
179 m_stop_info
.reason
= StopReason::eStopReasonNone
;
182 std::string
NativeThreadNetBSD::GetName() {
183 Log
*log
= GetLog(POSIXLog::Thread
);
186 struct ptrace_lwpstatus info
= {};
187 info
.pl_lwpid
= m_tid
;
188 Status error
= NativeProcessNetBSD::PtraceWrapper(
189 PT_LWPSTATUS
, static_cast<int>(m_process
.GetID()), &info
, sizeof(info
));
195 std::vector
<struct kinfo_lwp
> infos
;
196 int mib
[5] = {CTL_KERN
, KERN_LWP
, static_cast<int>(m_process
.GetID()),
197 sizeof(struct kinfo_lwp
), 0};
200 if (::sysctl(mib
, 5, nullptr, &size
, nullptr, 0) == -1 || size
== 0) {
201 LLDB_LOG(log
, "sysctl() for LWP info size failed: {0}",
202 llvm::sys::StrError());
206 mib
[4] = size
/ sizeof(size_t);
207 infos
.resize(size
/ sizeof(struct kinfo_lwp
));
209 if (sysctl(mib
, 5, infos
.data(), &size
, NULL
, 0) == -1 || size
== 0) {
210 LLDB_LOG(log
, "sysctl() for LWP info failed: {0}", llvm::sys::StrError());
214 size_t nlwps
= size
/ sizeof(struct kinfo_lwp
);
215 for (size_t i
= 0; i
< nlwps
; i
++) {
216 if (static_cast<lldb::tid_t
>(infos
[i
].l_lid
) == m_tid
) {
217 return infos
[i
].l_name
;
221 LLDB_LOG(log
, "unable to find lwp {0} in LWP infos", m_tid
);
226 lldb::StateType
NativeThreadNetBSD::GetState() { return m_state
; }
228 bool NativeThreadNetBSD::GetStopReason(ThreadStopInfo
&stop_info
,
229 std::string
&description
) {
230 Log
*log
= GetLog(POSIXLog::Thread
);
237 case eStateSuspended
:
239 stop_info
= m_stop_info
;
240 description
= m_stop_description
;
245 case eStateConnected
:
246 case eStateAttaching
:
247 case eStateLaunching
:
251 LLDB_LOG(log
, "tid = {0} in state {1} cannot answer stop reason", GetID(),
252 StateAsCString(m_state
));
255 llvm_unreachable("unhandled StateType!");
258 NativeRegisterContextNetBSD
&NativeThreadNetBSD::GetRegisterContext() {
259 assert(m_reg_context_up
);
260 return *m_reg_context_up
;
263 Status
NativeThreadNetBSD::SetWatchpoint(lldb::addr_t addr
, size_t size
,
264 uint32_t watch_flags
, bool hardware
) {
265 assert(m_state
== eStateStopped
);
267 return Status("not implemented");
268 Status error
= RemoveWatchpoint(addr
);
272 GetRegisterContext().SetHardwareWatchpoint(addr
, size
, watch_flags
);
273 if (wp_index
== LLDB_INVALID_INDEX32
)
274 return Status("Setting hardware watchpoint failed.");
275 m_watchpoint_index_map
.insert({addr
, wp_index
});
279 Status
NativeThreadNetBSD::RemoveWatchpoint(lldb::addr_t addr
) {
280 auto wp
= m_watchpoint_index_map
.find(addr
);
281 if (wp
== m_watchpoint_index_map
.end())
283 uint32_t wp_index
= wp
->second
;
284 m_watchpoint_index_map
.erase(wp
);
285 if (GetRegisterContext().ClearHardwareWatchpoint(wp_index
))
287 return Status("Clearing hardware watchpoint failed.");
290 Status
NativeThreadNetBSD::SetHardwareBreakpoint(lldb::addr_t addr
,
292 assert(m_state
== eStateStopped
);
293 Status error
= RemoveHardwareBreakpoint(addr
);
297 uint32_t bp_index
= GetRegisterContext().SetHardwareBreakpoint(addr
, size
);
299 if (bp_index
== LLDB_INVALID_INDEX32
)
300 return Status("Setting hardware breakpoint failed.");
302 m_hw_break_index_map
.insert({addr
, bp_index
});
306 Status
NativeThreadNetBSD::RemoveHardwareBreakpoint(lldb::addr_t addr
) {
307 auto bp
= m_hw_break_index_map
.find(addr
);
308 if (bp
== m_hw_break_index_map
.end())
311 uint32_t bp_index
= bp
->second
;
312 if (GetRegisterContext().ClearHardwareBreakpoint(bp_index
)) {
313 m_hw_break_index_map
.erase(bp
);
317 return Status("Clearing hardware breakpoint failed.");
321 NativeThreadNetBSD::CopyWatchpointsFrom(NativeThreadNetBSD
&source
) {
322 llvm::Error s
= GetRegisterContext().CopyHardwareWatchpointsFrom(
323 source
.GetRegisterContext());
325 m_watchpoint_index_map
= source
.m_watchpoint_index_map
;
326 m_hw_break_index_map
= source
.m_hw_break_index_map
;