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 | |
| 49 | lldb_private::Error |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 50 | NativeProcessProtocol::GetMemoryRegionInfo(lldb::addr_t load_addr, |
| 51 | MemoryRegionInfo &range_info) { |
| 52 | // Default: not implemented. |
| 53 | return Error("not implemented"); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 54 | } |
| 55 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 56 | bool NativeProcessProtocol::GetExitStatus(ExitType *exit_type, int *status, |
| 57 | std::string &exit_description) { |
| 58 | if (m_state == lldb::eStateExited) { |
| 59 | *exit_type = m_exit_type; |
| 60 | *status = m_exit_status; |
| 61 | exit_description = m_exit_description; |
| 62 | return true; |
| 63 | } |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 64 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 65 | *status = 0; |
| 66 | return false; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 69 | bool NativeProcessProtocol::SetExitStatus(ExitType exit_type, int status, |
| 70 | const char *exit_description, |
| 71 | bool bNotifyStateChange) { |
| 72 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
| 73 | if (log) |
| 74 | log->Printf("NativeProcessProtocol::%s(%d, %d, %s, %s) called", |
| 75 | __FUNCTION__, exit_type, status, |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 76 | exit_description ? exit_description : "nullptr", |
| 77 | bNotifyStateChange ? "true" : "false"); |
| 78 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 79 | // Exit status already set |
| 80 | if (m_state == lldb::eStateExited) { |
| 81 | if (log) |
| 82 | log->Printf("NativeProcessProtocol::%s exit status already set to %d, " |
| 83 | "ignoring new set to %d", |
| 84 | __FUNCTION__, m_exit_status, status); |
| 85 | return false; |
| 86 | } |
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 | m_state = lldb::eStateExited; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 89 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 90 | m_exit_type = exit_type; |
| 91 | m_exit_status = status; |
| 92 | if (exit_description && exit_description[0]) |
| 93 | m_exit_description = exit_description; |
| 94 | else |
| 95 | m_exit_description.clear(); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 96 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 97 | if (bNotifyStateChange) |
| 98 | SynchronouslyNotifyProcessStateChanged(lldb::eStateExited); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 99 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 100 | return true; |
| 101 | } |
| 102 | |
| 103 | NativeThreadProtocolSP NativeProcessProtocol::GetThreadAtIndex(uint32_t idx) { |
| 104 | std::lock_guard<std::recursive_mutex> guard(m_threads_mutex); |
| 105 | if (idx < m_threads.size()) |
| 106 | return m_threads[idx]; |
| 107 | return NativeThreadProtocolSP(); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | NativeThreadProtocolSP |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 111 | NativeProcessProtocol::GetThreadByIDUnlocked(lldb::tid_t tid) { |
| 112 | for (auto thread_sp : m_threads) { |
| 113 | if (thread_sp->GetID() == tid) |
| 114 | return thread_sp; |
| 115 | } |
| 116 | return NativeThreadProtocolSP(); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 119 | NativeThreadProtocolSP NativeProcessProtocol::GetThreadByID(lldb::tid_t tid) { |
| 120 | std::lock_guard<std::recursive_mutex> guard(m_threads_mutex); |
| 121 | return GetThreadByIDUnlocked(tid); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 124 | bool NativeProcessProtocol::IsAlive() const { |
| 125 | return m_state != eStateDetached && m_state != eStateExited && |
| 126 | m_state != eStateInvalid && m_state != eStateUnloaded; |
Todd Fiala | 511e5cd | 2014-09-11 23:29:14 +0000 | [diff] [blame] | 127 | } |
| 128 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 129 | bool NativeProcessProtocol::GetByteOrder(lldb::ByteOrder &byte_order) const { |
| 130 | ArchSpec process_arch; |
| 131 | if (!GetArchitecture(process_arch)) |
| 132 | return false; |
| 133 | byte_order = process_arch.GetByteOrder(); |
| 134 | return true; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 135 | } |
| 136 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 137 | const NativeWatchpointList::WatchpointMap & |
| 138 | NativeProcessProtocol::GetWatchpointMap() const { |
| 139 | return m_watchpoint_list.GetWatchpointMap(); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 140 | } |
| 141 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 142 | uint32_t NativeProcessProtocol::GetMaxWatchpoints() const { |
| 143 | // This default implementation will return the number of |
| 144 | // *hardware* breakpoints available. MacOSX and other OS |
| 145 | // implementations that support software breakpoints will want to |
| 146 | // override this correctly for their implementation. |
| 147 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
| 148 | |
| 149 | // get any thread |
| 150 | NativeThreadProtocolSP thread_sp( |
| 151 | const_cast<NativeProcessProtocol *>(this)->GetThreadAtIndex(0)); |
| 152 | if (!thread_sp) { |
| 153 | if (log) |
| 154 | log->Warning("NativeProcessProtocol::%s (): failed to find a thread to " |
| 155 | "grab a NativeRegisterContext!", |
| 156 | __FUNCTION__); |
| 157 | return 0; |
| 158 | } |
| 159 | |
| 160 | NativeRegisterContextSP reg_ctx_sp(thread_sp->GetRegisterContext()); |
| 161 | if (!reg_ctx_sp) { |
| 162 | if (log) |
| 163 | log->Warning("NativeProcessProtocol::%s (): failed to get a " |
| 164 | "RegisterContextNativeProcess from the first thread!", |
| 165 | __FUNCTION__); |
| 166 | return 0; |
| 167 | } |
| 168 | |
| 169 | return reg_ctx_sp->NumSupportedHardwareWatchpoints(); |
Chaoren Lin | 18fe640 | 2015-02-03 01:51:47 +0000 | [diff] [blame] | 170 | } |
| 171 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 172 | Error NativeProcessProtocol::SetWatchpoint(lldb::addr_t addr, size_t size, |
| 173 | uint32_t watch_flags, |
| 174 | bool hardware) { |
| 175 | // This default implementation assumes setting the watchpoint for |
| 176 | // the process will require setting the watchpoint for each of the |
| 177 | // threads. Furthermore, it will track watchpoints set for the |
| 178 | // process and will add them to each thread that is attached to |
| 179 | // via the (FIXME implement) OnThreadAttached () method. |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 180 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 181 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
| 182 | |
| 183 | // Update the thread list |
| 184 | UpdateThreads(); |
| 185 | |
| 186 | // Keep track of the threads we successfully set the watchpoint |
| 187 | // for. If one of the thread watchpoint setting operations fails, |
| 188 | // back off and remove the watchpoint for all the threads that |
| 189 | // were successfully set so we get back to a consistent state. |
| 190 | std::vector<NativeThreadProtocolSP> watchpoint_established_threads; |
| 191 | |
| 192 | // Tell each thread to set a watchpoint. In the event that |
| 193 | // hardware watchpoints are requested but the SetWatchpoint fails, |
| 194 | // try to set a software watchpoint as a fallback. It's |
| 195 | // conceivable that if there are more threads than hardware |
| 196 | // watchpoints available, some of the threads will fail to set |
| 197 | // hardware watchpoints while software ones may be available. |
| 198 | std::lock_guard<std::recursive_mutex> guard(m_threads_mutex); |
| 199 | for (auto thread_sp : m_threads) { |
| 200 | assert(thread_sp && "thread list should not have a NULL thread!"); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 201 | if (!thread_sp) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 202 | continue; |
| 203 | |
| 204 | Error thread_error = |
| 205 | thread_sp->SetWatchpoint(addr, size, watch_flags, hardware); |
| 206 | if (thread_error.Fail() && hardware) { |
| 207 | // Try software watchpoints since we failed on hardware watchpoint setting |
| 208 | // and we may have just run out of hardware watchpoints. |
| 209 | thread_error = thread_sp->SetWatchpoint(addr, size, watch_flags, false); |
| 210 | if (thread_error.Success()) { |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 211 | if (log) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 212 | log->Warning( |
| 213 | "hardware watchpoint requested but software watchpoint set"); |
| 214 | } |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 215 | } |
| 216 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 217 | if (thread_error.Success()) { |
| 218 | // Remember that we set this watchpoint successfully in |
| 219 | // case we need to clear it later. |
| 220 | watchpoint_established_threads.push_back(thread_sp); |
| 221 | } else { |
| 222 | // Unset the watchpoint for each thread we successfully |
| 223 | // set so that we get back to a consistent state of "not |
| 224 | // set" for the watchpoint. |
| 225 | for (auto unwatch_thread_sp : watchpoint_established_threads) { |
| 226 | Error remove_error = unwatch_thread_sp->RemoveWatchpoint(addr); |
| 227 | if (remove_error.Fail() && log) { |
| 228 | log->Warning("NativeProcessProtocol::%s (): RemoveWatchpoint failed " |
| 229 | "for pid=%" PRIu64 ", tid=%" PRIu64 ": %s", |
| 230 | __FUNCTION__, GetID(), unwatch_thread_sp->GetID(), |
| 231 | remove_error.AsCString()); |
| 232 | } |
| 233 | } |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 234 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 235 | return thread_error; |
| 236 | } |
| 237 | } |
| 238 | return m_watchpoint_list.Add(addr, size, watch_flags, hardware); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 239 | } |
| 240 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 241 | Error NativeProcessProtocol::RemoveWatchpoint(lldb::addr_t addr) { |
| 242 | // Update the thread list |
| 243 | UpdateThreads(); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 244 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 245 | Error overall_error; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 246 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 247 | std::lock_guard<std::recursive_mutex> guard(m_threads_mutex); |
| 248 | for (auto thread_sp : m_threads) { |
| 249 | assert(thread_sp && "thread list should not have a NULL thread!"); |
| 250 | if (!thread_sp) |
| 251 | continue; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 252 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 253 | const Error thread_error = thread_sp->RemoveWatchpoint(addr); |
| 254 | if (thread_error.Fail()) { |
| 255 | // Keep track of the first thread error if any threads |
| 256 | // fail. We want to try to remove the watchpoint from |
| 257 | // every thread, though, even if one or more have errors. |
| 258 | if (!overall_error.Fail()) |
| 259 | overall_error = thread_error; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 260 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 261 | } |
| 262 | const Error error = m_watchpoint_list.Remove(addr); |
| 263 | return overall_error.Fail() ? overall_error : error; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 264 | } |
| 265 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 266 | bool NativeProcessProtocol::RegisterNativeDelegate( |
| 267 | NativeDelegate &native_delegate) { |
| 268 | std::lock_guard<std::recursive_mutex> guard(m_delegates_mutex); |
| 269 | if (std::find(m_delegates.begin(), m_delegates.end(), &native_delegate) != |
| 270 | m_delegates.end()) |
| 271 | return false; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 272 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 273 | m_delegates.push_back(&native_delegate); |
| 274 | native_delegate.InitializeDelegate(this); |
| 275 | return true; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 276 | } |
| 277 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 278 | bool NativeProcessProtocol::UnregisterNativeDelegate( |
| 279 | NativeDelegate &native_delegate) { |
| 280 | std::lock_guard<std::recursive_mutex> guard(m_delegates_mutex); |
| 281 | |
| 282 | const auto initial_size = m_delegates.size(); |
| 283 | m_delegates.erase( |
| 284 | remove(m_delegates.begin(), m_delegates.end(), &native_delegate), |
| 285 | m_delegates.end()); |
| 286 | |
| 287 | // We removed the delegate if the count of delegates shrank after |
| 288 | // removing all copies of the given native_delegate from the vector. |
| 289 | return m_delegates.size() < initial_size; |
| 290 | } |
| 291 | |
| 292 | void NativeProcessProtocol::SynchronouslyNotifyProcessStateChanged( |
| 293 | lldb::StateType state) { |
| 294 | Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
| 295 | |
| 296 | std::lock_guard<std::recursive_mutex> guard(m_delegates_mutex); |
| 297 | for (auto native_delegate : m_delegates) |
| 298 | native_delegate->ProcessStateChanged(this, state); |
| 299 | |
| 300 | if (log) { |
| 301 | if (!m_delegates.empty()) { |
| 302 | log->Printf("NativeProcessProtocol::%s: sent state notification [%s] " |
| 303 | "from process %" PRIu64, |
| 304 | __FUNCTION__, lldb_private::StateAsCString(state), GetID()); |
| 305 | } else { |
| 306 | log->Printf("NativeProcessProtocol::%s: would send state notification " |
| 307 | "[%s] from process %" PRIu64 ", but no delegates", |
| 308 | __FUNCTION__, lldb_private::StateAsCString(state), GetID()); |
| 309 | } |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | void NativeProcessProtocol::NotifyDidExec() { |
| 314 | Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
| 315 | if (log) |
| 316 | log->Printf("NativeProcessProtocol::%s - preparing to call delegates", |
| 317 | __FUNCTION__); |
| 318 | |
| 319 | { |
Saleem Abdulrasool | 16ff860 | 2016-05-18 01:59:10 +0000 | [diff] [blame] | 320 | std::lock_guard<std::recursive_mutex> guard(m_delegates_mutex); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 321 | for (auto native_delegate : m_delegates) |
| 322 | native_delegate->DidExec(this); |
| 323 | } |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 324 | } |
| 325 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 326 | Error NativeProcessProtocol::SetSoftwareBreakpoint(lldb::addr_t addr, |
| 327 | uint32_t size_hint) { |
| 328 | Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_BREAKPOINTS)); |
| 329 | if (log) |
| 330 | log->Printf("NativeProcessProtocol::%s addr = 0x%" PRIx64, __FUNCTION__, |
| 331 | addr); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 332 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 333 | return m_breakpoint_list.AddRef( |
| 334 | addr, size_hint, false, |
| 335 | [this](lldb::addr_t addr, size_t size_hint, bool /* hardware */, |
| 336 | NativeBreakpointSP &breakpoint_sp) -> Error { |
| 337 | return SoftwareBreakpoint::CreateSoftwareBreakpoint( |
| 338 | *this, addr, size_hint, breakpoint_sp); |
| 339 | }); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 340 | } |
| 341 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 342 | Error NativeProcessProtocol::RemoveBreakpoint(lldb::addr_t addr) { |
| 343 | return m_breakpoint_list.DecRef(addr); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 344 | } |
| 345 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 346 | Error NativeProcessProtocol::EnableBreakpoint(lldb::addr_t addr) { |
| 347 | return m_breakpoint_list.EnableBreakpoint(addr); |
Todd Fiala | a9882ce | 2014-08-28 15:46:54 +0000 | [diff] [blame] | 348 | } |
| 349 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 350 | Error NativeProcessProtocol::DisableBreakpoint(lldb::addr_t addr) { |
| 351 | return m_breakpoint_list.DisableBreakpoint(addr); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 352 | } |
| 353 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 354 | lldb::StateType NativeProcessProtocol::GetState() const { |
| 355 | std::lock_guard<std::recursive_mutex> guard(m_state_mutex); |
| 356 | return m_state; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 357 | } |
| 358 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 359 | void NativeProcessProtocol::SetState(lldb::StateType state, |
| 360 | bool notify_delegates) { |
| 361 | std::lock_guard<std::recursive_mutex> guard(m_state_mutex); |
| 362 | |
| 363 | if (state == m_state) |
| 364 | return; |
| 365 | |
| 366 | m_state = state; |
| 367 | |
| 368 | if (StateIsStoppedState(state, false)) { |
| 369 | ++m_stop_id; |
| 370 | |
| 371 | // Give process a chance to do any stop id bump processing, such as |
| 372 | // clearing cached data that is invalidated each time the process runs. |
| 373 | // Note if/when we support some threads running, we'll end up needing |
| 374 | // to manage this per thread and per process. |
| 375 | DoStopIDBumped(m_stop_id); |
| 376 | } |
| 377 | |
| 378 | // Optionally notify delegates of the state change. |
| 379 | if (notify_delegates) |
| 380 | SynchronouslyNotifyProcessStateChanged(state); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 381 | } |
| 382 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 383 | uint32_t NativeProcessProtocol::GetStopID() const { |
| 384 | std::lock_guard<std::recursive_mutex> guard(m_state_mutex); |
| 385 | return m_stop_id; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 386 | } |
| 387 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 388 | void NativeProcessProtocol::DoStopIDBumped(uint32_t /* newBumpId */) { |
| 389 | // Default implementation does nothing. |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 390 | } |
| 391 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 392 | Error NativeProcessProtocol::ResolveProcessArchitecture(lldb::pid_t pid, |
| 393 | ArchSpec &arch) { |
| 394 | // Grab process info for the running process. |
| 395 | ProcessInstanceInfo process_info; |
| 396 | if (!Host::GetProcessInfo(pid, process_info)) |
| 397 | return Error("failed to get process info"); |
Tamas Berghammer | 5830aa7 | 2015-02-06 10:42:33 +0000 | [diff] [blame] | 398 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 399 | // Resolve the executable module. |
| 400 | ModuleSpecList module_specs; |
| 401 | if (!ObjectFile::GetModuleSpecifications(process_info.GetExecutableFile(), 0, |
| 402 | 0, module_specs)) |
| 403 | return Error("failed to get module specifications"); |
| 404 | lldbassert(module_specs.GetSize() == 1); |
Tamas Berghammer | 5830aa7 | 2015-02-06 10:42:33 +0000 | [diff] [blame] | 405 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 406 | arch = module_specs.GetModuleSpecRefAtIndex(0).GetArchitecture(); |
| 407 | if (arch.IsValid()) |
| 408 | return Error(); |
| 409 | else |
| 410 | return Error("failed to retrieve a valid architecture from the exe module"); |
Todd Fiala | e77fce0 | 2016-09-04 00:18:56 +0000 | [diff] [blame] | 411 | } |
| 412 | |
Pavel Labath | d5b310f | 2015-07-09 11:51:11 +0000 | [diff] [blame] | 413 | #ifndef __linux__ |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 414 | // These need to be implemented to support lldb-gdb-server on a given platform. |
| 415 | // Stubs are |
Pavel Labath | d5b310f | 2015-07-09 11:51:11 +0000 | [diff] [blame] | 416 | // provided to make the rest of the code link on non-supported platforms. |
| 417 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 418 | Error NativeProcessProtocol::Launch(ProcessLaunchInfo &launch_info, |
| 419 | NativeDelegate &native_delegate, |
| 420 | MainLoop &mainloop, |
| 421 | NativeProcessProtocolSP &process_sp) { |
| 422 | llvm_unreachable("Platform has no NativeProcessProtocol support"); |
Pavel Labath | d5b310f | 2015-07-09 11:51:11 +0000 | [diff] [blame] | 423 | } |
| 424 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 425 | Error NativeProcessProtocol::Attach(lldb::pid_t pid, |
| 426 | NativeDelegate &native_delegate, |
| 427 | MainLoop &mainloop, |
| 428 | NativeProcessProtocolSP &process_sp) { |
| 429 | llvm_unreachable("Platform has no NativeProcessProtocol support"); |
Pavel Labath | d5b310f | 2015-07-09 11:51:11 +0000 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | #endif |