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