1 //===-- NativeThreadFreeBSD.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 "NativeThreadFreeBSD.h"
10 #include "NativeRegisterContextFreeBSD.h"
12 #include "NativeProcessFreeBSD.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>
24 #include <sys/sysctl.h>
32 using namespace lldb_private
;
33 using namespace lldb_private::process_freebsd
;
35 NativeThreadFreeBSD::NativeThreadFreeBSD(NativeProcessFreeBSD
&process
,
37 : NativeThreadProtocol(process
, tid
), m_state(StateType::eStateInvalid
),
40 NativeRegisterContextFreeBSD::CreateHostNativeRegisterContextFreeBSD(
41 process
.GetArchitecture(), *this)),
42 m_stop_description() {}
44 Status
NativeThreadFreeBSD::Resume() {
45 Status ret
= NativeProcessFreeBSD::PtraceWrapper(PT_RESUME
, GetID());
48 ret
= NativeProcessFreeBSD::PtraceWrapper(PT_CLEARSTEP
, GetID());
49 // we can get EINVAL if the architecture in question does not support
50 // hardware single-stepping -- that's fine, we have nothing to clear
52 if (ret
.GetError() == EINVAL
)
59 Status
NativeThreadFreeBSD::SingleStep() {
60 Status ret
= NativeProcessFreeBSD::PtraceWrapper(PT_RESUME
, GetID());
63 ret
= NativeProcessFreeBSD::PtraceWrapper(PT_SETSTEP
, GetID());
69 Status
NativeThreadFreeBSD::Suspend() {
70 Status ret
= NativeProcessFreeBSD::PtraceWrapper(PT_SUSPEND
, GetID());
76 void NativeThreadFreeBSD::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 NativeThreadFreeBSD::SetStoppedByBreakpoint() {
101 m_stop_info
.reason
= StopReason::eStopReasonBreakpoint
;
102 m_stop_info
.signo
= SIGTRAP
;
105 void NativeThreadFreeBSD::SetStoppedByTrace() {
107 m_stop_info
.reason
= StopReason::eStopReasonTrace
;
108 m_stop_info
.signo
= SIGTRAP
;
111 void NativeThreadFreeBSD::SetStoppedByExec() {
113 m_stop_info
.reason
= StopReason::eStopReasonExec
;
114 m_stop_info
.signo
= SIGTRAP
;
117 void NativeThreadFreeBSD::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 NativeThreadFreeBSD::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 NativeThreadFreeBSD::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 NativeThreadFreeBSD::SetStoppedByVForkDone() {
155 m_stop_info
.reason
= StopReason::eStopReasonVForkDone
;
156 m_stop_info
.signo
= SIGTRAP
;
159 void NativeThreadFreeBSD::SetStoppedWithNoReason() {
162 m_stop_info
.reason
= StopReason::eStopReasonNone
;
163 m_stop_info
.signo
= 0;
166 void NativeThreadFreeBSD::SetStopped() {
167 const StateType new_state
= StateType::eStateStopped
;
169 m_stop_description
.clear();
172 void NativeThreadFreeBSD::SetRunning() {
173 m_state
= StateType::eStateRunning
;
174 m_stop_info
.reason
= StopReason::eStopReasonNone
;
177 void NativeThreadFreeBSD::SetStepping() {
178 m_state
= StateType::eStateStepping
;
179 m_stop_info
.reason
= StopReason::eStopReasonNone
;
182 std::string
NativeThreadFreeBSD::GetName() {
183 Log
*log
= GetLog(POSIXLog::Thread
);
185 std::vector
<struct kinfo_proc
> kp
;
186 int mib
[4] = {CTL_KERN
, KERN_PROC
, KERN_PROC_PID
| KERN_PROC_INC_THREAD
,
187 static_cast<int>(GetProcess().GetID())};
190 size_t len
= kp
.size() * sizeof(struct kinfo_proc
);
191 void *ptr
= len
== 0 ? nullptr : kp
.data();
192 int error
= ::sysctl(mib
, 4, ptr
, &len
, nullptr, 0);
193 if (ptr
== nullptr || (error
!= 0 && errno
== ENOMEM
)) {
194 kp
.resize(len
/ sizeof(struct kinfo_proc
));
199 LLDB_LOG(log
, "tid = {0} in state {1} failed to get thread name: {2}",
200 GetID(), m_state
, strerror(errno
));
202 kp
.resize(len
/ sizeof(struct kinfo_proc
));
206 for (auto &procinfo
: kp
) {
207 if (procinfo
.ki_tid
== static_cast<lwpid_t
>(GetID()))
208 return procinfo
.ki_tdname
;
214 lldb::StateType
NativeThreadFreeBSD::GetState() { return m_state
; }
216 bool NativeThreadFreeBSD::GetStopReason(ThreadStopInfo
&stop_info
,
217 std::string
&description
) {
218 Log
*log
= GetLog(POSIXLog::Thread
);
225 case eStateSuspended
:
227 stop_info
= m_stop_info
;
228 description
= m_stop_description
;
233 case eStateConnected
:
234 case eStateAttaching
:
235 case eStateLaunching
:
239 LLDB_LOG(log
, "tid = {0} in state {1} cannot answer stop reason", GetID(),
240 StateAsCString(m_state
));
243 llvm_unreachable("unhandled StateType!");
246 NativeRegisterContextFreeBSD
&NativeThreadFreeBSD::GetRegisterContext() {
247 assert(m_reg_context_up
);
248 return *m_reg_context_up
;
251 Status
NativeThreadFreeBSD::SetWatchpoint(lldb::addr_t addr
, size_t size
,
252 uint32_t watch_flags
, bool hardware
) {
253 assert(m_state
== eStateStopped
);
255 return Status("not implemented");
256 Status error
= RemoveWatchpoint(addr
);
260 GetRegisterContext().SetHardwareWatchpoint(addr
, size
, watch_flags
);
261 if (wp_index
== LLDB_INVALID_INDEX32
)
262 return Status("Setting hardware watchpoint failed.");
263 m_watchpoint_index_map
.insert({addr
, wp_index
});
267 Status
NativeThreadFreeBSD::RemoveWatchpoint(lldb::addr_t addr
) {
268 auto wp
= m_watchpoint_index_map
.find(addr
);
269 if (wp
== m_watchpoint_index_map
.end())
271 uint32_t wp_index
= wp
->second
;
272 m_watchpoint_index_map
.erase(wp
);
273 if (GetRegisterContext().ClearHardwareWatchpoint(wp_index
))
275 return Status("Clearing hardware watchpoint failed.");
278 Status
NativeThreadFreeBSD::SetHardwareBreakpoint(lldb::addr_t addr
,
280 assert(m_state
== eStateStopped
);
281 Status error
= RemoveHardwareBreakpoint(addr
);
285 uint32_t bp_index
= GetRegisterContext().SetHardwareBreakpoint(addr
, size
);
287 if (bp_index
== LLDB_INVALID_INDEX32
)
288 return Status("Setting hardware breakpoint failed.");
290 m_hw_break_index_map
.insert({addr
, bp_index
});
294 Status
NativeThreadFreeBSD::RemoveHardwareBreakpoint(lldb::addr_t addr
) {
295 auto bp
= m_hw_break_index_map
.find(addr
);
296 if (bp
== m_hw_break_index_map
.end())
299 uint32_t bp_index
= bp
->second
;
300 if (GetRegisterContext().ClearHardwareBreakpoint(bp_index
)) {
301 m_hw_break_index_map
.erase(bp
);
305 return Status("Clearing hardware breakpoint failed.");
309 NativeThreadFreeBSD::CopyWatchpointsFrom(NativeThreadFreeBSD
&source
) {
310 llvm::Error s
= GetRegisterContext().CopyHardwareWatchpointsFrom(
311 source
.GetRegisterContext());
313 m_watchpoint_index_map
= source
.m_watchpoint_index_map
;
314 m_hw_break_index_map
= source
.m_hw_break_index_map
;
319 llvm::Expected
<std::unique_ptr
<llvm::MemoryBuffer
>>
320 NativeThreadFreeBSD::GetSiginfo() const {
321 Log
*log
= GetLog(POSIXLog::Process
);
323 struct ptrace_lwpinfo info
;
324 const auto siginfo_err
= NativeProcessFreeBSD::PtraceWrapper(
325 PT_LWPINFO
, GetID(), &info
, sizeof(info
));
326 if (siginfo_err
.Fail()) {
327 LLDB_LOG(log
, "PT_LWPINFO failed {0}", siginfo_err
);
328 return siginfo_err
.ToError();
331 if (info
.pl_event
!= PL_EVENT_SIGNAL
)
332 return llvm::createStringError(llvm::inconvertibleErrorCode(),
333 "Thread not signaled");
334 if (!(info
.pl_flags
& PL_FLAG_SI
))
335 return llvm::createStringError(llvm::inconvertibleErrorCode(),
336 "No siginfo for thread");
338 return llvm::MemoryBuffer::getMemBufferCopy(
339 llvm::StringRef(reinterpret_cast<const char *>(&info
.pl_siginfo
),
340 sizeof(info
.pl_siginfo
)));