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