blob: da00da40186a8e166e640552ec09a2f2d6b000d0 [file] [log] [blame]
Todd Fialaaf245d12014-06-30 21:05:18 +00001//===-- 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 Lin2fe1d0a2015-02-03 01:51:38 +000010#include "lldb/Host/common/NativeProcessProtocol.h"
Todd Fiala511e5cd2014-09-11 23:29:14 +000011#include "lldb/Host/Host.h"
Chaoren Lin2fe1d0a2015-02-03 01:51:38 +000012#include "lldb/Host/common/NativeRegisterContext.h"
Chaoren Lin2fe1d0a2015-02-03 01:51:38 +000013#include "lldb/Host/common/NativeThreadProtocol.h"
14#include "lldb/Host/common/SoftwareBreakpoint.h"
Todd Fialae77fce02016-09-04 00:18:56 +000015#include "lldb/Utility/LLDBAssert.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000016#include "lldb/Utility/Log.h"
Pavel Labathd821c992018-08-07 11:07:21 +000017#include "lldb/Utility/State.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000018#include "lldb/lldb-enumerations.h"
Todd Fialaaf245d12014-06-30 21:05:18 +000019
20using namespace lldb;
21using namespace lldb_private;
22
23// -----------------------------------------------------------------------------
24// NativeProcessProtocol Members
25// -----------------------------------------------------------------------------
26
Pavel Labath96e600f2017-07-07 11:02:19 +000027NativeProcessProtocol::NativeProcessProtocol(lldb::pid_t pid, int terminal_fd,
28 NativeDelegate &delegate)
29 : m_pid(pid), m_terminal_fd(terminal_fd) {
30 bool registered = RegisterNativeDelegate(delegate);
31 assert(registered);
32 (void)registered;
33}
Todd Fialaaf245d12014-06-30 21:05:18 +000034
Zachary Turner97206d52017-05-12 04:51:55 +000035lldb_private::Status NativeProcessProtocol::Interrupt() {
36 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +000037#if !defined(SIGSTOP)
38 error.SetErrorString("local host does not support signaling");
39 return error;
Todd Fiala511e5cd2014-09-11 23:29:14 +000040#else
Kate Stoneb9c1b512016-09-06 20:57:50 +000041 return Signal(SIGSTOP);
Todd Fiala511e5cd2014-09-11 23:29:14 +000042#endif
43}
44
Zachary Turner97206d52017-05-12 04:51:55 +000045Status NativeProcessProtocol::IgnoreSignals(llvm::ArrayRef<int> signals) {
Pavel Labath4a705e72017-02-24 09:29:14 +000046 m_signals_to_ignore.clear();
47 m_signals_to_ignore.insert(signals.begin(), signals.end());
Zachary Turner97206d52017-05-12 04:51:55 +000048 return Status();
Pavel Labath4a705e72017-02-24 09:29:14 +000049}
50
Zachary Turner97206d52017-05-12 04:51:55 +000051lldb_private::Status
Kate Stoneb9c1b512016-09-06 20:57:50 +000052NativeProcessProtocol::GetMemoryRegionInfo(lldb::addr_t load_addr,
53 MemoryRegionInfo &range_info) {
54 // Default: not implemented.
Zachary Turner97206d52017-05-12 04:51:55 +000055 return Status("not implemented");
Todd Fialaaf245d12014-06-30 21:05:18 +000056}
57
Pavel Labath3508fc82017-06-19 12:47:50 +000058llvm::Optional<WaitStatus> NativeProcessProtocol::GetExitStatus() {
59 if (m_state == lldb::eStateExited)
60 return m_exit_status;
Todd Fialaaf245d12014-06-30 21:05:18 +000061
Pavel Labath3508fc82017-06-19 12:47:50 +000062 return llvm::None;
Todd Fialaaf245d12014-06-30 21:05:18 +000063}
64
Pavel Labath3508fc82017-06-19 12:47:50 +000065bool NativeProcessProtocol::SetExitStatus(WaitStatus status,
Kate Stoneb9c1b512016-09-06 20:57:50 +000066 bool bNotifyStateChange) {
67 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
Pavel Labath3508fc82017-06-19 12:47:50 +000068 LLDB_LOG(log, "status = {0}, notify = {1}", status, bNotifyStateChange);
Todd Fialaaf245d12014-06-30 21:05:18 +000069
Kate Stoneb9c1b512016-09-06 20:57:50 +000070 // Exit status already set
71 if (m_state == lldb::eStateExited) {
Pavel Labath3508fc82017-06-19 12:47:50 +000072 if (m_exit_status)
73 LLDB_LOG(log, "exit status already set to {0}", *m_exit_status);
74 else
75 LLDB_LOG(log, "state is exited, but status not set");
Kate Stoneb9c1b512016-09-06 20:57:50 +000076 return false;
77 }
Todd Fialaaf245d12014-06-30 21:05:18 +000078
Kate Stoneb9c1b512016-09-06 20:57:50 +000079 m_state = lldb::eStateExited;
Kate Stoneb9c1b512016-09-06 20:57:50 +000080 m_exit_status = status;
Todd Fialaaf245d12014-06-30 21:05:18 +000081
Kate Stoneb9c1b512016-09-06 20:57:50 +000082 if (bNotifyStateChange)
83 SynchronouslyNotifyProcessStateChanged(lldb::eStateExited);
Todd Fialaaf245d12014-06-30 21:05:18 +000084
Kate Stoneb9c1b512016-09-06 20:57:50 +000085 return true;
86}
87
Pavel Labatha5be48b2017-10-17 15:52:16 +000088NativeThreadProtocol *NativeProcessProtocol::GetThreadAtIndex(uint32_t idx) {
Kate Stoneb9c1b512016-09-06 20:57:50 +000089 std::lock_guard<std::recursive_mutex> guard(m_threads_mutex);
90 if (idx < m_threads.size())
Pavel Labatha5be48b2017-10-17 15:52:16 +000091 return m_threads[idx].get();
92 return nullptr;
Todd Fialaaf245d12014-06-30 21:05:18 +000093}
94
Pavel Labatha5be48b2017-10-17 15:52:16 +000095NativeThreadProtocol *
Kate Stoneb9c1b512016-09-06 20:57:50 +000096NativeProcessProtocol::GetThreadByIDUnlocked(lldb::tid_t tid) {
Pavel Labatha5be48b2017-10-17 15:52:16 +000097 for (const auto &thread : m_threads) {
98 if (thread->GetID() == tid)
99 return thread.get();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000100 }
Pavel Labatha5be48b2017-10-17 15:52:16 +0000101 return nullptr;
Todd Fialaaf245d12014-06-30 21:05:18 +0000102}
103
Pavel Labatha5be48b2017-10-17 15:52:16 +0000104NativeThreadProtocol *NativeProcessProtocol::GetThreadByID(lldb::tid_t tid) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000105 std::lock_guard<std::recursive_mutex> guard(m_threads_mutex);
106 return GetThreadByIDUnlocked(tid);
Todd Fialaaf245d12014-06-30 21:05:18 +0000107}
108
Kate Stoneb9c1b512016-09-06 20:57:50 +0000109bool NativeProcessProtocol::IsAlive() const {
110 return m_state != eStateDetached && m_state != eStateExited &&
111 m_state != eStateInvalid && m_state != eStateUnloaded;
Todd Fiala511e5cd2014-09-11 23:29:14 +0000112}
113
Kate Stoneb9c1b512016-09-06 20:57:50 +0000114const NativeWatchpointList::WatchpointMap &
115NativeProcessProtocol::GetWatchpointMap() const {
116 return m_watchpoint_list.GetWatchpointMap();
Todd Fialaaf245d12014-06-30 21:05:18 +0000117}
118
Omair Javaidd5ffbad2017-02-24 13:27:31 +0000119llvm::Optional<std::pair<uint32_t, uint32_t>>
120NativeProcessProtocol::GetHardwareDebugSupportInfo() const {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000121 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
122
123 // get any thread
Pavel Labatha5be48b2017-10-17 15:52:16 +0000124 NativeThreadProtocol *thread(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000125 const_cast<NativeProcessProtocol *>(this)->GetThreadAtIndex(0));
Pavel Labatha5be48b2017-10-17 15:52:16 +0000126 if (!thread) {
127 LLDB_LOG(log, "failed to find a thread to grab a NativeRegisterContext!");
Omair Javaidd5ffbad2017-02-24 13:27:31 +0000128 return llvm::None;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000129 }
130
Pavel Labathd37349f2017-11-10 11:05:49 +0000131 NativeRegisterContext &reg_ctx = thread->GetRegisterContext();
132 return std::make_pair(reg_ctx.NumSupportedHardwareBreakpoints(),
133 reg_ctx.NumSupportedHardwareWatchpoints());
Chaoren Lin18fe6402015-02-03 01:51:47 +0000134}
135
Zachary Turner97206d52017-05-12 04:51:55 +0000136Status NativeProcessProtocol::SetWatchpoint(lldb::addr_t addr, size_t size,
137 uint32_t watch_flags,
138 bool hardware) {
Adrian Prantl05097242018-04-30 16:49:04 +0000139 // This default implementation assumes setting the watchpoint for the process
140 // will require setting the watchpoint for each of the threads. Furthermore,
141 // it will track watchpoints set for the process and will add them to each
142 // thread that is attached to via the (FIXME implement) OnThreadAttached ()
143 // method.
Todd Fialaaf245d12014-06-30 21:05:18 +0000144
Kate Stoneb9c1b512016-09-06 20:57:50 +0000145 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
146
147 // Update the thread list
148 UpdateThreads();
149
Adrian Prantl05097242018-04-30 16:49:04 +0000150 // Keep track of the threads we successfully set the watchpoint for. If one
151 // of the thread watchpoint setting operations fails, back off and remove the
152 // watchpoint for all the threads that were successfully set so we get back
153 // to a consistent state.
Pavel Labatha5be48b2017-10-17 15:52:16 +0000154 std::vector<NativeThreadProtocol *> watchpoint_established_threads;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000155
Adrian Prantl05097242018-04-30 16:49:04 +0000156 // Tell each thread to set a watchpoint. In the event that hardware
157 // watchpoints are requested but the SetWatchpoint fails, try to set a
158 // software watchpoint as a fallback. It's conceivable that if there are
159 // more threads than hardware watchpoints available, some of the threads will
160 // fail to set hardware watchpoints while software ones may be available.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000161 std::lock_guard<std::recursive_mutex> guard(m_threads_mutex);
Pavel Labatha5be48b2017-10-17 15:52:16 +0000162 for (const auto &thread : m_threads) {
163 assert(thread && "thread list should not have a NULL thread!");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000164
Zachary Turner97206d52017-05-12 04:51:55 +0000165 Status thread_error =
Pavel Labatha5be48b2017-10-17 15:52:16 +0000166 thread->SetWatchpoint(addr, size, watch_flags, hardware);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000167 if (thread_error.Fail() && hardware) {
Adrian Prantl05097242018-04-30 16:49:04 +0000168 // Try software watchpoints since we failed on hardware watchpoint
169 // setting and we may have just run out of hardware watchpoints.
Pavel Labatha5be48b2017-10-17 15:52:16 +0000170 thread_error = thread->SetWatchpoint(addr, size, watch_flags, false);
171 if (thread_error.Success())
172 LLDB_LOG(log,
173 "hardware watchpoint requested but software watchpoint set");
Todd Fialaaf245d12014-06-30 21:05:18 +0000174 }
175
Kate Stoneb9c1b512016-09-06 20:57:50 +0000176 if (thread_error.Success()) {
Adrian Prantl05097242018-04-30 16:49:04 +0000177 // Remember that we set this watchpoint successfully in case we need to
178 // clear it later.
Pavel Labatha5be48b2017-10-17 15:52:16 +0000179 watchpoint_established_threads.push_back(thread.get());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000180 } else {
Adrian Prantl05097242018-04-30 16:49:04 +0000181 // Unset the watchpoint for each thread we successfully set so that we
182 // get back to a consistent state of "not set" for the watchpoint.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000183 for (auto unwatch_thread_sp : watchpoint_established_threads) {
Zachary Turner97206d52017-05-12 04:51:55 +0000184 Status remove_error = unwatch_thread_sp->RemoveWatchpoint(addr);
Pavel Labatha5be48b2017-10-17 15:52:16 +0000185 if (remove_error.Fail())
186 LLDB_LOG(log, "RemoveWatchpoint failed for pid={0}, tid={1}: {2}",
187 GetID(), unwatch_thread_sp->GetID(), remove_error);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000188 }
Todd Fialaaf245d12014-06-30 21:05:18 +0000189
Kate Stoneb9c1b512016-09-06 20:57:50 +0000190 return thread_error;
191 }
192 }
193 return m_watchpoint_list.Add(addr, size, watch_flags, hardware);
Todd Fialaaf245d12014-06-30 21:05:18 +0000194}
195
Zachary Turner97206d52017-05-12 04:51:55 +0000196Status NativeProcessProtocol::RemoveWatchpoint(lldb::addr_t addr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000197 // Update the thread list
198 UpdateThreads();
Todd Fialaaf245d12014-06-30 21:05:18 +0000199
Zachary Turner97206d52017-05-12 04:51:55 +0000200 Status overall_error;
Todd Fialaaf245d12014-06-30 21:05:18 +0000201
Kate Stoneb9c1b512016-09-06 20:57:50 +0000202 std::lock_guard<std::recursive_mutex> guard(m_threads_mutex);
Pavel Labatha5be48b2017-10-17 15:52:16 +0000203 for (const auto &thread : m_threads) {
204 assert(thread && "thread list should not have a NULL thread!");
Todd Fialaaf245d12014-06-30 21:05:18 +0000205
Pavel Labatha5be48b2017-10-17 15:52:16 +0000206 const Status thread_error = thread->RemoveWatchpoint(addr);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000207 if (thread_error.Fail()) {
Adrian Prantl05097242018-04-30 16:49:04 +0000208 // Keep track of the first thread error if any threads fail. We want to
209 // try to remove the watchpoint from every thread, though, even if one or
210 // more have errors.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000211 if (!overall_error.Fail())
212 overall_error = thread_error;
Todd Fialaaf245d12014-06-30 21:05:18 +0000213 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000214 }
Zachary Turner97206d52017-05-12 04:51:55 +0000215 const Status error = m_watchpoint_list.Remove(addr);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000216 return overall_error.Fail() ? overall_error : error;
Todd Fialaaf245d12014-06-30 21:05:18 +0000217}
218
Omair Javaidd5ffbad2017-02-24 13:27:31 +0000219const HardwareBreakpointMap &
220NativeProcessProtocol::GetHardwareBreakpointMap() const {
221 return m_hw_breakpoints_map;
222}
223
Zachary Turner97206d52017-05-12 04:51:55 +0000224Status NativeProcessProtocol::SetHardwareBreakpoint(lldb::addr_t addr,
225 size_t size) {
Adrian Prantl05097242018-04-30 16:49:04 +0000226 // This default implementation assumes setting a hardware breakpoint for this
227 // process will require setting same hardware breakpoint for each of its
228 // existing threads. New thread will do the same once created.
Omair Javaidd5ffbad2017-02-24 13:27:31 +0000229 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
230
231 // Update the thread list
232 UpdateThreads();
233
234 // Exit here if target does not have required hardware breakpoint capability.
235 auto hw_debug_cap = GetHardwareDebugSupportInfo();
236
237 if (hw_debug_cap == llvm::None || hw_debug_cap->first == 0 ||
238 hw_debug_cap->first <= m_hw_breakpoints_map.size())
Zachary Turner97206d52017-05-12 04:51:55 +0000239 return Status("Target does not have required no of hardware breakpoints");
Omair Javaidd5ffbad2017-02-24 13:27:31 +0000240
241 // Vector below stores all thread pointer for which we have we successfully
242 // set this hardware breakpoint. If any of the current process threads fails
243 // to set this hardware breakpoint then roll back and remove this breakpoint
244 // for all the threads that had already set it successfully.
Pavel Labatha5be48b2017-10-17 15:52:16 +0000245 std::vector<NativeThreadProtocol *> breakpoint_established_threads;
Omair Javaidd5ffbad2017-02-24 13:27:31 +0000246
247 // Request to set a hardware breakpoint for each of current process threads.
248 std::lock_guard<std::recursive_mutex> guard(m_threads_mutex);
Pavel Labatha5be48b2017-10-17 15:52:16 +0000249 for (const auto &thread : m_threads) {
250 assert(thread && "thread list should not have a NULL thread!");
Omair Javaidd5ffbad2017-02-24 13:27:31 +0000251
Pavel Labatha5be48b2017-10-17 15:52:16 +0000252 Status thread_error = thread->SetHardwareBreakpoint(addr, size);
Omair Javaidd5ffbad2017-02-24 13:27:31 +0000253 if (thread_error.Success()) {
Adrian Prantl05097242018-04-30 16:49:04 +0000254 // Remember that we set this breakpoint successfully in case we need to
255 // clear it later.
Pavel Labatha5be48b2017-10-17 15:52:16 +0000256 breakpoint_established_threads.push_back(thread.get());
Omair Javaidd5ffbad2017-02-24 13:27:31 +0000257 } else {
Adrian Prantl05097242018-04-30 16:49:04 +0000258 // Unset the breakpoint for each thread we successfully set so that we
259 // get back to a consistent state of "not set" for this hardware
260 // breakpoint.
Omair Javaidd5ffbad2017-02-24 13:27:31 +0000261 for (auto rollback_thread_sp : breakpoint_established_threads) {
Zachary Turner97206d52017-05-12 04:51:55 +0000262 Status remove_error =
263 rollback_thread_sp->RemoveHardwareBreakpoint(addr);
Pavel Labatha5be48b2017-10-17 15:52:16 +0000264 if (remove_error.Fail())
265 LLDB_LOG(log,
266 "RemoveHardwareBreakpoint failed for pid={0}, tid={1}: {2}",
267 GetID(), rollback_thread_sp->GetID(), remove_error);
Omair Javaidd5ffbad2017-02-24 13:27:31 +0000268 }
269
270 return thread_error;
271 }
272 }
273
274 // Register new hardware breakpoint into hardware breakpoints map of current
275 // process.
276 m_hw_breakpoints_map[addr] = {addr, size};
277
Zachary Turner97206d52017-05-12 04:51:55 +0000278 return Status();
Omair Javaidd5ffbad2017-02-24 13:27:31 +0000279}
280
Zachary Turner97206d52017-05-12 04:51:55 +0000281Status NativeProcessProtocol::RemoveHardwareBreakpoint(lldb::addr_t addr) {
Omair Javaidd5ffbad2017-02-24 13:27:31 +0000282 // Update the thread list
283 UpdateThreads();
284
Zachary Turner97206d52017-05-12 04:51:55 +0000285 Status error;
Omair Javaidd5ffbad2017-02-24 13:27:31 +0000286
287 std::lock_guard<std::recursive_mutex> guard(m_threads_mutex);
Pavel Labatha5be48b2017-10-17 15:52:16 +0000288 for (const auto &thread : m_threads) {
289 assert(thread && "thread list should not have a NULL thread!");
290 error = thread->RemoveHardwareBreakpoint(addr);
Omair Javaidd5ffbad2017-02-24 13:27:31 +0000291 }
292
293 // Also remove from hardware breakpoint map of current process.
294 m_hw_breakpoints_map.erase(addr);
295
296 return error;
297}
298
Kate Stoneb9c1b512016-09-06 20:57:50 +0000299bool NativeProcessProtocol::RegisterNativeDelegate(
300 NativeDelegate &native_delegate) {
301 std::lock_guard<std::recursive_mutex> guard(m_delegates_mutex);
302 if (std::find(m_delegates.begin(), m_delegates.end(), &native_delegate) !=
303 m_delegates.end())
304 return false;
Todd Fialaaf245d12014-06-30 21:05:18 +0000305
Kate Stoneb9c1b512016-09-06 20:57:50 +0000306 m_delegates.push_back(&native_delegate);
307 native_delegate.InitializeDelegate(this);
308 return true;
Todd Fialaaf245d12014-06-30 21:05:18 +0000309}
310
Kate Stoneb9c1b512016-09-06 20:57:50 +0000311bool NativeProcessProtocol::UnregisterNativeDelegate(
312 NativeDelegate &native_delegate) {
313 std::lock_guard<std::recursive_mutex> guard(m_delegates_mutex);
314
315 const auto initial_size = m_delegates.size();
316 m_delegates.erase(
317 remove(m_delegates.begin(), m_delegates.end(), &native_delegate),
318 m_delegates.end());
319
Adrian Prantl05097242018-04-30 16:49:04 +0000320 // We removed the delegate if the count of delegates shrank after removing
321 // all copies of the given native_delegate from the vector.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000322 return m_delegates.size() < initial_size;
323}
324
325void NativeProcessProtocol::SynchronouslyNotifyProcessStateChanged(
326 lldb::StateType state) {
327 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
328
329 std::lock_guard<std::recursive_mutex> guard(m_delegates_mutex);
330 for (auto native_delegate : m_delegates)
331 native_delegate->ProcessStateChanged(this, state);
332
333 if (log) {
334 if (!m_delegates.empty()) {
335 log->Printf("NativeProcessProtocol::%s: sent state notification [%s] "
336 "from process %" PRIu64,
337 __FUNCTION__, lldb_private::StateAsCString(state), GetID());
338 } else {
339 log->Printf("NativeProcessProtocol::%s: would send state notification "
340 "[%s] from process %" PRIu64 ", but no delegates",
341 __FUNCTION__, lldb_private::StateAsCString(state), GetID());
342 }
343 }
344}
345
346void NativeProcessProtocol::NotifyDidExec() {
347 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
348 if (log)
349 log->Printf("NativeProcessProtocol::%s - preparing to call delegates",
350 __FUNCTION__);
351
352 {
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +0000353 std::lock_guard<std::recursive_mutex> guard(m_delegates_mutex);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000354 for (auto native_delegate : m_delegates)
355 native_delegate->DidExec(this);
356 }
Todd Fialaaf245d12014-06-30 21:05:18 +0000357}
358
Zachary Turner97206d52017-05-12 04:51:55 +0000359Status NativeProcessProtocol::SetSoftwareBreakpoint(lldb::addr_t addr,
360 uint32_t size_hint) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000361 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_BREAKPOINTS));
362 if (log)
363 log->Printf("NativeProcessProtocol::%s addr = 0x%" PRIx64, __FUNCTION__,
364 addr);
Todd Fialaaf245d12014-06-30 21:05:18 +0000365
Kate Stoneb9c1b512016-09-06 20:57:50 +0000366 return m_breakpoint_list.AddRef(
367 addr, size_hint, false,
368 [this](lldb::addr_t addr, size_t size_hint, bool /* hardware */,
Zachary Turner97206d52017-05-12 04:51:55 +0000369 NativeBreakpointSP &breakpoint_sp) -> Status {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000370 return SoftwareBreakpoint::CreateSoftwareBreakpoint(
371 *this, addr, size_hint, breakpoint_sp);
372 });
Todd Fialaaf245d12014-06-30 21:05:18 +0000373}
374
Pavel Labathf8b825f2018-09-09 06:01:12 +0000375llvm::Expected<llvm::ArrayRef<uint8_t>>
376NativeProcessProtocol::GetSoftwareBreakpointTrapOpcode(size_t size_hint) {
377 static const uint8_t g_aarch64_opcode[] = {0x00, 0x00, 0x20, 0xd4};
378 static const uint8_t g_i386_opcode[] = {0xCC};
379 static const uint8_t g_mips64_opcode[] = {0x00, 0x00, 0x00, 0x0d};
380 static const uint8_t g_mips64el_opcode[] = {0x0d, 0x00, 0x00, 0x00};
381 static const uint8_t g_s390x_opcode[] = {0x00, 0x01};
382 static const uint8_t g_ppc64le_opcode[] = {0x08, 0x00, 0xe0, 0x7f}; // trap
383
384 switch (GetArchitecture().GetMachine()) {
385 case llvm::Triple::aarch64:
Pavel Labath4f545072018-09-09 08:42:00 +0000386 return llvm::makeArrayRef(g_aarch64_opcode);
Pavel Labathf8b825f2018-09-09 06:01:12 +0000387
388 case llvm::Triple::x86:
389 case llvm::Triple::x86_64:
Pavel Labath4f545072018-09-09 08:42:00 +0000390 return llvm::makeArrayRef(g_i386_opcode);
Pavel Labathf8b825f2018-09-09 06:01:12 +0000391
392 case llvm::Triple::mips:
393 case llvm::Triple::mips64:
Pavel Labath4f545072018-09-09 08:42:00 +0000394 return llvm::makeArrayRef(g_mips64_opcode);
Pavel Labathf8b825f2018-09-09 06:01:12 +0000395
396 case llvm::Triple::mipsel:
397 case llvm::Triple::mips64el:
Pavel Labath4f545072018-09-09 08:42:00 +0000398 return llvm::makeArrayRef(g_mips64el_opcode);
Pavel Labathf8b825f2018-09-09 06:01:12 +0000399
400 case llvm::Triple::systemz:
Pavel Labath4f545072018-09-09 08:42:00 +0000401 return llvm::makeArrayRef(g_s390x_opcode);
Pavel Labathf8b825f2018-09-09 06:01:12 +0000402
403 case llvm::Triple::ppc64le:
Pavel Labath4f545072018-09-09 08:42:00 +0000404 return llvm::makeArrayRef(g_ppc64le_opcode);
Pavel Labathf8b825f2018-09-09 06:01:12 +0000405
406 default:
407 return llvm::createStringError(llvm::inconvertibleErrorCode(),
408 "CPU type not supported!");
409 }
410}
411
Zachary Turner97206d52017-05-12 04:51:55 +0000412Status NativeProcessProtocol::RemoveBreakpoint(lldb::addr_t addr,
413 bool hardware) {
Omair Javaidd5ffbad2017-02-24 13:27:31 +0000414 if (hardware)
415 return RemoveHardwareBreakpoint(addr);
416 else
417 return m_breakpoint_list.DecRef(addr);
Todd Fialaaf245d12014-06-30 21:05:18 +0000418}
419
Zachary Turner97206d52017-05-12 04:51:55 +0000420Status NativeProcessProtocol::EnableBreakpoint(lldb::addr_t addr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000421 return m_breakpoint_list.EnableBreakpoint(addr);
Todd Fialaa9882ce2014-08-28 15:46:54 +0000422}
423
Zachary Turner97206d52017-05-12 04:51:55 +0000424Status NativeProcessProtocol::DisableBreakpoint(lldb::addr_t addr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000425 return m_breakpoint_list.DisableBreakpoint(addr);
Todd Fialaaf245d12014-06-30 21:05:18 +0000426}
427
Kate Stoneb9c1b512016-09-06 20:57:50 +0000428lldb::StateType NativeProcessProtocol::GetState() const {
429 std::lock_guard<std::recursive_mutex> guard(m_state_mutex);
430 return m_state;
Todd Fialaaf245d12014-06-30 21:05:18 +0000431}
432
Kate Stoneb9c1b512016-09-06 20:57:50 +0000433void NativeProcessProtocol::SetState(lldb::StateType state,
434 bool notify_delegates) {
435 std::lock_guard<std::recursive_mutex> guard(m_state_mutex);
436
437 if (state == m_state)
438 return;
439
440 m_state = state;
441
442 if (StateIsStoppedState(state, false)) {
443 ++m_stop_id;
444
445 // Give process a chance to do any stop id bump processing, such as
446 // clearing cached data that is invalidated each time the process runs.
Adrian Prantl05097242018-04-30 16:49:04 +0000447 // Note if/when we support some threads running, we'll end up needing to
448 // manage this per thread and per process.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000449 DoStopIDBumped(m_stop_id);
450 }
451
452 // Optionally notify delegates of the state change.
453 if (notify_delegates)
454 SynchronouslyNotifyProcessStateChanged(state);
Todd Fialaaf245d12014-06-30 21:05:18 +0000455}
456
Kate Stoneb9c1b512016-09-06 20:57:50 +0000457uint32_t NativeProcessProtocol::GetStopID() const {
458 std::lock_guard<std::recursive_mutex> guard(m_state_mutex);
459 return m_stop_id;
Todd Fialaaf245d12014-06-30 21:05:18 +0000460}
461
Kate Stoneb9c1b512016-09-06 20:57:50 +0000462void NativeProcessProtocol::DoStopIDBumped(uint32_t /* newBumpId */) {
463 // Default implementation does nothing.
Todd Fialaaf245d12014-06-30 21:05:18 +0000464}
465
Pavel Labath96e600f2017-07-07 11:02:19 +0000466NativeProcessProtocol::Factory::~Factory() = default;