1cfca06d7SDimitry Andric //===-- NativeProcessProtocol.cpp -----------------------------------------===//
20cac4ca3SEd Maste //
35f29bb8aSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45f29bb8aSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
55f29bb8aSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60cac4ca3SEd Maste //
70cac4ca3SEd Maste //===----------------------------------------------------------------------===//
80cac4ca3SEd Maste
912bd4897SEd Maste #include "lldb/Host/common/NativeProcessProtocol.h"
10205afe67SEd Maste #include "lldb/Host/Host.h"
1194994d37SDimitry Andric #include "lldb/Host/common/NativeBreakpointList.h"
1212bd4897SEd Maste #include "lldb/Host/common/NativeRegisterContext.h"
1312bd4897SEd Maste #include "lldb/Host/common/NativeThreadProtocol.h"
1414f1b3e8SDimitry Andric #include "lldb/Utility/LLDBAssert.h"
15145449b1SDimitry Andric #include "lldb/Utility/LLDBLog.h"
1674a628f7SDimitry Andric #include "lldb/Utility/Log.h"
1794994d37SDimitry Andric #include "lldb/Utility/State.h"
1814f1b3e8SDimitry Andric #include "lldb/lldb-enumerations.h"
190cac4ca3SEd Maste
20ead24645SDimitry Andric #include "llvm/Support/Process.h"
21e3b55780SDimitry Andric #include <optional>
22ead24645SDimitry Andric
230cac4ca3SEd Maste using namespace lldb;
240cac4ca3SEd Maste using namespace lldb_private;
250cac4ca3SEd Maste
260cac4ca3SEd Maste // NativeProcessProtocol Members
270cac4ca3SEd Maste
NativeProcessProtocol(lldb::pid_t pid,int terminal_fd,NativeDelegate & delegate)28e75e363cSDimitry Andric NativeProcessProtocol::NativeProcessProtocol(lldb::pid_t pid, int terminal_fd,
29e75e363cSDimitry Andric NativeDelegate &delegate)
30344a3780SDimitry Andric : m_pid(pid), m_delegate(delegate), m_terminal_fd(terminal_fd) {
31344a3780SDimitry Andric delegate.InitializeDelegate(this);
32e75e363cSDimitry Andric }
330cac4ca3SEd Maste
Interrupt()34b76161e4SDimitry Andric lldb_private::Status NativeProcessProtocol::Interrupt() {
35b76161e4SDimitry Andric Status error;
36205afe67SEd Maste #if !defined(SIGSTOP)
37205afe67SEd Maste error.SetErrorString("local host does not support signaling");
38205afe67SEd Maste return error;
39205afe67SEd Maste #else
40205afe67SEd Maste return Signal(SIGSTOP);
41205afe67SEd Maste #endif
42205afe67SEd Maste }
43205afe67SEd Maste
IgnoreSignals(llvm::ArrayRef<int> signals)44b76161e4SDimitry Andric Status NativeProcessProtocol::IgnoreSignals(llvm::ArrayRef<int> signals) {
4574a628f7SDimitry Andric m_signals_to_ignore.clear();
4674a628f7SDimitry Andric m_signals_to_ignore.insert(signals.begin(), signals.end());
47b76161e4SDimitry Andric return Status();
4874a628f7SDimitry Andric }
4974a628f7SDimitry Andric
50b76161e4SDimitry Andric lldb_private::Status
GetMemoryRegionInfo(lldb::addr_t load_addr,MemoryRegionInfo & range_info)5114f1b3e8SDimitry Andric NativeProcessProtocol::GetMemoryRegionInfo(lldb::addr_t load_addr,
5214f1b3e8SDimitry Andric MemoryRegionInfo &range_info) {
530cac4ca3SEd Maste // Default: not implemented.
54b76161e4SDimitry Andric return Status("not implemented");
550cac4ca3SEd Maste }
560cac4ca3SEd Maste
57344a3780SDimitry Andric lldb_private::Status
ReadMemoryTags(int32_t type,lldb::addr_t addr,size_t len,std::vector<uint8_t> & tags)58344a3780SDimitry Andric NativeProcessProtocol::ReadMemoryTags(int32_t type, lldb::addr_t addr,
59344a3780SDimitry Andric size_t len, std::vector<uint8_t> &tags) {
60344a3780SDimitry Andric return Status("not implemented");
61344a3780SDimitry Andric }
62344a3780SDimitry Andric
63344a3780SDimitry Andric lldb_private::Status
WriteMemoryTags(int32_t type,lldb::addr_t addr,size_t len,const std::vector<uint8_t> & tags)64344a3780SDimitry Andric NativeProcessProtocol::WriteMemoryTags(int32_t type, lldb::addr_t addr,
65344a3780SDimitry Andric size_t len,
66344a3780SDimitry Andric const std::vector<uint8_t> &tags) {
67344a3780SDimitry Andric return Status("not implemented");
68344a3780SDimitry Andric }
69344a3780SDimitry Andric
GetExitStatus()70e3b55780SDimitry Andric std::optional<WaitStatus> NativeProcessProtocol::GetExitStatus() {
71fdea456aSDimitry Andric if (m_state == lldb::eStateExited)
72fdea456aSDimitry Andric return m_exit_status;
73fdea456aSDimitry Andric
74e3b55780SDimitry Andric return std::nullopt;
750cac4ca3SEd Maste }
760cac4ca3SEd Maste
SetExitStatus(WaitStatus status,bool bNotifyStateChange)77fdea456aSDimitry Andric bool NativeProcessProtocol::SetExitStatus(WaitStatus status,
7814f1b3e8SDimitry Andric bool bNotifyStateChange) {
79145449b1SDimitry Andric Log *log = GetLog(LLDBLog::Process);
80fdea456aSDimitry Andric LLDB_LOG(log, "status = {0}, notify = {1}", status, bNotifyStateChange);
810cac4ca3SEd Maste
820cac4ca3SEd Maste // Exit status already set
8314f1b3e8SDimitry Andric if (m_state == lldb::eStateExited) {
84fdea456aSDimitry Andric if (m_exit_status)
85fdea456aSDimitry Andric LLDB_LOG(log, "exit status already set to {0}", *m_exit_status);
86fdea456aSDimitry Andric else
87fdea456aSDimitry Andric LLDB_LOG(log, "state is exited, but status not set");
880cac4ca3SEd Maste return false;
890cac4ca3SEd Maste }
900cac4ca3SEd Maste
910cac4ca3SEd Maste m_state = lldb::eStateExited;
920cac4ca3SEd Maste m_exit_status = status;
930cac4ca3SEd Maste
940cac4ca3SEd Maste if (bNotifyStateChange)
950cac4ca3SEd Maste SynchronouslyNotifyProcessStateChanged(lldb::eStateExited);
960cac4ca3SEd Maste
970cac4ca3SEd Maste return true;
980cac4ca3SEd Maste }
990cac4ca3SEd Maste
GetThreadAtIndex(uint32_t idx)100ef5d0b5eSDimitry Andric NativeThreadProtocol *NativeProcessProtocol::GetThreadAtIndex(uint32_t idx) {
101f3fbd1c0SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_threads_mutex);
1020cac4ca3SEd Maste if (idx < m_threads.size())
103ef5d0b5eSDimitry Andric return m_threads[idx].get();
104ef5d0b5eSDimitry Andric return nullptr;
1050cac4ca3SEd Maste }
1060cac4ca3SEd Maste
107ef5d0b5eSDimitry Andric NativeThreadProtocol *
GetThreadByIDUnlocked(lldb::tid_t tid)10814f1b3e8SDimitry Andric NativeProcessProtocol::GetThreadByIDUnlocked(lldb::tid_t tid) {
109ef5d0b5eSDimitry Andric for (const auto &thread : m_threads) {
110ef5d0b5eSDimitry Andric if (thread->GetID() == tid)
111ef5d0b5eSDimitry Andric return thread.get();
1120cac4ca3SEd Maste }
113ef5d0b5eSDimitry Andric return nullptr;
1140cac4ca3SEd Maste }
1150cac4ca3SEd Maste
GetThreadByID(lldb::tid_t tid)116ef5d0b5eSDimitry Andric NativeThreadProtocol *NativeProcessProtocol::GetThreadByID(lldb::tid_t tid) {
117f3fbd1c0SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_threads_mutex);
118205afe67SEd Maste return GetThreadByIDUnlocked(tid);
119205afe67SEd Maste }
120205afe67SEd Maste
IsAlive() const12114f1b3e8SDimitry Andric bool NativeProcessProtocol::IsAlive() const {
12214f1b3e8SDimitry Andric return m_state != eStateDetached && m_state != eStateExited &&
12314f1b3e8SDimitry Andric m_state != eStateInvalid && m_state != eStateUnloaded;
1240cac4ca3SEd Maste }
1250cac4ca3SEd Maste
12612bd4897SEd Maste const NativeWatchpointList::WatchpointMap &
GetWatchpointMap() const12714f1b3e8SDimitry Andric NativeProcessProtocol::GetWatchpointMap() const {
12812bd4897SEd Maste return m_watchpoint_list.GetWatchpointMap();
12912bd4897SEd Maste }
13012bd4897SEd Maste
131e3b55780SDimitry Andric std::optional<std::pair<uint32_t, uint32_t>>
GetHardwareDebugSupportInfo() const13274a628f7SDimitry Andric NativeProcessProtocol::GetHardwareDebugSupportInfo() const {
133145449b1SDimitry Andric Log *log = GetLog(LLDBLog::Process);
1340cac4ca3SEd Maste
1350cac4ca3SEd Maste // get any thread
136ef5d0b5eSDimitry Andric NativeThreadProtocol *thread(
13714f1b3e8SDimitry Andric const_cast<NativeProcessProtocol *>(this)->GetThreadAtIndex(0));
138ef5d0b5eSDimitry Andric if (!thread) {
139ef5d0b5eSDimitry Andric LLDB_LOG(log, "failed to find a thread to grab a NativeRegisterContext!");
140e3b55780SDimitry Andric return std::nullopt;
1410cac4ca3SEd Maste }
1420cac4ca3SEd Maste
143ef5d0b5eSDimitry Andric NativeRegisterContext ®_ctx = thread->GetRegisterContext();
144ef5d0b5eSDimitry Andric return std::make_pair(reg_ctx.NumSupportedHardwareBreakpoints(),
145ef5d0b5eSDimitry Andric reg_ctx.NumSupportedHardwareWatchpoints());
1460cac4ca3SEd Maste }
1470cac4ca3SEd Maste
SetWatchpoint(lldb::addr_t addr,size_t size,uint32_t watch_flags,bool hardware)148b76161e4SDimitry Andric Status NativeProcessProtocol::SetWatchpoint(lldb::addr_t addr, size_t size,
14914f1b3e8SDimitry Andric uint32_t watch_flags,
15014f1b3e8SDimitry Andric bool hardware) {
151f73363f1SDimitry Andric // This default implementation assumes setting the watchpoint for the process
152f73363f1SDimitry Andric // will require setting the watchpoint for each of the threads. Furthermore,
153f73363f1SDimitry Andric // it will track watchpoints set for the process and will add them to each
154f73363f1SDimitry Andric // thread that is attached to via the (FIXME implement) OnThreadAttached ()
155f73363f1SDimitry Andric // method.
1560cac4ca3SEd Maste
157145449b1SDimitry Andric Log *log = GetLog(LLDBLog::Process);
1580cac4ca3SEd Maste
1590cac4ca3SEd Maste // Update the thread list
1600cac4ca3SEd Maste UpdateThreads();
1610cac4ca3SEd Maste
162f73363f1SDimitry Andric // Keep track of the threads we successfully set the watchpoint for. If one
163f73363f1SDimitry Andric // of the thread watchpoint setting operations fails, back off and remove the
164f73363f1SDimitry Andric // watchpoint for all the threads that were successfully set so we get back
165f73363f1SDimitry Andric // to a consistent state.
166ef5d0b5eSDimitry Andric std::vector<NativeThreadProtocol *> watchpoint_established_threads;
1670cac4ca3SEd Maste
168f73363f1SDimitry Andric // Tell each thread to set a watchpoint. In the event that hardware
169f73363f1SDimitry Andric // watchpoints are requested but the SetWatchpoint fails, try to set a
170f73363f1SDimitry Andric // software watchpoint as a fallback. It's conceivable that if there are
171f73363f1SDimitry Andric // more threads than hardware watchpoints available, some of the threads will
172f73363f1SDimitry Andric // fail to set hardware watchpoints while software ones may be available.
173f3fbd1c0SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_threads_mutex);
174ef5d0b5eSDimitry Andric for (const auto &thread : m_threads) {
175ef5d0b5eSDimitry Andric assert(thread && "thread list should not have a NULL thread!");
1760cac4ca3SEd Maste
177b76161e4SDimitry Andric Status thread_error =
178ef5d0b5eSDimitry Andric thread->SetWatchpoint(addr, size, watch_flags, hardware);
17914f1b3e8SDimitry Andric if (thread_error.Fail() && hardware) {
180f73363f1SDimitry Andric // Try software watchpoints since we failed on hardware watchpoint
181f73363f1SDimitry Andric // setting and we may have just run out of hardware watchpoints.
182ef5d0b5eSDimitry Andric thread_error = thread->SetWatchpoint(addr, size, watch_flags, false);
183ef5d0b5eSDimitry Andric if (thread_error.Success())
184ef5d0b5eSDimitry Andric LLDB_LOG(log,
18514f1b3e8SDimitry Andric "hardware watchpoint requested but software watchpoint set");
1860cac4ca3SEd Maste }
1870cac4ca3SEd Maste
18814f1b3e8SDimitry Andric if (thread_error.Success()) {
189f73363f1SDimitry Andric // Remember that we set this watchpoint successfully in case we need to
190f73363f1SDimitry Andric // clear it later.
191ef5d0b5eSDimitry Andric watchpoint_established_threads.push_back(thread.get());
19214f1b3e8SDimitry Andric } else {
193f73363f1SDimitry Andric // Unset the watchpoint for each thread we successfully set so that we
194f73363f1SDimitry Andric // get back to a consistent state of "not set" for the watchpoint.
19514f1b3e8SDimitry Andric for (auto unwatch_thread_sp : watchpoint_established_threads) {
196b76161e4SDimitry Andric Status remove_error = unwatch_thread_sp->RemoveWatchpoint(addr);
197ef5d0b5eSDimitry Andric if (remove_error.Fail())
198ef5d0b5eSDimitry Andric LLDB_LOG(log, "RemoveWatchpoint failed for pid={0}, tid={1}: {2}",
199ef5d0b5eSDimitry Andric GetID(), unwatch_thread_sp->GetID(), remove_error);
2000cac4ca3SEd Maste }
2010cac4ca3SEd Maste
2020cac4ca3SEd Maste return thread_error;
2030cac4ca3SEd Maste }
2040cac4ca3SEd Maste }
20512bd4897SEd Maste return m_watchpoint_list.Add(addr, size, watch_flags, hardware);
2060cac4ca3SEd Maste }
2070cac4ca3SEd Maste
RemoveWatchpoint(lldb::addr_t addr)208b76161e4SDimitry Andric Status NativeProcessProtocol::RemoveWatchpoint(lldb::addr_t addr) {
2090cac4ca3SEd Maste // Update the thread list
2100cac4ca3SEd Maste UpdateThreads();
2110cac4ca3SEd Maste
212b76161e4SDimitry Andric Status overall_error;
2130cac4ca3SEd Maste
214f3fbd1c0SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_threads_mutex);
215ef5d0b5eSDimitry Andric for (const auto &thread : m_threads) {
216ef5d0b5eSDimitry Andric assert(thread && "thread list should not have a NULL thread!");
2170cac4ca3SEd Maste
218ef5d0b5eSDimitry Andric const Status thread_error = thread->RemoveWatchpoint(addr);
21914f1b3e8SDimitry Andric if (thread_error.Fail()) {
220f73363f1SDimitry Andric // Keep track of the first thread error if any threads fail. We want to
221f73363f1SDimitry Andric // try to remove the watchpoint from every thread, though, even if one or
222f73363f1SDimitry Andric // more have errors.
2230cac4ca3SEd Maste if (!overall_error.Fail())
2240cac4ca3SEd Maste overall_error = thread_error;
2250cac4ca3SEd Maste }
2260cac4ca3SEd Maste }
227b76161e4SDimitry Andric const Status error = m_watchpoint_list.Remove(addr);
22812bd4897SEd Maste return overall_error.Fail() ? overall_error : error;
2290cac4ca3SEd Maste }
2300cac4ca3SEd Maste
23174a628f7SDimitry Andric const HardwareBreakpointMap &
GetHardwareBreakpointMap() const23274a628f7SDimitry Andric NativeProcessProtocol::GetHardwareBreakpointMap() const {
23374a628f7SDimitry Andric return m_hw_breakpoints_map;
23474a628f7SDimitry Andric }
23574a628f7SDimitry Andric
SetHardwareBreakpoint(lldb::addr_t addr,size_t size)236b76161e4SDimitry Andric Status NativeProcessProtocol::SetHardwareBreakpoint(lldb::addr_t addr,
23774a628f7SDimitry Andric size_t size) {
238f73363f1SDimitry Andric // This default implementation assumes setting a hardware breakpoint for this
239f73363f1SDimitry Andric // process will require setting same hardware breakpoint for each of its
240f73363f1SDimitry Andric // existing threads. New thread will do the same once created.
241145449b1SDimitry Andric Log *log = GetLog(LLDBLog::Process);
24274a628f7SDimitry Andric
24374a628f7SDimitry Andric // Update the thread list
24474a628f7SDimitry Andric UpdateThreads();
24574a628f7SDimitry Andric
24674a628f7SDimitry Andric // Exit here if target does not have required hardware breakpoint capability.
24774a628f7SDimitry Andric auto hw_debug_cap = GetHardwareDebugSupportInfo();
24874a628f7SDimitry Andric
249e3b55780SDimitry Andric if (hw_debug_cap == std::nullopt || hw_debug_cap->first == 0 ||
25074a628f7SDimitry Andric hw_debug_cap->first <= m_hw_breakpoints_map.size())
251b76161e4SDimitry Andric return Status("Target does not have required no of hardware breakpoints");
25274a628f7SDimitry Andric
25374a628f7SDimitry Andric // Vector below stores all thread pointer for which we have we successfully
25474a628f7SDimitry Andric // set this hardware breakpoint. If any of the current process threads fails
25574a628f7SDimitry Andric // to set this hardware breakpoint then roll back and remove this breakpoint
25674a628f7SDimitry Andric // for all the threads that had already set it successfully.
257ef5d0b5eSDimitry Andric std::vector<NativeThreadProtocol *> breakpoint_established_threads;
25874a628f7SDimitry Andric
25974a628f7SDimitry Andric // Request to set a hardware breakpoint for each of current process threads.
26074a628f7SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_threads_mutex);
261ef5d0b5eSDimitry Andric for (const auto &thread : m_threads) {
262ef5d0b5eSDimitry Andric assert(thread && "thread list should not have a NULL thread!");
26374a628f7SDimitry Andric
264ef5d0b5eSDimitry Andric Status thread_error = thread->SetHardwareBreakpoint(addr, size);
26574a628f7SDimitry Andric if (thread_error.Success()) {
266f73363f1SDimitry Andric // Remember that we set this breakpoint successfully in case we need to
267f73363f1SDimitry Andric // clear it later.
268ef5d0b5eSDimitry Andric breakpoint_established_threads.push_back(thread.get());
26974a628f7SDimitry Andric } else {
270f73363f1SDimitry Andric // Unset the breakpoint for each thread we successfully set so that we
271f73363f1SDimitry Andric // get back to a consistent state of "not set" for this hardware
272f73363f1SDimitry Andric // breakpoint.
27374a628f7SDimitry Andric for (auto rollback_thread_sp : breakpoint_established_threads) {
274b76161e4SDimitry Andric Status remove_error =
275b76161e4SDimitry Andric rollback_thread_sp->RemoveHardwareBreakpoint(addr);
276ef5d0b5eSDimitry Andric if (remove_error.Fail())
277ef5d0b5eSDimitry Andric LLDB_LOG(log,
278ef5d0b5eSDimitry Andric "RemoveHardwareBreakpoint failed for pid={0}, tid={1}: {2}",
279ef5d0b5eSDimitry Andric GetID(), rollback_thread_sp->GetID(), remove_error);
28074a628f7SDimitry Andric }
28174a628f7SDimitry Andric
28274a628f7SDimitry Andric return thread_error;
28374a628f7SDimitry Andric }
28474a628f7SDimitry Andric }
28574a628f7SDimitry Andric
28674a628f7SDimitry Andric // Register new hardware breakpoint into hardware breakpoints map of current
28774a628f7SDimitry Andric // process.
28874a628f7SDimitry Andric m_hw_breakpoints_map[addr] = {addr, size};
28974a628f7SDimitry Andric
290b76161e4SDimitry Andric return Status();
29174a628f7SDimitry Andric }
29274a628f7SDimitry Andric
RemoveHardwareBreakpoint(lldb::addr_t addr)293b76161e4SDimitry Andric Status NativeProcessProtocol::RemoveHardwareBreakpoint(lldb::addr_t addr) {
29474a628f7SDimitry Andric // Update the thread list
29574a628f7SDimitry Andric UpdateThreads();
29674a628f7SDimitry Andric
297b76161e4SDimitry Andric Status error;
29874a628f7SDimitry Andric
29974a628f7SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_threads_mutex);
300ef5d0b5eSDimitry Andric for (const auto &thread : m_threads) {
301ef5d0b5eSDimitry Andric assert(thread && "thread list should not have a NULL thread!");
302ef5d0b5eSDimitry Andric error = thread->RemoveHardwareBreakpoint(addr);
30374a628f7SDimitry Andric }
30474a628f7SDimitry Andric
30574a628f7SDimitry Andric // Also remove from hardware breakpoint map of current process.
30674a628f7SDimitry Andric m_hw_breakpoints_map.erase(addr);
30774a628f7SDimitry Andric
30874a628f7SDimitry Andric return error;
30974a628f7SDimitry Andric }
31074a628f7SDimitry Andric
SynchronouslyNotifyProcessStateChanged(lldb::StateType state)31114f1b3e8SDimitry Andric void NativeProcessProtocol::SynchronouslyNotifyProcessStateChanged(
31214f1b3e8SDimitry Andric lldb::StateType state) {
313145449b1SDimitry Andric Log *log = GetLog(LLDBLog::Process);
3140cac4ca3SEd Maste
315344a3780SDimitry Andric m_delegate.ProcessStateChanged(this, state);
3160cac4ca3SEd Maste
317145449b1SDimitry Andric switch (state) {
318145449b1SDimitry Andric case eStateStopped:
319145449b1SDimitry Andric case eStateExited:
320145449b1SDimitry Andric case eStateCrashed:
321145449b1SDimitry Andric NotifyTracersProcessDidStop();
322145449b1SDimitry Andric break;
323145449b1SDimitry Andric default:
324145449b1SDimitry Andric break;
325145449b1SDimitry Andric }
326145449b1SDimitry Andric
327344a3780SDimitry Andric LLDB_LOG(log, "sent state notification [{0}] from process {1}", state,
328344a3780SDimitry Andric GetID());
3290cac4ca3SEd Maste }
3300cac4ca3SEd Maste
NotifyDidExec()33114f1b3e8SDimitry Andric void NativeProcessProtocol::NotifyDidExec() {
332145449b1SDimitry Andric Log *log = GetLog(LLDBLog::Process);
333344a3780SDimitry Andric LLDB_LOG(log, "process {0} exec()ed", GetID());
3340cac4ca3SEd Maste
335145449b1SDimitry Andric m_software_breakpoints.clear();
336145449b1SDimitry Andric
337344a3780SDimitry Andric m_delegate.DidExec(this);
3380cac4ca3SEd Maste }
3390cac4ca3SEd Maste
SetSoftwareBreakpoint(lldb::addr_t addr,uint32_t size_hint)340b76161e4SDimitry Andric Status NativeProcessProtocol::SetSoftwareBreakpoint(lldb::addr_t addr,
34114f1b3e8SDimitry Andric uint32_t size_hint) {
342145449b1SDimitry Andric Log *log = GetLog(LLDBLog::Breakpoints);
34394994d37SDimitry Andric LLDB_LOG(log, "addr = {0:x}, size_hint = {1}", addr, size_hint);
3440cac4ca3SEd Maste
34594994d37SDimitry Andric auto it = m_software_breakpoints.find(addr);
34694994d37SDimitry Andric if (it != m_software_breakpoints.end()) {
34794994d37SDimitry Andric ++it->second.ref_count;
34894994d37SDimitry Andric return Status();
34994994d37SDimitry Andric }
35094994d37SDimitry Andric auto expected_bkpt = EnableSoftwareBreakpoint(addr, size_hint);
35194994d37SDimitry Andric if (!expected_bkpt)
35294994d37SDimitry Andric return Status(expected_bkpt.takeError());
35394994d37SDimitry Andric
35494994d37SDimitry Andric m_software_breakpoints.emplace(addr, std::move(*expected_bkpt));
35594994d37SDimitry Andric return Status();
35694994d37SDimitry Andric }
35794994d37SDimitry Andric
RemoveSoftwareBreakpoint(lldb::addr_t addr)35894994d37SDimitry Andric Status NativeProcessProtocol::RemoveSoftwareBreakpoint(lldb::addr_t addr) {
359145449b1SDimitry Andric Log *log = GetLog(LLDBLog::Breakpoints);
36094994d37SDimitry Andric LLDB_LOG(log, "addr = {0:x}", addr);
36194994d37SDimitry Andric auto it = m_software_breakpoints.find(addr);
36294994d37SDimitry Andric if (it == m_software_breakpoints.end())
36394994d37SDimitry Andric return Status("Breakpoint not found.");
36494994d37SDimitry Andric assert(it->second.ref_count > 0);
36594994d37SDimitry Andric if (--it->second.ref_count > 0)
36694994d37SDimitry Andric return Status();
36794994d37SDimitry Andric
36894994d37SDimitry Andric // This is the last reference. Let's remove the breakpoint.
36994994d37SDimitry Andric Status error;
37094994d37SDimitry Andric
37194994d37SDimitry Andric // Clear a software breakpoint instruction
37294994d37SDimitry Andric llvm::SmallVector<uint8_t, 4> curr_break_op(
37394994d37SDimitry Andric it->second.breakpoint_opcodes.size(), 0);
37494994d37SDimitry Andric
37594994d37SDimitry Andric // Read the breakpoint opcode
37694994d37SDimitry Andric size_t bytes_read = 0;
37794994d37SDimitry Andric error =
37894994d37SDimitry Andric ReadMemory(addr, curr_break_op.data(), curr_break_op.size(), bytes_read);
37994994d37SDimitry Andric if (error.Fail() || bytes_read < curr_break_op.size()) {
38094994d37SDimitry Andric return Status("addr=0x%" PRIx64
38194994d37SDimitry Andric ": tried to read %zu bytes but only read %zu",
38294994d37SDimitry Andric addr, curr_break_op.size(), bytes_read);
38394994d37SDimitry Andric }
38494994d37SDimitry Andric const auto &saved = it->second.saved_opcodes;
38594994d37SDimitry Andric // Make sure the breakpoint opcode exists at this address
386e3b55780SDimitry Andric if (llvm::ArrayRef(curr_break_op) != it->second.breakpoint_opcodes) {
38794994d37SDimitry Andric if (curr_break_op != it->second.saved_opcodes)
38894994d37SDimitry Andric return Status("Original breakpoint trap is no longer in memory.");
38994994d37SDimitry Andric LLDB_LOG(log,
39094994d37SDimitry Andric "Saved opcodes ({0:@[x]}) have already been restored at {1:x}.",
39194994d37SDimitry Andric llvm::make_range(saved.begin(), saved.end()), addr);
39294994d37SDimitry Andric } else {
39394994d37SDimitry Andric // We found a valid breakpoint opcode at this address, now restore the
39494994d37SDimitry Andric // saved opcode.
39594994d37SDimitry Andric size_t bytes_written = 0;
39694994d37SDimitry Andric error = WriteMemory(addr, saved.data(), saved.size(), bytes_written);
39794994d37SDimitry Andric if (error.Fail() || bytes_written < saved.size()) {
39894994d37SDimitry Andric return Status("addr=0x%" PRIx64
39994994d37SDimitry Andric ": tried to write %zu bytes but only wrote %zu",
40094994d37SDimitry Andric addr, saved.size(), bytes_written);
40194994d37SDimitry Andric }
40294994d37SDimitry Andric
40394994d37SDimitry Andric // Verify that our original opcode made it back to the inferior
40494994d37SDimitry Andric llvm::SmallVector<uint8_t, 4> verify_opcode(saved.size(), 0);
40594994d37SDimitry Andric size_t verify_bytes_read = 0;
40694994d37SDimitry Andric error = ReadMemory(addr, verify_opcode.data(), verify_opcode.size(),
40794994d37SDimitry Andric verify_bytes_read);
40894994d37SDimitry Andric if (error.Fail() || verify_bytes_read < verify_opcode.size()) {
40994994d37SDimitry Andric return Status("addr=0x%" PRIx64
41094994d37SDimitry Andric ": tried to read %zu verification bytes but only read %zu",
41194994d37SDimitry Andric addr, verify_opcode.size(), verify_bytes_read);
41294994d37SDimitry Andric }
41394994d37SDimitry Andric if (verify_opcode != saved)
41494994d37SDimitry Andric LLDB_LOG(log, "Restoring bytes at {0:x}: {1:@[x]}", addr,
41594994d37SDimitry Andric llvm::make_range(saved.begin(), saved.end()));
41694994d37SDimitry Andric }
41794994d37SDimitry Andric
41894994d37SDimitry Andric m_software_breakpoints.erase(it);
41994994d37SDimitry Andric return Status();
42094994d37SDimitry Andric }
42194994d37SDimitry Andric
42294994d37SDimitry Andric llvm::Expected<NativeProcessProtocol::SoftwareBreakpoint>
EnableSoftwareBreakpoint(lldb::addr_t addr,uint32_t size_hint)42394994d37SDimitry Andric NativeProcessProtocol::EnableSoftwareBreakpoint(lldb::addr_t addr,
42494994d37SDimitry Andric uint32_t size_hint) {
425145449b1SDimitry Andric Log *log = GetLog(LLDBLog::Breakpoints);
42694994d37SDimitry Andric
42794994d37SDimitry Andric auto expected_trap = GetSoftwareBreakpointTrapOpcode(size_hint);
42894994d37SDimitry Andric if (!expected_trap)
42994994d37SDimitry Andric return expected_trap.takeError();
43094994d37SDimitry Andric
43194994d37SDimitry Andric llvm::SmallVector<uint8_t, 4> saved_opcode_bytes(expected_trap->size(), 0);
43294994d37SDimitry Andric // Save the original opcodes by reading them so we can restore later.
43394994d37SDimitry Andric size_t bytes_read = 0;
43494994d37SDimitry Andric Status error = ReadMemory(addr, saved_opcode_bytes.data(),
43594994d37SDimitry Andric saved_opcode_bytes.size(), bytes_read);
43694994d37SDimitry Andric if (error.Fail())
43794994d37SDimitry Andric return error.ToError();
43894994d37SDimitry Andric
43994994d37SDimitry Andric // Ensure we read as many bytes as we expected.
44094994d37SDimitry Andric if (bytes_read != saved_opcode_bytes.size()) {
44194994d37SDimitry Andric return llvm::createStringError(
44294994d37SDimitry Andric llvm::inconvertibleErrorCode(),
44394994d37SDimitry Andric "Failed to read memory while attempting to set breakpoint: attempted "
44494994d37SDimitry Andric "to read {0} bytes but only read {1}.",
44594994d37SDimitry Andric saved_opcode_bytes.size(), bytes_read);
44694994d37SDimitry Andric }
44794994d37SDimitry Andric
44894994d37SDimitry Andric LLDB_LOG(
44994994d37SDimitry Andric log, "Overwriting bytes at {0:x}: {1:@[x]}", addr,
45094994d37SDimitry Andric llvm::make_range(saved_opcode_bytes.begin(), saved_opcode_bytes.end()));
45194994d37SDimitry Andric
45294994d37SDimitry Andric // Write a software breakpoint in place of the original opcode.
45394994d37SDimitry Andric size_t bytes_written = 0;
45494994d37SDimitry Andric error = WriteMemory(addr, expected_trap->data(), expected_trap->size(),
45594994d37SDimitry Andric bytes_written);
45694994d37SDimitry Andric if (error.Fail())
45794994d37SDimitry Andric return error.ToError();
45894994d37SDimitry Andric
45994994d37SDimitry Andric // Ensure we wrote as many bytes as we expected.
46094994d37SDimitry Andric if (bytes_written != expected_trap->size()) {
46194994d37SDimitry Andric return llvm::createStringError(
46294994d37SDimitry Andric llvm::inconvertibleErrorCode(),
46394994d37SDimitry Andric "Failed write memory while attempting to set "
46494994d37SDimitry Andric "breakpoint: attempted to write {0} bytes but only wrote {1}",
46594994d37SDimitry Andric expected_trap->size(), bytes_written);
46694994d37SDimitry Andric }
46794994d37SDimitry Andric
46894994d37SDimitry Andric llvm::SmallVector<uint8_t, 4> verify_bp_opcode_bytes(expected_trap->size(),
46994994d37SDimitry Andric 0);
47094994d37SDimitry Andric size_t verify_bytes_read = 0;
47194994d37SDimitry Andric error = ReadMemory(addr, verify_bp_opcode_bytes.data(),
47294994d37SDimitry Andric verify_bp_opcode_bytes.size(), verify_bytes_read);
47394994d37SDimitry Andric if (error.Fail())
47494994d37SDimitry Andric return error.ToError();
47594994d37SDimitry Andric
47694994d37SDimitry Andric // Ensure we read as many verification bytes as we expected.
47794994d37SDimitry Andric if (verify_bytes_read != verify_bp_opcode_bytes.size()) {
47894994d37SDimitry Andric return llvm::createStringError(
47994994d37SDimitry Andric llvm::inconvertibleErrorCode(),
48094994d37SDimitry Andric "Failed to read memory while "
48194994d37SDimitry Andric "attempting to verify breakpoint: attempted to read {0} bytes "
48294994d37SDimitry Andric "but only read {1}",
48394994d37SDimitry Andric verify_bp_opcode_bytes.size(), verify_bytes_read);
48494994d37SDimitry Andric }
48594994d37SDimitry Andric
486e3b55780SDimitry Andric if (llvm::ArrayRef(verify_bp_opcode_bytes.data(), verify_bytes_read) !=
48794994d37SDimitry Andric *expected_trap) {
48894994d37SDimitry Andric return llvm::createStringError(
48994994d37SDimitry Andric llvm::inconvertibleErrorCode(),
49094994d37SDimitry Andric "Verification of software breakpoint "
49194994d37SDimitry Andric "writing failed - trap opcodes not successfully read back "
49294994d37SDimitry Andric "after writing when setting breakpoint at {0:x}",
49394994d37SDimitry Andric addr);
49494994d37SDimitry Andric }
49594994d37SDimitry Andric
49694994d37SDimitry Andric LLDB_LOG(log, "addr = {0:x}: SUCCESS", addr);
49794994d37SDimitry Andric return SoftwareBreakpoint{1, saved_opcode_bytes, *expected_trap};
49894994d37SDimitry Andric }
49994994d37SDimitry Andric
50094994d37SDimitry Andric llvm::Expected<llvm::ArrayRef<uint8_t>>
GetSoftwareBreakpointTrapOpcode(size_t size_hint)50194994d37SDimitry Andric NativeProcessProtocol::GetSoftwareBreakpointTrapOpcode(size_t size_hint) {
50294994d37SDimitry Andric static const uint8_t g_aarch64_opcode[] = {0x00, 0x00, 0x20, 0xd4};
50394994d37SDimitry Andric static const uint8_t g_i386_opcode[] = {0xCC};
50494994d37SDimitry Andric static const uint8_t g_mips64_opcode[] = {0x00, 0x00, 0x00, 0x0d};
50594994d37SDimitry Andric static const uint8_t g_mips64el_opcode[] = {0x0d, 0x00, 0x00, 0x00};
5067fa27ce4SDimitry Andric static const uint8_t g_msp430_opcode[] = {0x43, 0x43};
50794994d37SDimitry Andric static const uint8_t g_s390x_opcode[] = {0x00, 0x01};
508344a3780SDimitry Andric static const uint8_t g_ppc_opcode[] = {0x7f, 0xe0, 0x00, 0x08}; // trap
509344a3780SDimitry Andric static const uint8_t g_ppcle_opcode[] = {0x08, 0x00, 0xe0, 0x7f}; // trap
510e3b55780SDimitry Andric static const uint8_t g_riscv_opcode[] = {0x73, 0x00, 0x10, 0x00}; // ebreak
511e3b55780SDimitry Andric static const uint8_t g_riscv_opcode_c[] = {0x02, 0x90}; // c.ebreak
512e3b55780SDimitry Andric static const uint8_t g_loongarch_opcode[] = {0x05, 0x00, 0x2a,
513e3b55780SDimitry Andric 0x00}; // break 0x5
51494994d37SDimitry Andric
51594994d37SDimitry Andric switch (GetArchitecture().GetMachine()) {
51694994d37SDimitry Andric case llvm::Triple::aarch64:
517ead24645SDimitry Andric case llvm::Triple::aarch64_32:
518e3b55780SDimitry Andric return llvm::ArrayRef(g_aarch64_opcode);
51994994d37SDimitry Andric
52094994d37SDimitry Andric case llvm::Triple::x86:
52194994d37SDimitry Andric case llvm::Triple::x86_64:
522e3b55780SDimitry Andric return llvm::ArrayRef(g_i386_opcode);
52394994d37SDimitry Andric
52494994d37SDimitry Andric case llvm::Triple::mips:
52594994d37SDimitry Andric case llvm::Triple::mips64:
526e3b55780SDimitry Andric return llvm::ArrayRef(g_mips64_opcode);
52794994d37SDimitry Andric
52894994d37SDimitry Andric case llvm::Triple::mipsel:
52994994d37SDimitry Andric case llvm::Triple::mips64el:
530e3b55780SDimitry Andric return llvm::ArrayRef(g_mips64el_opcode);
53194994d37SDimitry Andric
5327fa27ce4SDimitry Andric case llvm::Triple::msp430:
5337fa27ce4SDimitry Andric return llvm::ArrayRef(g_msp430_opcode);
5347fa27ce4SDimitry Andric
53594994d37SDimitry Andric case llvm::Triple::systemz:
536e3b55780SDimitry Andric return llvm::ArrayRef(g_s390x_opcode);
53794994d37SDimitry Andric
538344a3780SDimitry Andric case llvm::Triple::ppc:
539344a3780SDimitry Andric case llvm::Triple::ppc64:
540e3b55780SDimitry Andric return llvm::ArrayRef(g_ppc_opcode);
541344a3780SDimitry Andric
54294994d37SDimitry Andric case llvm::Triple::ppc64le:
543e3b55780SDimitry Andric return llvm::ArrayRef(g_ppcle_opcode);
544e3b55780SDimitry Andric
545e3b55780SDimitry Andric case llvm::Triple::riscv32:
546e3b55780SDimitry Andric case llvm::Triple::riscv64: {
547e3b55780SDimitry Andric return size_hint == 2 ? llvm::ArrayRef(g_riscv_opcode_c)
548e3b55780SDimitry Andric : llvm::ArrayRef(g_riscv_opcode);
549e3b55780SDimitry Andric }
550e3b55780SDimitry Andric
551e3b55780SDimitry Andric case llvm::Triple::loongarch32:
552e3b55780SDimitry Andric case llvm::Triple::loongarch64:
553e3b55780SDimitry Andric return llvm::ArrayRef(g_loongarch_opcode);
55494994d37SDimitry Andric
55594994d37SDimitry Andric default:
55694994d37SDimitry Andric return llvm::createStringError(llvm::inconvertibleErrorCode(),
55794994d37SDimitry Andric "CPU type not supported!");
55894994d37SDimitry Andric }
55994994d37SDimitry Andric }
56094994d37SDimitry Andric
GetSoftwareBreakpointPCOffset()56194994d37SDimitry Andric size_t NativeProcessProtocol::GetSoftwareBreakpointPCOffset() {
56294994d37SDimitry Andric switch (GetArchitecture().GetMachine()) {
56394994d37SDimitry Andric case llvm::Triple::x86:
56494994d37SDimitry Andric case llvm::Triple::x86_64:
56594994d37SDimitry Andric case llvm::Triple::systemz:
56694994d37SDimitry Andric // These architectures report increment the PC after breakpoint is hit.
56794994d37SDimitry Andric return cantFail(GetSoftwareBreakpointTrapOpcode(0)).size();
56894994d37SDimitry Andric
56994994d37SDimitry Andric case llvm::Triple::arm:
57094994d37SDimitry Andric case llvm::Triple::aarch64:
571ead24645SDimitry Andric case llvm::Triple::aarch64_32:
57294994d37SDimitry Andric case llvm::Triple::mips64:
57394994d37SDimitry Andric case llvm::Triple::mips64el:
57494994d37SDimitry Andric case llvm::Triple::mips:
57594994d37SDimitry Andric case llvm::Triple::mipsel:
576344a3780SDimitry Andric case llvm::Triple::ppc:
577344a3780SDimitry Andric case llvm::Triple::ppc64:
57894994d37SDimitry Andric case llvm::Triple::ppc64le:
579e3b55780SDimitry Andric case llvm::Triple::riscv32:
580e3b55780SDimitry Andric case llvm::Triple::riscv64:
581e3b55780SDimitry Andric case llvm::Triple::loongarch32:
582e3b55780SDimitry Andric case llvm::Triple::loongarch64:
58394994d37SDimitry Andric // On these architectures the PC doesn't get updated for breakpoint hits.
58494994d37SDimitry Andric return 0;
58594994d37SDimitry Andric
58694994d37SDimitry Andric default:
58794994d37SDimitry Andric llvm_unreachable("CPU type not supported!");
58894994d37SDimitry Andric }
58994994d37SDimitry Andric }
59094994d37SDimitry Andric
FixupBreakpointPCAsNeeded(NativeThreadProtocol & thread)59194994d37SDimitry Andric void NativeProcessProtocol::FixupBreakpointPCAsNeeded(
59294994d37SDimitry Andric NativeThreadProtocol &thread) {
593145449b1SDimitry Andric Log *log = GetLog(LLDBLog::Breakpoints);
59494994d37SDimitry Andric
59594994d37SDimitry Andric Status error;
59694994d37SDimitry Andric
59794994d37SDimitry Andric // Find out the size of a breakpoint (might depend on where we are in the
59894994d37SDimitry Andric // code).
59994994d37SDimitry Andric NativeRegisterContext &context = thread.GetRegisterContext();
60094994d37SDimitry Andric
60194994d37SDimitry Andric uint32_t breakpoint_size = GetSoftwareBreakpointPCOffset();
60294994d37SDimitry Andric LLDB_LOG(log, "breakpoint size: {0}", breakpoint_size);
60394994d37SDimitry Andric if (breakpoint_size == 0)
60494994d37SDimitry Andric return;
60594994d37SDimitry Andric
60694994d37SDimitry Andric // First try probing for a breakpoint at a software breakpoint location: PC -
60794994d37SDimitry Andric // breakpoint size.
60894994d37SDimitry Andric const lldb::addr_t initial_pc_addr = context.GetPCfromBreakpointLocation();
60994994d37SDimitry Andric lldb::addr_t breakpoint_addr = initial_pc_addr;
61094994d37SDimitry Andric // Do not allow breakpoint probe to wrap around.
61194994d37SDimitry Andric if (breakpoint_addr >= breakpoint_size)
61294994d37SDimitry Andric breakpoint_addr -= breakpoint_size;
61394994d37SDimitry Andric
61494994d37SDimitry Andric if (m_software_breakpoints.count(breakpoint_addr) == 0) {
61594994d37SDimitry Andric // We didn't find one at a software probe location. Nothing to do.
61694994d37SDimitry Andric LLDB_LOG(log,
61794994d37SDimitry Andric "pid {0} no lldb software breakpoint found at current pc with "
61894994d37SDimitry Andric "adjustment: {1}",
61994994d37SDimitry Andric GetID(), breakpoint_addr);
62094994d37SDimitry Andric return;
62194994d37SDimitry Andric }
62294994d37SDimitry Andric
62394994d37SDimitry Andric //
62494994d37SDimitry Andric // We have a software breakpoint and need to adjust the PC.
62594994d37SDimitry Andric //
62694994d37SDimitry Andric
62794994d37SDimitry Andric // Change the program counter.
62894994d37SDimitry Andric LLDB_LOG(log, "pid {0} tid {1}: changing PC from {2:x} to {3:x}", GetID(),
62994994d37SDimitry Andric thread.GetID(), initial_pc_addr, breakpoint_addr);
63094994d37SDimitry Andric
63194994d37SDimitry Andric error = context.SetPC(breakpoint_addr);
63294994d37SDimitry Andric if (error.Fail()) {
63394994d37SDimitry Andric // This can happen in case the process was killed between the time we read
63494994d37SDimitry Andric // the PC and when we are updating it. There's nothing better to do than to
63594994d37SDimitry Andric // swallow the error.
63694994d37SDimitry Andric LLDB_LOG(log, "pid {0} tid {1}: failed to set PC: {2}", GetID(),
63794994d37SDimitry Andric thread.GetID(), error);
63894994d37SDimitry Andric }
6390cac4ca3SEd Maste }
6400cac4ca3SEd Maste
RemoveBreakpoint(lldb::addr_t addr,bool hardware)641b76161e4SDimitry Andric Status NativeProcessProtocol::RemoveBreakpoint(lldb::addr_t addr,
64274a628f7SDimitry Andric bool hardware) {
64374a628f7SDimitry Andric if (hardware)
64474a628f7SDimitry Andric return RemoveHardwareBreakpoint(addr);
64574a628f7SDimitry Andric else
64694994d37SDimitry Andric return RemoveSoftwareBreakpoint(addr);
6470cac4ca3SEd Maste }
6480cac4ca3SEd Maste
ReadMemoryWithoutTrap(lldb::addr_t addr,void * buf,size_t size,size_t & bytes_read)64994994d37SDimitry Andric Status NativeProcessProtocol::ReadMemoryWithoutTrap(lldb::addr_t addr,
65094994d37SDimitry Andric void *buf, size_t size,
65194994d37SDimitry Andric size_t &bytes_read) {
65294994d37SDimitry Andric Status error = ReadMemory(addr, buf, size, bytes_read);
65394994d37SDimitry Andric if (error.Fail())
65494994d37SDimitry Andric return error;
6550cac4ca3SEd Maste
656e3b55780SDimitry Andric llvm::MutableArrayRef data(static_cast<uint8_t *>(buf), bytes_read);
65794994d37SDimitry Andric for (const auto &pair : m_software_breakpoints) {
65894994d37SDimitry Andric lldb::addr_t bp_addr = pair.first;
659e3b55780SDimitry Andric auto saved_opcodes = llvm::ArrayRef(pair.second.saved_opcodes);
66094994d37SDimitry Andric
66194994d37SDimitry Andric if (bp_addr + saved_opcodes.size() < addr || addr + bytes_read <= bp_addr)
662cfca06d7SDimitry Andric continue; // Breakpoint not in range, ignore
66394994d37SDimitry Andric
66494994d37SDimitry Andric if (bp_addr < addr) {
66594994d37SDimitry Andric saved_opcodes = saved_opcodes.drop_front(addr - bp_addr);
66694994d37SDimitry Andric bp_addr = addr;
66794994d37SDimitry Andric }
66894994d37SDimitry Andric auto bp_data = data.drop_front(bp_addr - addr);
66994994d37SDimitry Andric std::copy_n(saved_opcodes.begin(),
67094994d37SDimitry Andric std::min(saved_opcodes.size(), bp_data.size()),
67194994d37SDimitry Andric bp_data.begin());
67294994d37SDimitry Andric }
67394994d37SDimitry Andric return Status();
6740cac4ca3SEd Maste }
6750cac4ca3SEd Maste
676ead24645SDimitry Andric llvm::Expected<llvm::StringRef>
ReadCStringFromMemory(lldb::addr_t addr,char * buffer,size_t max_size,size_t & total_bytes_read)677ead24645SDimitry Andric NativeProcessProtocol::ReadCStringFromMemory(lldb::addr_t addr, char *buffer,
678ead24645SDimitry Andric size_t max_size,
679ead24645SDimitry Andric size_t &total_bytes_read) {
680ead24645SDimitry Andric static const size_t cache_line_size =
681ead24645SDimitry Andric llvm::sys::Process::getPageSizeEstimate();
682ead24645SDimitry Andric size_t bytes_read = 0;
683ead24645SDimitry Andric size_t bytes_left = max_size;
684ead24645SDimitry Andric addr_t curr_addr = addr;
685ead24645SDimitry Andric size_t string_size;
686ead24645SDimitry Andric char *curr_buffer = buffer;
687ead24645SDimitry Andric total_bytes_read = 0;
688ead24645SDimitry Andric Status status;
689ead24645SDimitry Andric
690ead24645SDimitry Andric while (bytes_left > 0 && status.Success()) {
691ead24645SDimitry Andric addr_t cache_line_bytes_left =
692ead24645SDimitry Andric cache_line_size - (curr_addr % cache_line_size);
693ead24645SDimitry Andric addr_t bytes_to_read = std::min<addr_t>(bytes_left, cache_line_bytes_left);
694706b4fc4SDimitry Andric status = ReadMemory(curr_addr, static_cast<void *>(curr_buffer),
695ead24645SDimitry Andric bytes_to_read, bytes_read);
696ead24645SDimitry Andric
697ead24645SDimitry Andric if (bytes_read == 0)
698ead24645SDimitry Andric break;
699ead24645SDimitry Andric
700ead24645SDimitry Andric void *str_end = std::memchr(curr_buffer, '\0', bytes_read);
701ead24645SDimitry Andric if (str_end != nullptr) {
702ead24645SDimitry Andric total_bytes_read =
703706b4fc4SDimitry Andric static_cast<size_t>((static_cast<char *>(str_end) - buffer + 1));
704ead24645SDimitry Andric status.Clear();
705ead24645SDimitry Andric break;
706ead24645SDimitry Andric }
707ead24645SDimitry Andric
708ead24645SDimitry Andric total_bytes_read += bytes_read;
709ead24645SDimitry Andric curr_buffer += bytes_read;
710ead24645SDimitry Andric curr_addr += bytes_read;
711ead24645SDimitry Andric bytes_left -= bytes_read;
712ead24645SDimitry Andric }
713ead24645SDimitry Andric
714ead24645SDimitry Andric string_size = total_bytes_read - 1;
715ead24645SDimitry Andric
716ead24645SDimitry Andric // Make sure we return a null terminated string.
717ead24645SDimitry Andric if (bytes_left == 0 && max_size > 0 && buffer[max_size - 1] != '\0') {
718ead24645SDimitry Andric buffer[max_size - 1] = '\0';
719ead24645SDimitry Andric total_bytes_read--;
720ead24645SDimitry Andric }
721ead24645SDimitry Andric
722ead24645SDimitry Andric if (!status.Success())
723ead24645SDimitry Andric return status.ToError();
724ead24645SDimitry Andric
725ead24645SDimitry Andric return llvm::StringRef(buffer, string_size);
726ead24645SDimitry Andric }
727ead24645SDimitry Andric
GetState() const72814f1b3e8SDimitry Andric lldb::StateType NativeProcessProtocol::GetState() const {
729f3fbd1c0SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_state_mutex);
7300cac4ca3SEd Maste return m_state;
7310cac4ca3SEd Maste }
7320cac4ca3SEd Maste
SetState(lldb::StateType state,bool notify_delegates)73314f1b3e8SDimitry Andric void NativeProcessProtocol::SetState(lldb::StateType state,
73414f1b3e8SDimitry Andric bool notify_delegates) {
735f3fbd1c0SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_state_mutex);
73612bd4897SEd Maste
73712bd4897SEd Maste if (state == m_state)
73812bd4897SEd Maste return;
73912bd4897SEd Maste
7400cac4ca3SEd Maste m_state = state;
7410cac4ca3SEd Maste
74214f1b3e8SDimitry Andric if (StateIsStoppedState(state, false)) {
7430cac4ca3SEd Maste ++m_stop_id;
7440cac4ca3SEd Maste
7450cac4ca3SEd Maste // Give process a chance to do any stop id bump processing, such as
7460cac4ca3SEd Maste // clearing cached data that is invalidated each time the process runs.
747f73363f1SDimitry Andric // Note if/when we support some threads running, we'll end up needing to
748f73363f1SDimitry Andric // manage this per thread and per process.
7490cac4ca3SEd Maste DoStopIDBumped(m_stop_id);
7500cac4ca3SEd Maste }
7510cac4ca3SEd Maste
7520cac4ca3SEd Maste // Optionally notify delegates of the state change.
7530cac4ca3SEd Maste if (notify_delegates)
7540cac4ca3SEd Maste SynchronouslyNotifyProcessStateChanged(state);
7550cac4ca3SEd Maste }
7560cac4ca3SEd Maste
GetStopID() const75714f1b3e8SDimitry Andric uint32_t NativeProcessProtocol::GetStopID() const {
758f3fbd1c0SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_state_mutex);
7590cac4ca3SEd Maste return m_stop_id;
7600cac4ca3SEd Maste }
7610cac4ca3SEd Maste
DoStopIDBumped(uint32_t)76214f1b3e8SDimitry Andric void NativeProcessProtocol::DoStopIDBumped(uint32_t /* newBumpId */) {
7630cac4ca3SEd Maste // Default implementation does nothing.
7640cac4ca3SEd Maste }
7655e95aa85SEd Maste
7667fa27ce4SDimitry Andric NativeProcessProtocol::Manager::~Manager() = default;
767