blob: 1fd7400bf800c29c49b0274c1905c352d0d585eb [file] [log] [blame]
Kamil Rytarowski1a3d19d2017-03-21 17:30:47 +00001//===-- NativeThreadNetBSD.cpp -------------------------------- -*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "NativeThreadNetBSD.h"
11#include "NativeRegisterContextNetBSD.h"
12
13#include "NativeProcessNetBSD.h"
14
Kamil Rytarowskif07a9992017-03-28 22:43:17 +000015#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"
Kamil Rytarowski36e23ec2017-04-18 12:53:35 +000019#include "lldb/Utility/LLDBAssert.h"
20
21#include <sstream>
Kamil Rytarowskif07a9992017-03-28 22:43:17 +000022
Kamil Rytarowski1a3d19d2017-03-21 17:30:47 +000023using namespace lldb;
24using namespace lldb_private;
25using namespace lldb_private::process_netbsd;
26
Pavel Labath82abefa2017-07-18 09:24:48 +000027NativeThreadNetBSD::NativeThreadNetBSD(NativeProcessNetBSD &process,
Kamil Rytarowski1a3d19d2017-03-21 17:30:47 +000028 lldb::tid_t tid)
Kamil Rytarowskif07a9992017-03-28 22:43:17 +000029 : NativeThreadProtocol(process, tid), m_state(StateType::eStateInvalid),
30 m_stop_info(), m_reg_context_sp(), m_stop_description() {}
31
32void 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);
36
37 SetStopped();
38
39 m_stop_info.reason = StopReason::eStopReasonSignal;
40 m_stop_info.details.signal.signo = signo;
41
42 m_stop_description.clear();
43 if (info) {
44 switch (signo) {
45 case SIGSEGV:
46 case SIGBUS:
47 case SIGFPE:
48 case SIGILL:
49 const auto reason = GetCrashReason(*info);
50 m_stop_description = GetCrashReasonString(reason, *info);
51 break;
52 }
53 }
54}
55
56void NativeThreadNetBSD::SetStoppedByBreakpoint() {
57 SetStopped();
58 m_stop_info.reason = StopReason::eStopReasonBreakpoint;
59 m_stop_info.details.signal.signo = SIGTRAP;
60}
61
Kamil Rytarowski3eef2b52017-03-30 20:25:29 +000062void NativeThreadNetBSD::SetStoppedByTrace() {
63 SetStopped();
64 m_stop_info.reason = StopReason::eStopReasonTrace;
65 m_stop_info.details.signal.signo = SIGTRAP;
66}
67
68void NativeThreadNetBSD::SetStoppedByExec() {
69 SetStopped();
70 m_stop_info.reason = StopReason::eStopReasonExec;
71 m_stop_info.details.signal.signo = SIGTRAP;
72}
73
Kamil Rytarowski36e23ec2017-04-18 12:53:35 +000074void NativeThreadNetBSD::SetStoppedByWatchpoint(uint32_t wp_index) {
75 SetStopped();
76
77 lldbassert(wp_index != LLDB_INVALID_INDEX32 && "wp_index cannot be invalid");
78
79 std::ostringstream ostr;
80 ostr << GetRegisterContext()->GetWatchpointAddress(wp_index) << " ";
81 ostr << wp_index;
82
83 ostr << " " << GetRegisterContext()->GetWatchpointHitAddress(wp_index);
84
85 m_stop_description = ostr.str();
86
87 m_stop_info.reason = StopReason::eStopReasonWatchpoint;
88 m_stop_info.details.signal.signo = SIGTRAP;
89}
90
Kamil Rytarowskif07a9992017-03-28 22:43:17 +000091void NativeThreadNetBSD::SetStopped() {
92 const StateType new_state = StateType::eStateStopped;
93 m_state = new_state;
94 m_stop_description.clear();
95}
96
97void NativeThreadNetBSD::SetRunning() {
98 m_state = StateType::eStateRunning;
99 m_stop_info.reason = StopReason::eStopReasonNone;
100}
101
Kamil Rytarowski3eef2b52017-03-30 20:25:29 +0000102void NativeThreadNetBSD::SetStepping() {
103 m_state = StateType::eStateStepping;
104 m_stop_info.reason = StopReason::eStopReasonNone;
105}
106
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000107std::string NativeThreadNetBSD::GetName() { return std::string(""); }
108
109lldb::StateType NativeThreadNetBSD::GetState() { return m_state; }
110
111bool NativeThreadNetBSD::GetStopReason(ThreadStopInfo &stop_info,
112 std::string &description) {
113 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_THREAD));
114
115 description.clear();
116
117 switch (m_state) {
118 case eStateStopped:
119 case eStateCrashed:
120 case eStateExited:
121 case eStateSuspended:
122 case eStateUnloaded:
123 stop_info = m_stop_info;
124 description = m_stop_description;
125
126 return true;
127
128 case eStateInvalid:
129 case eStateConnected:
130 case eStateAttaching:
131 case eStateLaunching:
132 case eStateRunning:
133 case eStateStepping:
134 case eStateDetached:
135 LLDB_LOG(log, "tid = {0} in state {1} cannot answer stop reason", GetID(),
136 StateAsCString(m_state));
137 return false;
138 }
139 llvm_unreachable("unhandled StateType!");
140}
141
142NativeRegisterContextSP NativeThreadNetBSD::GetRegisterContext() {
143 // Return the register context if we already created it.
144 if (m_reg_context_sp)
145 return m_reg_context_sp;
146
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000147 ArchSpec target_arch;
Pavel Labath82abefa2017-07-18 09:24:48 +0000148 if (!m_process.GetArchitecture(target_arch))
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000149 return NativeRegisterContextSP();
150
151 const uint32_t concrete_frame_idx = 0;
152 m_reg_context_sp.reset(
153 NativeRegisterContextNetBSD::CreateHostNativeRegisterContextNetBSD(
154 target_arch, *this, concrete_frame_idx));
155
156 return m_reg_context_sp;
157}
158
Zachary Turner97206d52017-05-12 04:51:55 +0000159Status NativeThreadNetBSD::SetWatchpoint(lldb::addr_t addr, size_t size,
160 uint32_t watch_flags, bool hardware) {
Kamil Rytarowski36e23ec2017-04-18 12:53:35 +0000161 if (!hardware)
Zachary Turner97206d52017-05-12 04:51:55 +0000162 return Status("not implemented");
Kamil Rytarowski36e23ec2017-04-18 12:53:35 +0000163 if (m_state == eStateLaunching)
Zachary Turner97206d52017-05-12 04:51:55 +0000164 return Status();
165 Status error = RemoveWatchpoint(addr);
Kamil Rytarowski36e23ec2017-04-18 12:53:35 +0000166 if (error.Fail())
167 return error;
168 NativeRegisterContextSP reg_ctx = GetRegisterContext();
169 uint32_t wp_index = reg_ctx->SetHardwareWatchpoint(addr, size, watch_flags);
170 if (wp_index == LLDB_INVALID_INDEX32)
Zachary Turner97206d52017-05-12 04:51:55 +0000171 return Status("Setting hardware watchpoint failed.");
Kamil Rytarowski36e23ec2017-04-18 12:53:35 +0000172 m_watchpoint_index_map.insert({addr, wp_index});
Zachary Turner97206d52017-05-12 04:51:55 +0000173 return Status();
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000174}
175
Zachary Turner97206d52017-05-12 04:51:55 +0000176Status NativeThreadNetBSD::RemoveWatchpoint(lldb::addr_t addr) {
Kamil Rytarowski36e23ec2017-04-18 12:53:35 +0000177 auto wp = m_watchpoint_index_map.find(addr);
178 if (wp == m_watchpoint_index_map.end())
Zachary Turner97206d52017-05-12 04:51:55 +0000179 return Status();
Kamil Rytarowski36e23ec2017-04-18 12:53:35 +0000180 uint32_t wp_index = wp->second;
181 m_watchpoint_index_map.erase(wp);
182 if (GetRegisterContext()->ClearHardwareWatchpoint(wp_index))
Zachary Turner97206d52017-05-12 04:51:55 +0000183 return Status();
184 return Status("Clearing hardware watchpoint failed.");
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000185}
186
Zachary Turner97206d52017-05-12 04:51:55 +0000187Status NativeThreadNetBSD::SetHardwareBreakpoint(lldb::addr_t addr,
188 size_t size) {
Kamil Rytarowski36e23ec2017-04-18 12:53:35 +0000189 if (m_state == eStateLaunching)
Zachary Turner97206d52017-05-12 04:51:55 +0000190 return Status();
Kamil Rytarowski36e23ec2017-04-18 12:53:35 +0000191
Zachary Turner97206d52017-05-12 04:51:55 +0000192 Status error = RemoveHardwareBreakpoint(addr);
Kamil Rytarowski36e23ec2017-04-18 12:53:35 +0000193 if (error.Fail())
194 return error;
195
196 NativeRegisterContextSP reg_ctx = GetRegisterContext();
197 uint32_t bp_index = reg_ctx->SetHardwareBreakpoint(addr, size);
198
199 if (bp_index == LLDB_INVALID_INDEX32)
Zachary Turner97206d52017-05-12 04:51:55 +0000200 return Status("Setting hardware breakpoint failed.");
Kamil Rytarowski36e23ec2017-04-18 12:53:35 +0000201
202 m_hw_break_index_map.insert({addr, bp_index});
Zachary Turner97206d52017-05-12 04:51:55 +0000203 return Status();
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000204}
205
Zachary Turner97206d52017-05-12 04:51:55 +0000206Status NativeThreadNetBSD::RemoveHardwareBreakpoint(lldb::addr_t addr) {
Kamil Rytarowski36e23ec2017-04-18 12:53:35 +0000207 auto bp = m_hw_break_index_map.find(addr);
208 if (bp == m_hw_break_index_map.end())
Zachary Turner97206d52017-05-12 04:51:55 +0000209 return Status();
Kamil Rytarowski36e23ec2017-04-18 12:53:35 +0000210
211 uint32_t bp_index = bp->second;
212 if (GetRegisterContext()->ClearHardwareBreakpoint(bp_index)) {
213 m_hw_break_index_map.erase(bp);
Zachary Turner97206d52017-05-12 04:51:55 +0000214 return Status();
Kamil Rytarowski36e23ec2017-04-18 12:53:35 +0000215 }
216
Zachary Turner97206d52017-05-12 04:51:55 +0000217 return Status("Clearing hardware breakpoint failed.");
Kamil Rytarowskif07a9992017-03-28 22:43:17 +0000218}