1 //===-- NativeThreadNetBSD.cpp -------------------------------- -*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "NativeThreadNetBSD.h"
11 #include "NativeRegisterContextNetBSD.h"
13 #include "NativeProcessNetBSD.h"
15 #include "Plugins/Process/POSIX/CrashReason.h"
16 #include "Plugins/Process/POSIX/ProcessPOSIXLog.h"
17 #include "lldb/Core/RegisterValue.h"
18 #include "lldb/Core/State.h"
19 #include "lldb/Utility/LLDBAssert.h"
24 using namespace lldb_private;
25 using namespace lldb_private::process_netbsd;
27 NativeThreadNetBSD::NativeThreadNetBSD(NativeProcessNetBSD &process,
29 : NativeThreadProtocol(process, tid), m_state(StateType::eStateInvalid),
30 m_stop_info(), m_reg_context_sp(), m_stop_description() {}
32 void NativeThreadNetBSD::SetStoppedBySignal(uint32_t signo,
33 const siginfo_t *info) {
34 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_THREAD));
35 LLDB_LOG(log, "tid = {0} in called with signal {1}", GetID(), signo);
39 m_stop_info.reason = StopReason::eStopReasonSignal;
40 m_stop_info.details.signal.signo = signo;
42 m_stop_description.clear();
49 const auto reason = GetCrashReason(*info);
50 m_stop_description = GetCrashReasonString(reason, *info);
56 void NativeThreadNetBSD::SetStoppedByBreakpoint() {
58 m_stop_info.reason = StopReason::eStopReasonBreakpoint;
59 m_stop_info.details.signal.signo = SIGTRAP;
62 void NativeThreadNetBSD::SetStoppedByTrace() {
64 m_stop_info.reason = StopReason::eStopReasonTrace;
65 m_stop_info.details.signal.signo = SIGTRAP;
68 void NativeThreadNetBSD::SetStoppedByExec() {
70 m_stop_info.reason = StopReason::eStopReasonExec;
71 m_stop_info.details.signal.signo = SIGTRAP;
74 void NativeThreadNetBSD::SetStoppedByWatchpoint(uint32_t wp_index) {
77 lldbassert(wp_index != LLDB_INVALID_INDEX32 && "wp_index cannot be invalid");
79 std::ostringstream ostr;
80 ostr << GetRegisterContext()->GetWatchpointAddress(wp_index) << " ";
83 ostr << " " << GetRegisterContext()->GetWatchpointHitAddress(wp_index);
85 m_stop_description = ostr.str();
87 m_stop_info.reason = StopReason::eStopReasonWatchpoint;
88 m_stop_info.details.signal.signo = SIGTRAP;
91 void NativeThreadNetBSD::SetStopped() {
92 const StateType new_state = StateType::eStateStopped;
94 m_stop_description.clear();
97 void NativeThreadNetBSD::SetRunning() {
98 m_state = StateType::eStateRunning;
99 m_stop_info.reason = StopReason::eStopReasonNone;
102 void NativeThreadNetBSD::SetStepping() {
103 m_state = StateType::eStateStepping;
104 m_stop_info.reason = StopReason::eStopReasonNone;
107 std::string NativeThreadNetBSD::GetName() { return std::string(""); }
109 lldb::StateType NativeThreadNetBSD::GetState() { return m_state; }
111 bool NativeThreadNetBSD::GetStopReason(ThreadStopInfo &stop_info,
112 std::string &description) {
113 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_THREAD));
121 case eStateSuspended:
123 stop_info = m_stop_info;
124 description = m_stop_description;
129 case eStateConnected:
130 case eStateAttaching:
131 case eStateLaunching:
135 LLDB_LOG(log, "tid = {0} in state {1} cannot answer stop reason", GetID(),
136 StateAsCString(m_state));
139 llvm_unreachable("unhandled StateType!");
142 NativeRegisterContextSP NativeThreadNetBSD::GetRegisterContext() {
143 // Return the register context if we already created it.
144 if (m_reg_context_sp)
145 return m_reg_context_sp;
147 ArchSpec target_arch;
148 if (!m_process.GetArchitecture(target_arch))
149 return NativeRegisterContextSP();
151 const uint32_t concrete_frame_idx = 0;
152 m_reg_context_sp.reset(
153 NativeRegisterContextNetBSD::CreateHostNativeRegisterContextNetBSD(
154 target_arch, *this, concrete_frame_idx));
156 return m_reg_context_sp;
159 Status NativeThreadNetBSD::SetWatchpoint(lldb::addr_t addr, size_t size,
160 uint32_t watch_flags, bool hardware) {
162 return Status("not implemented");
163 if (m_state == eStateLaunching)
165 Status error = RemoveWatchpoint(addr);
168 NativeRegisterContextSP reg_ctx = GetRegisterContext();
169 uint32_t wp_index = reg_ctx->SetHardwareWatchpoint(addr, size, watch_flags);
170 if (wp_index == LLDB_INVALID_INDEX32)
171 return Status("Setting hardware watchpoint failed.");
172 m_watchpoint_index_map.insert({addr, wp_index});
176 Status NativeThreadNetBSD::RemoveWatchpoint(lldb::addr_t addr) {
177 auto wp = m_watchpoint_index_map.find(addr);
178 if (wp == m_watchpoint_index_map.end())
180 uint32_t wp_index = wp->second;
181 m_watchpoint_index_map.erase(wp);
182 if (GetRegisterContext()->ClearHardwareWatchpoint(wp_index))
184 return Status("Clearing hardware watchpoint failed.");
187 Status NativeThreadNetBSD::SetHardwareBreakpoint(lldb::addr_t addr,
189 if (m_state == eStateLaunching)
192 Status error = RemoveHardwareBreakpoint(addr);
196 NativeRegisterContextSP reg_ctx = GetRegisterContext();
197 uint32_t bp_index = reg_ctx->SetHardwareBreakpoint(addr, size);
199 if (bp_index == LLDB_INVALID_INDEX32)
200 return Status("Setting hardware breakpoint failed.");
202 m_hw_break_index_map.insert({addr, bp_index});
206 Status NativeThreadNetBSD::RemoveHardwareBreakpoint(lldb::addr_t addr) {
207 auto bp = m_hw_break_index_map.find(addr);
208 if (bp == m_hw_break_index_map.end())
211 uint32_t bp_index = bp->second;
212 if (GetRegisterContext()->ClearHardwareBreakpoint(bp_index)) {
213 m_hw_break_index_map.erase(bp);
217 return Status("Clearing hardware breakpoint failed.");