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