Kamil Rytarowski | 1a3d19d | 2017-03-21 17:30:47 +0000 | [diff] [blame] | 1 | //===-- 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 Rytarowski | f07a999 | 2017-03-28 22:43:17 +0000 | [diff] [blame] | 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" |
Kamil Rytarowski | 36e23ec | 2017-04-18 12:53:35 +0000 | [diff] [blame] | 19 | #include "lldb/Utility/LLDBAssert.h" |
| 20 | |
| 21 | #include <sstream> |
Kamil Rytarowski | f07a999 | 2017-03-28 22:43:17 +0000 | [diff] [blame] | 22 | |
Kamil Rytarowski | 1a3d19d | 2017-03-21 17:30:47 +0000 | [diff] [blame] | 23 | using namespace lldb; |
| 24 | using namespace lldb_private; |
| 25 | using namespace lldb_private::process_netbsd; |
| 26 | |
Pavel Labath | 82abefa | 2017-07-18 09:24:48 +0000 | [diff] [blame] | 27 | NativeThreadNetBSD::NativeThreadNetBSD(NativeProcessNetBSD &process, |
Kamil Rytarowski | 1a3d19d | 2017-03-21 17:30:47 +0000 | [diff] [blame] | 28 | lldb::tid_t tid) |
Kamil Rytarowski | f07a999 | 2017-03-28 22:43:17 +0000 | [diff] [blame] | 29 | : NativeThreadProtocol(process, tid), m_state(StateType::eStateInvalid), |
Pavel Labath | d37349f | 2017-11-10 11:05:49 +0000 | [diff] [blame^] | 30 | m_stop_info(), m_reg_context_up( |
| 31 | NativeRegisterContextNetBSD::CreateHostNativeRegisterContextNetBSD(process.GetArchitecture(), *this) |
| 32 | ), m_stop_description() {} |
Kamil Rytarowski | f07a999 | 2017-03-28 22:43:17 +0000 | [diff] [blame] | 33 | |
| 34 | void NativeThreadNetBSD::SetStoppedBySignal(uint32_t signo, |
| 35 | const siginfo_t *info) { |
| 36 | Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_THREAD)); |
| 37 | LLDB_LOG(log, "tid = {0} in called with signal {1}", GetID(), signo); |
| 38 | |
| 39 | SetStopped(); |
| 40 | |
| 41 | m_stop_info.reason = StopReason::eStopReasonSignal; |
| 42 | m_stop_info.details.signal.signo = signo; |
| 43 | |
| 44 | m_stop_description.clear(); |
| 45 | if (info) { |
| 46 | switch (signo) { |
| 47 | case SIGSEGV: |
| 48 | case SIGBUS: |
| 49 | case SIGFPE: |
| 50 | case SIGILL: |
| 51 | const auto reason = GetCrashReason(*info); |
| 52 | m_stop_description = GetCrashReasonString(reason, *info); |
| 53 | break; |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | void NativeThreadNetBSD::SetStoppedByBreakpoint() { |
| 59 | SetStopped(); |
| 60 | m_stop_info.reason = StopReason::eStopReasonBreakpoint; |
| 61 | m_stop_info.details.signal.signo = SIGTRAP; |
| 62 | } |
| 63 | |
Kamil Rytarowski | 3eef2b5 | 2017-03-30 20:25:29 +0000 | [diff] [blame] | 64 | void NativeThreadNetBSD::SetStoppedByTrace() { |
| 65 | SetStopped(); |
| 66 | m_stop_info.reason = StopReason::eStopReasonTrace; |
| 67 | m_stop_info.details.signal.signo = SIGTRAP; |
| 68 | } |
| 69 | |
| 70 | void NativeThreadNetBSD::SetStoppedByExec() { |
| 71 | SetStopped(); |
| 72 | m_stop_info.reason = StopReason::eStopReasonExec; |
| 73 | m_stop_info.details.signal.signo = SIGTRAP; |
| 74 | } |
| 75 | |
Kamil Rytarowski | 36e23ec | 2017-04-18 12:53:35 +0000 | [diff] [blame] | 76 | void NativeThreadNetBSD::SetStoppedByWatchpoint(uint32_t wp_index) { |
| 77 | SetStopped(); |
| 78 | |
| 79 | lldbassert(wp_index != LLDB_INVALID_INDEX32 && "wp_index cannot be invalid"); |
| 80 | |
| 81 | std::ostringstream ostr; |
Pavel Labath | d37349f | 2017-11-10 11:05:49 +0000 | [diff] [blame^] | 82 | ostr << GetRegisterContext().GetWatchpointAddress(wp_index) << " "; |
Kamil Rytarowski | 36e23ec | 2017-04-18 12:53:35 +0000 | [diff] [blame] | 83 | ostr << wp_index; |
| 84 | |
Pavel Labath | d37349f | 2017-11-10 11:05:49 +0000 | [diff] [blame^] | 85 | ostr << " " << GetRegisterContext().GetWatchpointHitAddress(wp_index); |
Kamil Rytarowski | 36e23ec | 2017-04-18 12:53:35 +0000 | [diff] [blame] | 86 | |
| 87 | m_stop_description = ostr.str(); |
| 88 | |
| 89 | m_stop_info.reason = StopReason::eStopReasonWatchpoint; |
| 90 | m_stop_info.details.signal.signo = SIGTRAP; |
| 91 | } |
| 92 | |
Kamil Rytarowski | f07a999 | 2017-03-28 22:43:17 +0000 | [diff] [blame] | 93 | void NativeThreadNetBSD::SetStopped() { |
| 94 | const StateType new_state = StateType::eStateStopped; |
| 95 | m_state = new_state; |
| 96 | m_stop_description.clear(); |
| 97 | } |
| 98 | |
| 99 | void NativeThreadNetBSD::SetRunning() { |
| 100 | m_state = StateType::eStateRunning; |
| 101 | m_stop_info.reason = StopReason::eStopReasonNone; |
| 102 | } |
| 103 | |
Kamil Rytarowski | 3eef2b5 | 2017-03-30 20:25:29 +0000 | [diff] [blame] | 104 | void NativeThreadNetBSD::SetStepping() { |
| 105 | m_state = StateType::eStateStepping; |
| 106 | m_stop_info.reason = StopReason::eStopReasonNone; |
| 107 | } |
| 108 | |
Kamil Rytarowski | f07a999 | 2017-03-28 22:43:17 +0000 | [diff] [blame] | 109 | std::string NativeThreadNetBSD::GetName() { return std::string(""); } |
| 110 | |
| 111 | lldb::StateType NativeThreadNetBSD::GetState() { return m_state; } |
| 112 | |
| 113 | bool NativeThreadNetBSD::GetStopReason(ThreadStopInfo &stop_info, |
| 114 | std::string &description) { |
| 115 | Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_THREAD)); |
| 116 | |
| 117 | description.clear(); |
| 118 | |
| 119 | switch (m_state) { |
| 120 | case eStateStopped: |
| 121 | case eStateCrashed: |
| 122 | case eStateExited: |
| 123 | case eStateSuspended: |
| 124 | case eStateUnloaded: |
| 125 | stop_info = m_stop_info; |
| 126 | description = m_stop_description; |
| 127 | |
| 128 | return true; |
| 129 | |
| 130 | case eStateInvalid: |
| 131 | case eStateConnected: |
| 132 | case eStateAttaching: |
| 133 | case eStateLaunching: |
| 134 | case eStateRunning: |
| 135 | case eStateStepping: |
| 136 | case eStateDetached: |
| 137 | LLDB_LOG(log, "tid = {0} in state {1} cannot answer stop reason", GetID(), |
| 138 | StateAsCString(m_state)); |
| 139 | return false; |
| 140 | } |
| 141 | llvm_unreachable("unhandled StateType!"); |
| 142 | } |
| 143 | |
Pavel Labath | d37349f | 2017-11-10 11:05:49 +0000 | [diff] [blame^] | 144 | NativeRegisterContext& NativeThreadNetBSD::GetRegisterContext() { |
| 145 | assert(m_reg_context_up); |
| 146 | return *m_reg_context_up; |
Kamil Rytarowski | f07a999 | 2017-03-28 22:43:17 +0000 | [diff] [blame] | 147 | } |
| 148 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 149 | Status NativeThreadNetBSD::SetWatchpoint(lldb::addr_t addr, size_t size, |
| 150 | uint32_t watch_flags, bool hardware) { |
Kamil Rytarowski | 36e23ec | 2017-04-18 12:53:35 +0000 | [diff] [blame] | 151 | if (!hardware) |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 152 | return Status("not implemented"); |
Kamil Rytarowski | 36e23ec | 2017-04-18 12:53:35 +0000 | [diff] [blame] | 153 | if (m_state == eStateLaunching) |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 154 | return Status(); |
| 155 | Status error = RemoveWatchpoint(addr); |
Kamil Rytarowski | 36e23ec | 2017-04-18 12:53:35 +0000 | [diff] [blame] | 156 | if (error.Fail()) |
| 157 | return error; |
Pavel Labath | d37349f | 2017-11-10 11:05:49 +0000 | [diff] [blame^] | 158 | uint32_t wp_index = GetRegisterContext().SetHardwareWatchpoint(addr, size, watch_flags); |
Kamil Rytarowski | 36e23ec | 2017-04-18 12:53:35 +0000 | [diff] [blame] | 159 | if (wp_index == LLDB_INVALID_INDEX32) |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 160 | return Status("Setting hardware watchpoint failed."); |
Kamil Rytarowski | 36e23ec | 2017-04-18 12:53:35 +0000 | [diff] [blame] | 161 | m_watchpoint_index_map.insert({addr, wp_index}); |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 162 | return Status(); |
Kamil Rytarowski | f07a999 | 2017-03-28 22:43:17 +0000 | [diff] [blame] | 163 | } |
| 164 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 165 | Status NativeThreadNetBSD::RemoveWatchpoint(lldb::addr_t addr) { |
Kamil Rytarowski | 36e23ec | 2017-04-18 12:53:35 +0000 | [diff] [blame] | 166 | auto wp = m_watchpoint_index_map.find(addr); |
| 167 | if (wp == m_watchpoint_index_map.end()) |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 168 | return Status(); |
Kamil Rytarowski | 36e23ec | 2017-04-18 12:53:35 +0000 | [diff] [blame] | 169 | uint32_t wp_index = wp->second; |
| 170 | m_watchpoint_index_map.erase(wp); |
Pavel Labath | d37349f | 2017-11-10 11:05:49 +0000 | [diff] [blame^] | 171 | if (GetRegisterContext().ClearHardwareWatchpoint(wp_index)) |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 172 | return Status(); |
| 173 | return Status("Clearing hardware watchpoint failed."); |
Kamil Rytarowski | f07a999 | 2017-03-28 22:43:17 +0000 | [diff] [blame] | 174 | } |
| 175 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 176 | Status NativeThreadNetBSD::SetHardwareBreakpoint(lldb::addr_t addr, |
| 177 | size_t size) { |
Kamil Rytarowski | 36e23ec | 2017-04-18 12:53:35 +0000 | [diff] [blame] | 178 | if (m_state == eStateLaunching) |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 179 | return Status(); |
Kamil Rytarowski | 36e23ec | 2017-04-18 12:53:35 +0000 | [diff] [blame] | 180 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 181 | Status error = RemoveHardwareBreakpoint(addr); |
Kamil Rytarowski | 36e23ec | 2017-04-18 12:53:35 +0000 | [diff] [blame] | 182 | if (error.Fail()) |
| 183 | return error; |
| 184 | |
Pavel Labath | d37349f | 2017-11-10 11:05:49 +0000 | [diff] [blame^] | 185 | uint32_t bp_index = GetRegisterContext().SetHardwareBreakpoint(addr, size); |
Kamil Rytarowski | 36e23ec | 2017-04-18 12:53:35 +0000 | [diff] [blame] | 186 | |
| 187 | if (bp_index == LLDB_INVALID_INDEX32) |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 188 | return Status("Setting hardware breakpoint failed."); |
Kamil Rytarowski | 36e23ec | 2017-04-18 12:53:35 +0000 | [diff] [blame] | 189 | |
| 190 | m_hw_break_index_map.insert({addr, bp_index}); |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 191 | return Status(); |
Kamil Rytarowski | f07a999 | 2017-03-28 22:43:17 +0000 | [diff] [blame] | 192 | } |
| 193 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 194 | Status NativeThreadNetBSD::RemoveHardwareBreakpoint(lldb::addr_t addr) { |
Kamil Rytarowski | 36e23ec | 2017-04-18 12:53:35 +0000 | [diff] [blame] | 195 | auto bp = m_hw_break_index_map.find(addr); |
| 196 | if (bp == m_hw_break_index_map.end()) |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 197 | return Status(); |
Kamil Rytarowski | 36e23ec | 2017-04-18 12:53:35 +0000 | [diff] [blame] | 198 | |
| 199 | uint32_t bp_index = bp->second; |
Pavel Labath | d37349f | 2017-11-10 11:05:49 +0000 | [diff] [blame^] | 200 | if (GetRegisterContext().ClearHardwareBreakpoint(bp_index)) { |
Kamil Rytarowski | 36e23ec | 2017-04-18 12:53:35 +0000 | [diff] [blame] | 201 | m_hw_break_index_map.erase(bp); |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 202 | return Status(); |
Kamil Rytarowski | 36e23ec | 2017-04-18 12:53:35 +0000 | [diff] [blame] | 203 | } |
| 204 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 205 | return Status("Clearing hardware breakpoint failed."); |
Kamil Rytarowski | f07a999 | 2017-03-28 22:43:17 +0000 | [diff] [blame] | 206 | } |