Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1 | //===-- NativeProcessProtocol.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 | |
Chaoren Lin | 2fe1d0a | 2015-02-03 01:51:38 +0000 | [diff] [blame] | 10 | #include "lldb/Host/common/NativeProcessProtocol.h" |
Todd Fiala | 511e5cd | 2014-09-11 23:29:14 +0000 | [diff] [blame] | 11 | #include "lldb/Host/Host.h" |
Chaoren Lin | 2fe1d0a | 2015-02-03 01:51:38 +0000 | [diff] [blame] | 12 | #include "lldb/Host/common/NativeRegisterContext.h" |
Chaoren Lin | 2fe1d0a | 2015-02-03 01:51:38 +0000 | [diff] [blame] | 13 | #include "lldb/Host/common/NativeThreadProtocol.h" |
| 14 | #include "lldb/Host/common/SoftwareBreakpoint.h" |
Todd Fiala | e77fce0 | 2016-09-04 00:18:56 +0000 | [diff] [blame] | 15 | #include "lldb/Utility/LLDBAssert.h" |
Zachary Turner | 6f9e690 | 2017-03-03 20:56:28 +0000 | [diff] [blame] | 16 | #include "lldb/Utility/Log.h" |
Pavel Labath | d821c99 | 2018-08-07 11:07:21 +0000 | [diff] [blame] | 17 | #include "lldb/Utility/State.h" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 18 | #include "lldb/lldb-enumerations.h" |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 19 | |
| 20 | using namespace lldb; |
| 21 | using namespace lldb_private; |
| 22 | |
| 23 | // ----------------------------------------------------------------------------- |
| 24 | // NativeProcessProtocol Members |
| 25 | // ----------------------------------------------------------------------------- |
| 26 | |
Pavel Labath | 96e600f | 2017-07-07 11:02:19 +0000 | [diff] [blame] | 27 | NativeProcessProtocol::NativeProcessProtocol(lldb::pid_t pid, int terminal_fd, |
| 28 | NativeDelegate &delegate) |
| 29 | : m_pid(pid), m_terminal_fd(terminal_fd) { |
| 30 | bool registered = RegisterNativeDelegate(delegate); |
| 31 | assert(registered); |
| 32 | (void)registered; |
| 33 | } |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 34 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 35 | lldb_private::Status NativeProcessProtocol::Interrupt() { |
| 36 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 37 | #if !defined(SIGSTOP) |
| 38 | error.SetErrorString("local host does not support signaling"); |
| 39 | return error; |
Todd Fiala | 511e5cd | 2014-09-11 23:29:14 +0000 | [diff] [blame] | 40 | #else |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 41 | return Signal(SIGSTOP); |
Todd Fiala | 511e5cd | 2014-09-11 23:29:14 +0000 | [diff] [blame] | 42 | #endif |
| 43 | } |
| 44 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 45 | Status NativeProcessProtocol::IgnoreSignals(llvm::ArrayRef<int> signals) { |
Pavel Labath | 4a705e7 | 2017-02-24 09:29:14 +0000 | [diff] [blame] | 46 | m_signals_to_ignore.clear(); |
| 47 | m_signals_to_ignore.insert(signals.begin(), signals.end()); |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 48 | return Status(); |
Pavel Labath | 4a705e7 | 2017-02-24 09:29:14 +0000 | [diff] [blame] | 49 | } |
| 50 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 51 | lldb_private::Status |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 52 | NativeProcessProtocol::GetMemoryRegionInfo(lldb::addr_t load_addr, |
| 53 | MemoryRegionInfo &range_info) { |
| 54 | // Default: not implemented. |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 55 | return Status("not implemented"); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 56 | } |
| 57 | |
Pavel Labath | 3508fc8 | 2017-06-19 12:47:50 +0000 | [diff] [blame] | 58 | llvm::Optional<WaitStatus> NativeProcessProtocol::GetExitStatus() { |
| 59 | if (m_state == lldb::eStateExited) |
| 60 | return m_exit_status; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 61 | |
Pavel Labath | 3508fc8 | 2017-06-19 12:47:50 +0000 | [diff] [blame] | 62 | return llvm::None; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 63 | } |
| 64 | |
Pavel Labath | 3508fc8 | 2017-06-19 12:47:50 +0000 | [diff] [blame] | 65 | bool NativeProcessProtocol::SetExitStatus(WaitStatus status, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 66 | bool bNotifyStateChange) { |
| 67 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
Pavel Labath | 3508fc8 | 2017-06-19 12:47:50 +0000 | [diff] [blame] | 68 | LLDB_LOG(log, "status = {0}, notify = {1}", status, bNotifyStateChange); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 69 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 70 | // Exit status already set |
| 71 | if (m_state == lldb::eStateExited) { |
Pavel Labath | 3508fc8 | 2017-06-19 12:47:50 +0000 | [diff] [blame] | 72 | if (m_exit_status) |
| 73 | LLDB_LOG(log, "exit status already set to {0}", *m_exit_status); |
| 74 | else |
| 75 | LLDB_LOG(log, "state is exited, but status not set"); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 76 | return false; |
| 77 | } |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 78 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 79 | m_state = lldb::eStateExited; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 80 | m_exit_status = status; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 81 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 82 | if (bNotifyStateChange) |
| 83 | SynchronouslyNotifyProcessStateChanged(lldb::eStateExited); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 84 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 85 | return true; |
| 86 | } |
| 87 | |
Pavel Labath | a5be48b | 2017-10-17 15:52:16 +0000 | [diff] [blame] | 88 | NativeThreadProtocol *NativeProcessProtocol::GetThreadAtIndex(uint32_t idx) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 89 | std::lock_guard<std::recursive_mutex> guard(m_threads_mutex); |
| 90 | if (idx < m_threads.size()) |
Pavel Labath | a5be48b | 2017-10-17 15:52:16 +0000 | [diff] [blame] | 91 | return m_threads[idx].get(); |
| 92 | return nullptr; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 93 | } |
| 94 | |
Pavel Labath | a5be48b | 2017-10-17 15:52:16 +0000 | [diff] [blame] | 95 | NativeThreadProtocol * |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 96 | NativeProcessProtocol::GetThreadByIDUnlocked(lldb::tid_t tid) { |
Pavel Labath | a5be48b | 2017-10-17 15:52:16 +0000 | [diff] [blame] | 97 | for (const auto &thread : m_threads) { |
| 98 | if (thread->GetID() == tid) |
| 99 | return thread.get(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 100 | } |
Pavel Labath | a5be48b | 2017-10-17 15:52:16 +0000 | [diff] [blame] | 101 | return nullptr; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Pavel Labath | a5be48b | 2017-10-17 15:52:16 +0000 | [diff] [blame] | 104 | NativeThreadProtocol *NativeProcessProtocol::GetThreadByID(lldb::tid_t tid) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 105 | std::lock_guard<std::recursive_mutex> guard(m_threads_mutex); |
| 106 | return GetThreadByIDUnlocked(tid); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 109 | bool NativeProcessProtocol::IsAlive() const { |
| 110 | return m_state != eStateDetached && m_state != eStateExited && |
| 111 | m_state != eStateInvalid && m_state != eStateUnloaded; |
Todd Fiala | 511e5cd | 2014-09-11 23:29:14 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 114 | const NativeWatchpointList::WatchpointMap & |
| 115 | NativeProcessProtocol::GetWatchpointMap() const { |
| 116 | return m_watchpoint_list.GetWatchpointMap(); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Omair Javaid | d5ffbad | 2017-02-24 13:27:31 +0000 | [diff] [blame] | 119 | llvm::Optional<std::pair<uint32_t, uint32_t>> |
| 120 | NativeProcessProtocol::GetHardwareDebugSupportInfo() const { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 121 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
| 122 | |
| 123 | // get any thread |
Pavel Labath | a5be48b | 2017-10-17 15:52:16 +0000 | [diff] [blame] | 124 | NativeThreadProtocol *thread( |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 125 | const_cast<NativeProcessProtocol *>(this)->GetThreadAtIndex(0)); |
Pavel Labath | a5be48b | 2017-10-17 15:52:16 +0000 | [diff] [blame] | 126 | if (!thread) { |
| 127 | LLDB_LOG(log, "failed to find a thread to grab a NativeRegisterContext!"); |
Omair Javaid | d5ffbad | 2017-02-24 13:27:31 +0000 | [diff] [blame] | 128 | return llvm::None; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Pavel Labath | d37349f | 2017-11-10 11:05:49 +0000 | [diff] [blame] | 131 | NativeRegisterContext ®_ctx = thread->GetRegisterContext(); |
| 132 | return std::make_pair(reg_ctx.NumSupportedHardwareBreakpoints(), |
| 133 | reg_ctx.NumSupportedHardwareWatchpoints()); |
Chaoren Lin | 18fe640 | 2015-02-03 01:51:47 +0000 | [diff] [blame] | 134 | } |
| 135 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 136 | Status NativeProcessProtocol::SetWatchpoint(lldb::addr_t addr, size_t size, |
| 137 | uint32_t watch_flags, |
| 138 | bool hardware) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 139 | // This default implementation assumes setting the watchpoint for the process |
| 140 | // will require setting the watchpoint for each of the threads. Furthermore, |
| 141 | // it will track watchpoints set for the process and will add them to each |
| 142 | // thread that is attached to via the (FIXME implement) OnThreadAttached () |
| 143 | // method. |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 144 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 145 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
| 146 | |
| 147 | // Update the thread list |
| 148 | UpdateThreads(); |
| 149 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 150 | // Keep track of the threads we successfully set the watchpoint for. If one |
| 151 | // of the thread watchpoint setting operations fails, back off and remove the |
| 152 | // watchpoint for all the threads that were successfully set so we get back |
| 153 | // to a consistent state. |
Pavel Labath | a5be48b | 2017-10-17 15:52:16 +0000 | [diff] [blame] | 154 | std::vector<NativeThreadProtocol *> watchpoint_established_threads; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 155 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 156 | // Tell each thread to set a watchpoint. In the event that hardware |
| 157 | // watchpoints are requested but the SetWatchpoint fails, try to set a |
| 158 | // software watchpoint as a fallback. It's conceivable that if there are |
| 159 | // more threads than hardware watchpoints available, some of the threads will |
| 160 | // fail to set hardware watchpoints while software ones may be available. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 161 | std::lock_guard<std::recursive_mutex> guard(m_threads_mutex); |
Pavel Labath | a5be48b | 2017-10-17 15:52:16 +0000 | [diff] [blame] | 162 | for (const auto &thread : m_threads) { |
| 163 | assert(thread && "thread list should not have a NULL thread!"); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 164 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 165 | Status thread_error = |
Pavel Labath | a5be48b | 2017-10-17 15:52:16 +0000 | [diff] [blame] | 166 | thread->SetWatchpoint(addr, size, watch_flags, hardware); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 167 | if (thread_error.Fail() && hardware) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 168 | // Try software watchpoints since we failed on hardware watchpoint |
| 169 | // setting and we may have just run out of hardware watchpoints. |
Pavel Labath | a5be48b | 2017-10-17 15:52:16 +0000 | [diff] [blame] | 170 | thread_error = thread->SetWatchpoint(addr, size, watch_flags, false); |
| 171 | if (thread_error.Success()) |
| 172 | LLDB_LOG(log, |
| 173 | "hardware watchpoint requested but software watchpoint set"); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 174 | } |
| 175 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 176 | if (thread_error.Success()) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 177 | // Remember that we set this watchpoint successfully in case we need to |
| 178 | // clear it later. |
Pavel Labath | a5be48b | 2017-10-17 15:52:16 +0000 | [diff] [blame] | 179 | watchpoint_established_threads.push_back(thread.get()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 180 | } else { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 181 | // Unset the watchpoint for each thread we successfully set so that we |
| 182 | // get back to a consistent state of "not set" for the watchpoint. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 183 | for (auto unwatch_thread_sp : watchpoint_established_threads) { |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 184 | Status remove_error = unwatch_thread_sp->RemoveWatchpoint(addr); |
Pavel Labath | a5be48b | 2017-10-17 15:52:16 +0000 | [diff] [blame] | 185 | if (remove_error.Fail()) |
| 186 | LLDB_LOG(log, "RemoveWatchpoint failed for pid={0}, tid={1}: {2}", |
| 187 | GetID(), unwatch_thread_sp->GetID(), remove_error); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 188 | } |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 189 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 190 | return thread_error; |
| 191 | } |
| 192 | } |
| 193 | return m_watchpoint_list.Add(addr, size, watch_flags, hardware); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 194 | } |
| 195 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 196 | Status NativeProcessProtocol::RemoveWatchpoint(lldb::addr_t addr) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 197 | // Update the thread list |
| 198 | UpdateThreads(); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 199 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 200 | Status overall_error; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 201 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 202 | std::lock_guard<std::recursive_mutex> guard(m_threads_mutex); |
Pavel Labath | a5be48b | 2017-10-17 15:52:16 +0000 | [diff] [blame] | 203 | for (const auto &thread : m_threads) { |
| 204 | assert(thread && "thread list should not have a NULL thread!"); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 205 | |
Pavel Labath | a5be48b | 2017-10-17 15:52:16 +0000 | [diff] [blame] | 206 | const Status thread_error = thread->RemoveWatchpoint(addr); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 207 | if (thread_error.Fail()) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 208 | // Keep track of the first thread error if any threads fail. We want to |
| 209 | // try to remove the watchpoint from every thread, though, even if one or |
| 210 | // more have errors. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 211 | if (!overall_error.Fail()) |
| 212 | overall_error = thread_error; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 213 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 214 | } |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 215 | const Status error = m_watchpoint_list.Remove(addr); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 216 | return overall_error.Fail() ? overall_error : error; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 217 | } |
| 218 | |
Omair Javaid | d5ffbad | 2017-02-24 13:27:31 +0000 | [diff] [blame] | 219 | const HardwareBreakpointMap & |
| 220 | NativeProcessProtocol::GetHardwareBreakpointMap() const { |
| 221 | return m_hw_breakpoints_map; |
| 222 | } |
| 223 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 224 | Status NativeProcessProtocol::SetHardwareBreakpoint(lldb::addr_t addr, |
| 225 | size_t size) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 226 | // This default implementation assumes setting a hardware breakpoint for this |
| 227 | // process will require setting same hardware breakpoint for each of its |
| 228 | // existing threads. New thread will do the same once created. |
Omair Javaid | d5ffbad | 2017-02-24 13:27:31 +0000 | [diff] [blame] | 229 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
| 230 | |
| 231 | // Update the thread list |
| 232 | UpdateThreads(); |
| 233 | |
| 234 | // Exit here if target does not have required hardware breakpoint capability. |
| 235 | auto hw_debug_cap = GetHardwareDebugSupportInfo(); |
| 236 | |
| 237 | if (hw_debug_cap == llvm::None || hw_debug_cap->first == 0 || |
| 238 | hw_debug_cap->first <= m_hw_breakpoints_map.size()) |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 239 | return Status("Target does not have required no of hardware breakpoints"); |
Omair Javaid | d5ffbad | 2017-02-24 13:27:31 +0000 | [diff] [blame] | 240 | |
| 241 | // Vector below stores all thread pointer for which we have we successfully |
| 242 | // set this hardware breakpoint. If any of the current process threads fails |
| 243 | // to set this hardware breakpoint then roll back and remove this breakpoint |
| 244 | // for all the threads that had already set it successfully. |
Pavel Labath | a5be48b | 2017-10-17 15:52:16 +0000 | [diff] [blame] | 245 | std::vector<NativeThreadProtocol *> breakpoint_established_threads; |
Omair Javaid | d5ffbad | 2017-02-24 13:27:31 +0000 | [diff] [blame] | 246 | |
| 247 | // Request to set a hardware breakpoint for each of current process threads. |
| 248 | std::lock_guard<std::recursive_mutex> guard(m_threads_mutex); |
Pavel Labath | a5be48b | 2017-10-17 15:52:16 +0000 | [diff] [blame] | 249 | for (const auto &thread : m_threads) { |
| 250 | assert(thread && "thread list should not have a NULL thread!"); |
Omair Javaid | d5ffbad | 2017-02-24 13:27:31 +0000 | [diff] [blame] | 251 | |
Pavel Labath | a5be48b | 2017-10-17 15:52:16 +0000 | [diff] [blame] | 252 | Status thread_error = thread->SetHardwareBreakpoint(addr, size); |
Omair Javaid | d5ffbad | 2017-02-24 13:27:31 +0000 | [diff] [blame] | 253 | if (thread_error.Success()) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 254 | // Remember that we set this breakpoint successfully in case we need to |
| 255 | // clear it later. |
Pavel Labath | a5be48b | 2017-10-17 15:52:16 +0000 | [diff] [blame] | 256 | breakpoint_established_threads.push_back(thread.get()); |
Omair Javaid | d5ffbad | 2017-02-24 13:27:31 +0000 | [diff] [blame] | 257 | } else { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 258 | // Unset the breakpoint for each thread we successfully set so that we |
| 259 | // get back to a consistent state of "not set" for this hardware |
| 260 | // breakpoint. |
Omair Javaid | d5ffbad | 2017-02-24 13:27:31 +0000 | [diff] [blame] | 261 | for (auto rollback_thread_sp : breakpoint_established_threads) { |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 262 | Status remove_error = |
| 263 | rollback_thread_sp->RemoveHardwareBreakpoint(addr); |
Pavel Labath | a5be48b | 2017-10-17 15:52:16 +0000 | [diff] [blame] | 264 | if (remove_error.Fail()) |
| 265 | LLDB_LOG(log, |
| 266 | "RemoveHardwareBreakpoint failed for pid={0}, tid={1}: {2}", |
| 267 | GetID(), rollback_thread_sp->GetID(), remove_error); |
Omair Javaid | d5ffbad | 2017-02-24 13:27:31 +0000 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | return thread_error; |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | // Register new hardware breakpoint into hardware breakpoints map of current |
| 275 | // process. |
| 276 | m_hw_breakpoints_map[addr] = {addr, size}; |
| 277 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 278 | return Status(); |
Omair Javaid | d5ffbad | 2017-02-24 13:27:31 +0000 | [diff] [blame] | 279 | } |
| 280 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 281 | Status NativeProcessProtocol::RemoveHardwareBreakpoint(lldb::addr_t addr) { |
Omair Javaid | d5ffbad | 2017-02-24 13:27:31 +0000 | [diff] [blame] | 282 | // Update the thread list |
| 283 | UpdateThreads(); |
| 284 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 285 | Status error; |
Omair Javaid | d5ffbad | 2017-02-24 13:27:31 +0000 | [diff] [blame] | 286 | |
| 287 | std::lock_guard<std::recursive_mutex> guard(m_threads_mutex); |
Pavel Labath | a5be48b | 2017-10-17 15:52:16 +0000 | [diff] [blame] | 288 | for (const auto &thread : m_threads) { |
| 289 | assert(thread && "thread list should not have a NULL thread!"); |
| 290 | error = thread->RemoveHardwareBreakpoint(addr); |
Omair Javaid | d5ffbad | 2017-02-24 13:27:31 +0000 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | // Also remove from hardware breakpoint map of current process. |
| 294 | m_hw_breakpoints_map.erase(addr); |
| 295 | |
| 296 | return error; |
| 297 | } |
| 298 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 299 | bool NativeProcessProtocol::RegisterNativeDelegate( |
| 300 | NativeDelegate &native_delegate) { |
| 301 | std::lock_guard<std::recursive_mutex> guard(m_delegates_mutex); |
| 302 | if (std::find(m_delegates.begin(), m_delegates.end(), &native_delegate) != |
| 303 | m_delegates.end()) |
| 304 | return false; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 305 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 306 | m_delegates.push_back(&native_delegate); |
| 307 | native_delegate.InitializeDelegate(this); |
| 308 | return true; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 309 | } |
| 310 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 311 | bool NativeProcessProtocol::UnregisterNativeDelegate( |
| 312 | NativeDelegate &native_delegate) { |
| 313 | std::lock_guard<std::recursive_mutex> guard(m_delegates_mutex); |
| 314 | |
| 315 | const auto initial_size = m_delegates.size(); |
| 316 | m_delegates.erase( |
| 317 | remove(m_delegates.begin(), m_delegates.end(), &native_delegate), |
| 318 | m_delegates.end()); |
| 319 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 320 | // We removed the delegate if the count of delegates shrank after removing |
| 321 | // all copies of the given native_delegate from the vector. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 322 | return m_delegates.size() < initial_size; |
| 323 | } |
| 324 | |
| 325 | void NativeProcessProtocol::SynchronouslyNotifyProcessStateChanged( |
| 326 | lldb::StateType state) { |
| 327 | Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
| 328 | |
| 329 | std::lock_guard<std::recursive_mutex> guard(m_delegates_mutex); |
| 330 | for (auto native_delegate : m_delegates) |
| 331 | native_delegate->ProcessStateChanged(this, state); |
| 332 | |
| 333 | if (log) { |
| 334 | if (!m_delegates.empty()) { |
| 335 | log->Printf("NativeProcessProtocol::%s: sent state notification [%s] " |
| 336 | "from process %" PRIu64, |
| 337 | __FUNCTION__, lldb_private::StateAsCString(state), GetID()); |
| 338 | } else { |
| 339 | log->Printf("NativeProcessProtocol::%s: would send state notification " |
| 340 | "[%s] from process %" PRIu64 ", but no delegates", |
| 341 | __FUNCTION__, lldb_private::StateAsCString(state), GetID()); |
| 342 | } |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | void NativeProcessProtocol::NotifyDidExec() { |
| 347 | Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
| 348 | if (log) |
| 349 | log->Printf("NativeProcessProtocol::%s - preparing to call delegates", |
| 350 | __FUNCTION__); |
| 351 | |
| 352 | { |
Saleem Abdulrasool | 16ff860 | 2016-05-18 01:59:10 +0000 | [diff] [blame] | 353 | std::lock_guard<std::recursive_mutex> guard(m_delegates_mutex); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 354 | for (auto native_delegate : m_delegates) |
| 355 | native_delegate->DidExec(this); |
| 356 | } |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 357 | } |
| 358 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 359 | Status NativeProcessProtocol::SetSoftwareBreakpoint(lldb::addr_t addr, |
| 360 | uint32_t size_hint) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 361 | Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_BREAKPOINTS)); |
| 362 | if (log) |
| 363 | log->Printf("NativeProcessProtocol::%s addr = 0x%" PRIx64, __FUNCTION__, |
| 364 | addr); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 365 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 366 | return m_breakpoint_list.AddRef( |
| 367 | addr, size_hint, false, |
| 368 | [this](lldb::addr_t addr, size_t size_hint, bool /* hardware */, |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 369 | NativeBreakpointSP &breakpoint_sp) -> Status { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 370 | return SoftwareBreakpoint::CreateSoftwareBreakpoint( |
| 371 | *this, addr, size_hint, breakpoint_sp); |
| 372 | }); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 373 | } |
| 374 | |
Pavel Labath | f8b825f | 2018-09-09 06:01:12 +0000 | [diff] [blame] | 375 | llvm::Expected<llvm::ArrayRef<uint8_t>> |
| 376 | NativeProcessProtocol::GetSoftwareBreakpointTrapOpcode(size_t size_hint) { |
| 377 | static const uint8_t g_aarch64_opcode[] = {0x00, 0x00, 0x20, 0xd4}; |
| 378 | static const uint8_t g_i386_opcode[] = {0xCC}; |
| 379 | static const uint8_t g_mips64_opcode[] = {0x00, 0x00, 0x00, 0x0d}; |
| 380 | static const uint8_t g_mips64el_opcode[] = {0x0d, 0x00, 0x00, 0x00}; |
| 381 | static const uint8_t g_s390x_opcode[] = {0x00, 0x01}; |
| 382 | static const uint8_t g_ppc64le_opcode[] = {0x08, 0x00, 0xe0, 0x7f}; // trap |
| 383 | |
| 384 | switch (GetArchitecture().GetMachine()) { |
| 385 | case llvm::Triple::aarch64: |
Pavel Labath | 4f54507 | 2018-09-09 08:42:00 +0000 | [diff] [blame^] | 386 | return llvm::makeArrayRef(g_aarch64_opcode); |
Pavel Labath | f8b825f | 2018-09-09 06:01:12 +0000 | [diff] [blame] | 387 | |
| 388 | case llvm::Triple::x86: |
| 389 | case llvm::Triple::x86_64: |
Pavel Labath | 4f54507 | 2018-09-09 08:42:00 +0000 | [diff] [blame^] | 390 | return llvm::makeArrayRef(g_i386_opcode); |
Pavel Labath | f8b825f | 2018-09-09 06:01:12 +0000 | [diff] [blame] | 391 | |
| 392 | case llvm::Triple::mips: |
| 393 | case llvm::Triple::mips64: |
Pavel Labath | 4f54507 | 2018-09-09 08:42:00 +0000 | [diff] [blame^] | 394 | return llvm::makeArrayRef(g_mips64_opcode); |
Pavel Labath | f8b825f | 2018-09-09 06:01:12 +0000 | [diff] [blame] | 395 | |
| 396 | case llvm::Triple::mipsel: |
| 397 | case llvm::Triple::mips64el: |
Pavel Labath | 4f54507 | 2018-09-09 08:42:00 +0000 | [diff] [blame^] | 398 | return llvm::makeArrayRef(g_mips64el_opcode); |
Pavel Labath | f8b825f | 2018-09-09 06:01:12 +0000 | [diff] [blame] | 399 | |
| 400 | case llvm::Triple::systemz: |
Pavel Labath | 4f54507 | 2018-09-09 08:42:00 +0000 | [diff] [blame^] | 401 | return llvm::makeArrayRef(g_s390x_opcode); |
Pavel Labath | f8b825f | 2018-09-09 06:01:12 +0000 | [diff] [blame] | 402 | |
| 403 | case llvm::Triple::ppc64le: |
Pavel Labath | 4f54507 | 2018-09-09 08:42:00 +0000 | [diff] [blame^] | 404 | return llvm::makeArrayRef(g_ppc64le_opcode); |
Pavel Labath | f8b825f | 2018-09-09 06:01:12 +0000 | [diff] [blame] | 405 | |
| 406 | default: |
| 407 | return llvm::createStringError(llvm::inconvertibleErrorCode(), |
| 408 | "CPU type not supported!"); |
| 409 | } |
| 410 | } |
| 411 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 412 | Status NativeProcessProtocol::RemoveBreakpoint(lldb::addr_t addr, |
| 413 | bool hardware) { |
Omair Javaid | d5ffbad | 2017-02-24 13:27:31 +0000 | [diff] [blame] | 414 | if (hardware) |
| 415 | return RemoveHardwareBreakpoint(addr); |
| 416 | else |
| 417 | return m_breakpoint_list.DecRef(addr); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 418 | } |
| 419 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 420 | Status NativeProcessProtocol::EnableBreakpoint(lldb::addr_t addr) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 421 | return m_breakpoint_list.EnableBreakpoint(addr); |
Todd Fiala | a9882ce | 2014-08-28 15:46:54 +0000 | [diff] [blame] | 422 | } |
| 423 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 424 | Status NativeProcessProtocol::DisableBreakpoint(lldb::addr_t addr) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 425 | return m_breakpoint_list.DisableBreakpoint(addr); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 426 | } |
| 427 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 428 | lldb::StateType NativeProcessProtocol::GetState() const { |
| 429 | std::lock_guard<std::recursive_mutex> guard(m_state_mutex); |
| 430 | return m_state; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 431 | } |
| 432 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 433 | void NativeProcessProtocol::SetState(lldb::StateType state, |
| 434 | bool notify_delegates) { |
| 435 | std::lock_guard<std::recursive_mutex> guard(m_state_mutex); |
| 436 | |
| 437 | if (state == m_state) |
| 438 | return; |
| 439 | |
| 440 | m_state = state; |
| 441 | |
| 442 | if (StateIsStoppedState(state, false)) { |
| 443 | ++m_stop_id; |
| 444 | |
| 445 | // Give process a chance to do any stop id bump processing, such as |
| 446 | // clearing cached data that is invalidated each time the process runs. |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 447 | // Note if/when we support some threads running, we'll end up needing to |
| 448 | // manage this per thread and per process. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 449 | DoStopIDBumped(m_stop_id); |
| 450 | } |
| 451 | |
| 452 | // Optionally notify delegates of the state change. |
| 453 | if (notify_delegates) |
| 454 | SynchronouslyNotifyProcessStateChanged(state); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 455 | } |
| 456 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 457 | uint32_t NativeProcessProtocol::GetStopID() const { |
| 458 | std::lock_guard<std::recursive_mutex> guard(m_state_mutex); |
| 459 | return m_stop_id; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 460 | } |
| 461 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 462 | void NativeProcessProtocol::DoStopIDBumped(uint32_t /* newBumpId */) { |
| 463 | // Default implementation does nothing. |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 464 | } |
| 465 | |
Pavel Labath | 96e600f | 2017-07-07 11:02:19 +0000 | [diff] [blame] | 466 | NativeProcessProtocol::Factory::~Factory() = default; |