20#include "llvm/Support/Process.h"
30 : m_pid(pid), m_delegate(delegate), m_terminal_fd(terminal_fd) {
37 error.SetErrorString(
"local host does not support signaling");
54 return Status(
"not implemented");
59 size_t len, std::vector<uint8_t> &tags) {
60 return Status(
"not implemented");
66 const std::vector<uint8_t> &tags) {
67 return Status(
"not implemented");
78 bool bNotifyStateChange) {
80 LLDB_LOG(log,
"status = {0}, notify = {1}", status, bNotifyStateChange);
87 LLDB_LOG(log,
"state is exited, but status not set");
94 if (bNotifyStateChange)
110 if (thread->GetID() == tid)
131std::optional<std::pair<uint32_t, uint32_t>>
139 LLDB_LOG(log,
"failed to find a thread to grab a NativeRegisterContext!");
149 uint32_t watch_flags,
166 std::vector<NativeThreadProtocol *> watchpoint_established_threads;
175 assert(thread &&
"thread list should not have a NULL thread!");
178 thread->SetWatchpoint(addr, size, watch_flags, hardware);
179 if (thread_error.
Fail() && hardware) {
182 thread_error = thread->SetWatchpoint(addr, size, watch_flags,
false);
185 "hardware watchpoint requested but software watchpoint set");
191 watchpoint_established_threads.push_back(thread.get());
195 for (
auto unwatch_thread_sp : watchpoint_established_threads) {
196 Status remove_error = unwatch_thread_sp->RemoveWatchpoint(addr);
197 if (remove_error.
Fail())
198 LLDB_LOG(log,
"RemoveWatchpoint failed for pid={0}, tid={1}: {2}",
199 GetID(), unwatch_thread_sp->GetID(), remove_error);
216 assert(thread &&
"thread list should not have a NULL thread!");
218 const Status thread_error = thread->RemoveWatchpoint(addr);
219 if (thread_error.
Fail()) {
223 if (!overall_error.
Fail())
224 overall_error = thread_error;
228 return overall_error.
Fail() ? overall_error :
error;
249 if (hw_debug_cap == std::nullopt || hw_debug_cap->first == 0 ||
251 return Status(
"Target does not have required no of hardware breakpoints");
257 std::vector<NativeThreadProtocol *> breakpoint_established_threads;
262 assert(thread &&
"thread list should not have a NULL thread!");
264 Status thread_error = thread->SetHardwareBreakpoint(addr, size);
268 breakpoint_established_threads.push_back(thread.get());
273 for (
auto rollback_thread_sp : breakpoint_established_threads) {
275 rollback_thread_sp->RemoveHardwareBreakpoint(addr);
276 if (remove_error.
Fail())
278 "RemoveHardwareBreakpoint failed for pid={0}, tid={1}: {2}",
279 GetID(), rollback_thread_sp->GetID(), remove_error);
301 assert(thread &&
"thread list should not have a NULL thread!");
302 error = thread->RemoveHardwareBreakpoint(addr);
327 LLDB_LOG(log,
"sent state notification [{0}] from process {1}", state,
341 uint32_t size_hint) {
343 LLDB_LOG(log,
"addr = {0:x}, size_hint = {1}", addr, size_hint);
347 ++it->second.ref_count;
352 return Status(expected_bkpt.takeError());
360 LLDB_LOG(log,
"addr = {0:x}", addr);
363 return Status(
"Breakpoint not found.");
364 assert(it->second.ref_count > 0);
365 if (--it->second.ref_count > 0)
372 llvm::SmallVector<uint8_t, 4> curr_break_op(
373 it->second.breakpoint_opcodes.size(), 0);
376 size_t bytes_read = 0;
378 ReadMemory(addr, curr_break_op.data(), curr_break_op.size(), bytes_read);
379 if (
error.Fail() || bytes_read < curr_break_op.size()) {
380 return Status(
"addr=0x%" PRIx64
381 ": tried to read %zu bytes but only read %zu",
382 addr, curr_break_op.size(), bytes_read);
384 const auto &saved = it->second.saved_opcodes;
386 if (llvm::ArrayRef(curr_break_op) != it->second.breakpoint_opcodes) {
387 if (curr_break_op != it->second.saved_opcodes)
388 return Status(
"Original breakpoint trap is no longer in memory.");
390 "Saved opcodes ({0:@[x]}) have already been restored at {1:x}.",
391 llvm::make_range(saved.begin(), saved.end()), addr);
395 size_t bytes_written = 0;
397 if (
error.Fail() || bytes_written < saved.size()) {
398 return Status(
"addr=0x%" PRIx64
399 ": tried to write %zu bytes but only wrote %zu",
400 addr, saved.size(), bytes_written);
404 llvm::SmallVector<uint8_t, 4> verify_opcode(saved.size(), 0);
405 size_t verify_bytes_read = 0;
408 if (
error.Fail() || verify_bytes_read < verify_opcode.size()) {
409 return Status(
"addr=0x%" PRIx64
410 ": tried to read %zu verification bytes but only read %zu",
411 addr, verify_opcode.size(), verify_bytes_read);
413 if (verify_opcode != saved)
414 LLDB_LOG(log,
"Restoring bytes at {0:x}: {1:@[x]}", addr,
415 llvm::make_range(saved.begin(), saved.end()));
422llvm::Expected<NativeProcessProtocol::SoftwareBreakpoint>
424 uint32_t size_hint) {
429 return expected_trap.takeError();
431 llvm::SmallVector<uint8_t, 4> saved_opcode_bytes(expected_trap->size(), 0);
433 size_t bytes_read = 0;
435 saved_opcode_bytes.size(), bytes_read);
437 return error.ToError();
440 if (bytes_read != saved_opcode_bytes.size()) {
441 return llvm::createStringError(
442 llvm::inconvertibleErrorCode(),
443 "Failed to read memory while attempting to set breakpoint: attempted "
444 "to read {0} bytes but only read {1}.",
445 saved_opcode_bytes.size(), bytes_read);
449 log,
"Overwriting bytes at {0:x}: {1:@[x]}", addr,
450 llvm::make_range(saved_opcode_bytes.begin(), saved_opcode_bytes.end()));
453 size_t bytes_written = 0;
457 return error.ToError();
460 if (bytes_written != expected_trap->size()) {
461 return llvm::createStringError(
462 llvm::inconvertibleErrorCode(),
463 "Failed write memory while attempting to set "
464 "breakpoint: attempted to write {0} bytes but only wrote {1}",
465 expected_trap->size(), bytes_written);
468 llvm::SmallVector<uint8_t, 4> verify_bp_opcode_bytes(expected_trap->size(),
470 size_t verify_bytes_read = 0;
472 verify_bp_opcode_bytes.size(), verify_bytes_read);
474 return error.ToError();
477 if (verify_bytes_read != verify_bp_opcode_bytes.size()) {
478 return llvm::createStringError(
479 llvm::inconvertibleErrorCode(),
480 "Failed to read memory while "
481 "attempting to verify breakpoint: attempted to read {0} bytes "
483 verify_bp_opcode_bytes.size(), verify_bytes_read);
486 if (llvm::ArrayRef(verify_bp_opcode_bytes.data(), verify_bytes_read) !=
488 return llvm::createStringError(
489 llvm::inconvertibleErrorCode(),
490 "Verification of software breakpoint "
491 "writing failed - trap opcodes not successfully read back "
492 "after writing when setting breakpoint at {0:x}",
496 LLDB_LOG(log,
"addr = {0:x}: SUCCESS", addr);
497 return SoftwareBreakpoint{1, saved_opcode_bytes, *expected_trap};
500llvm::Expected<llvm::ArrayRef<uint8_t>>
502 static const uint8_t g_aarch64_opcode[] = {0x00, 0x00, 0x20, 0xd4};
503 static const uint8_t g_i386_opcode[] = {0xCC};
504 static const uint8_t g_mips64_opcode[] = {0x00, 0x00, 0x00, 0x0d};
505 static const uint8_t g_mips64el_opcode[] = {0x0d, 0x00, 0x00, 0x00};
506 static const uint8_t g_msp430_opcode[] = {0x43, 0x43};
507 static const uint8_t g_s390x_opcode[] = {0x00, 0x01};
508 static const uint8_t g_ppc_opcode[] = {0x7f, 0xe0, 0x00, 0x08};
509 static const uint8_t g_ppcle_opcode[] = {0x08, 0x00, 0xe0, 0x7f};
510 static const uint8_t g_riscv_opcode[] = {0x73, 0x00, 0x10, 0x00};
511 static const uint8_t g_riscv_opcode_c[] = {0x02, 0x90};
512 static const uint8_t g_loongarch_opcode[] = {0x05, 0x00, 0x2a,
516 case llvm::Triple::aarch64:
517 case llvm::Triple::aarch64_32:
518 return llvm::ArrayRef(g_aarch64_opcode);
520 case llvm::Triple::x86:
521 case llvm::Triple::x86_64:
522 return llvm::ArrayRef(g_i386_opcode);
524 case llvm::Triple::mips:
525 case llvm::Triple::mips64:
526 return llvm::ArrayRef(g_mips64_opcode);
528 case llvm::Triple::mipsel:
529 case llvm::Triple::mips64el:
530 return llvm::ArrayRef(g_mips64el_opcode);
532 case llvm::Triple::msp430:
533 return llvm::ArrayRef(g_msp430_opcode);
535 case llvm::Triple::systemz:
536 return llvm::ArrayRef(g_s390x_opcode);
538 case llvm::Triple::ppc:
539 case llvm::Triple::ppc64:
540 return llvm::ArrayRef(g_ppc_opcode);
542 case llvm::Triple::ppc64le:
543 return llvm::ArrayRef(g_ppcle_opcode);
545 case llvm::Triple::riscv32:
546 case llvm::Triple::riscv64: {
547 return size_hint == 2 ? llvm::ArrayRef(g_riscv_opcode_c)
548 : llvm::ArrayRef(g_riscv_opcode);
551 case llvm::Triple::loongarch32:
552 case llvm::Triple::loongarch64:
553 return llvm::ArrayRef(g_loongarch_opcode);
556 return llvm::createStringError(llvm::inconvertibleErrorCode(),
557 "CPU type not supported!");
563 case llvm::Triple::x86:
564 case llvm::Triple::x86_64:
565 case llvm::Triple::systemz:
569 case llvm::Triple::arm:
570 case llvm::Triple::aarch64:
571 case llvm::Triple::aarch64_32:
572 case llvm::Triple::mips64:
573 case llvm::Triple::mips64el:
574 case llvm::Triple::mips:
575 case llvm::Triple::mipsel:
576 case llvm::Triple::ppc:
577 case llvm::Triple::ppc64:
578 case llvm::Triple::ppc64le:
579 case llvm::Triple::riscv32:
580 case llvm::Triple::riscv64:
581 case llvm::Triple::loongarch32:
582 case llvm::Triple::loongarch64:
587 llvm_unreachable(
"CPU type not supported!");
602 LLDB_LOG(log,
"breakpoint size: {0}", breakpoint_size);
603 if (breakpoint_size == 0)
611 if (breakpoint_addr >= breakpoint_size)
612 breakpoint_addr -= breakpoint_size;
617 "pid {0} no lldb software breakpoint found at current pc with "
619 GetID(), breakpoint_addr);
628 LLDB_LOG(log,
"pid {0} tid {1}: changing PC from {2:x} to {3:x}",
GetID(),
629 thread.
GetID(), initial_pc_addr, breakpoint_addr);
636 LLDB_LOG(log,
"pid {0} tid {1}: failed to set PC: {2}",
GetID(),
650 void *buf,
size_t size,
651 size_t &bytes_read) {
656 llvm::MutableArrayRef data(
static_cast<uint8_t *
>(buf), bytes_read);
659 auto saved_opcodes = llvm::ArrayRef(pair.second.saved_opcodes);
661 if (bp_addr + saved_opcodes.size() < addr || addr + bytes_read <= bp_addr)
664 if (bp_addr < addr) {
665 saved_opcodes = saved_opcodes.drop_front(addr - bp_addr);
668 auto bp_data = data.drop_front(bp_addr - addr);
669 std::copy_n(saved_opcodes.begin(),
670 std::min(saved_opcodes.size(), bp_data.size()),
676llvm::Expected<llvm::StringRef>
679 size_t &total_bytes_read) {
680 static const size_t cache_line_size =
681 llvm::sys::Process::getPageSizeEstimate();
682 size_t bytes_read = 0;
683 size_t bytes_left = max_size;
686 char *curr_buffer = buffer;
687 total_bytes_read = 0;
690 while (bytes_left > 0 && status.
Success()) {
691 addr_t cache_line_bytes_left =
692 cache_line_size - (curr_addr % cache_line_size);
693 addr_t bytes_to_read = std::min<addr_t>(bytes_left, cache_line_bytes_left);
694 status =
ReadMemory(curr_addr,
static_cast<void *
>(curr_buffer),
695 bytes_to_read, bytes_read);
700 void *str_end = std::memchr(curr_buffer,
'\0', bytes_read);
701 if (str_end !=
nullptr) {
703 static_cast<size_t>((
static_cast<char *
>(str_end) - buffer + 1));
708 total_bytes_read += bytes_read;
709 curr_buffer += bytes_read;
710 curr_addr += bytes_read;
711 bytes_left -= bytes_read;
714 string_size = total_bytes_read - 1;
717 if (bytes_left == 0 && max_size > 0 && buffer[max_size - 1] !=
'\0') {
718 buffer[max_size - 1] =
'\0';
725 return llvm::StringRef(buffer, string_size);
734 bool notify_delegates) {
753 if (notify_delegates)
static llvm::raw_ostream & error(Stream &strm)
#define LLDB_LOG(log,...)
The LLDB_LOG* macros defined below are the way to emit log messages.
virtual void ProcessStateChanged(NativeProcessProtocol *process, lldb::StateType state)=0
virtual void DidExec(NativeProcessProtocol *process)=0
virtual void InitializeDelegate(NativeProcessProtocol *process)=0
virtual Status SetWatchpoint(lldb::addr_t addr, size_t size, uint32_t watch_flags, bool hardware)
virtual Status ReadMemoryTags(int32_t type, lldb::addr_t addr, size_t len, std::vector< uint8_t > &tags)
llvm::Expected< SoftwareBreakpoint > EnableSoftwareBreakpoint(lldb::addr_t addr, uint32_t size_hint)
virtual Status GetMemoryRegionInfo(lldb::addr_t load_addr, MemoryRegionInfo &range_info)
virtual void NotifyTracersProcessDidStop()
Notify tracers that the target process just stopped.
virtual std::optional< WaitStatus > GetExitStatus()
virtual Status RemoveWatchpoint(lldb::addr_t addr)
virtual Status Interrupt()
Tells a process to interrupt all operations as if by a Ctrl-C.
virtual Status WriteMemoryTags(int32_t type, lldb::addr_t addr, size_t len, const std::vector< uint8_t > &tags)
virtual void DoStopIDBumped(uint32_t newBumpId)
NativeProcessProtocol(lldb::pid_t pid, int terminal_fd, NativeDelegate &delegate)
virtual size_t GetSoftwareBreakpointPCOffset()
Return the offset of the PC relative to the software breakpoint that was hit.
lldb::pid_t GetID() const
virtual const HardwareBreakpointMap & GetHardwareBreakpointMap() const
Status SetSoftwareBreakpoint(lldb::addr_t addr, uint32_t size_hint)
virtual Status IgnoreSignals(llvm::ArrayRef< int > signals)
NativeThreadProtocol * GetThreadByIDUnlocked(lldb::tid_t tid)
virtual const ArchSpec & GetArchitecture() const =0
virtual const NativeWatchpointList::WatchpointMap & GetWatchpointMap() const
lldb::StateType GetState() const
void SetState(lldb::StateType state, bool notify_delegates=true)
HardwareBreakpointMap m_hw_breakpoints_map
llvm::Expected< llvm::StringRef > ReadCStringFromMemory(lldb::addr_t addr, char *buffer, size_t max_size, size_t &total_bytes_read)
Reads a null terminated string from memory.
NativeThreadProtocol * GetThreadByID(lldb::tid_t tid)
void SynchronouslyNotifyProcessStateChanged(lldb::StateType state)
uint32_t GetStopID() const
virtual Status ReadMemory(lldb::addr_t addr, void *buf, size_t size, size_t &bytes_read)=0
std::recursive_mutex m_threads_mutex
std::vector< std::unique_ptr< NativeThreadProtocol > > m_threads
llvm::DenseSet< int > m_signals_to_ignore
std::optional< WaitStatus > m_exit_status
virtual bool SetExitStatus(WaitStatus status, bool bNotifyStateChange)
virtual Status RemoveBreakpoint(lldb::addr_t addr, bool hardware=false)
virtual Status WriteMemory(lldb::addr_t addr, const void *buf, size_t size, size_t &bytes_written)=0
Status ReadMemoryWithoutTrap(lldb::addr_t addr, void *buf, size_t size, size_t &bytes_read)
virtual Status Signal(int signo)=0
Sends a process a UNIX signal signal.
NativeDelegate & m_delegate
Status RemoveSoftwareBreakpoint(lldb::addr_t addr)
void FixupBreakpointPCAsNeeded(NativeThreadProtocol &thread)
NativeThreadProtocol * GetThreadAtIndex(uint32_t idx)
virtual size_t UpdateThreads()=0
virtual void NotifyDidExec()
Notify the delegate that an exec occurred.
virtual bool IsAlive() const
virtual Status SetHardwareBreakpoint(lldb::addr_t addr, size_t size)
virtual std::optional< std::pair< uint32_t, uint32_t > > GetHardwareDebugSupportInfo() const
virtual llvm::Expected< llvm::ArrayRef< uint8_t > > GetSoftwareBreakpointTrapOpcode(size_t size_hint)
std::recursive_mutex m_state_mutex
virtual Status RemoveHardwareBreakpoint(lldb::addr_t addr)
std::unordered_map< lldb::addr_t, SoftwareBreakpoint > m_software_breakpoints
NativeWatchpointList m_watchpoint_list
virtual uint32_t NumSupportedHardwareWatchpoints()
Status SetPC(lldb::addr_t pc)
virtual lldb::addr_t GetPCfromBreakpointLocation(lldb::addr_t fail_value=LLDB_INVALID_ADDRESS)
virtual uint32_t NumSupportedHardwareBreakpoints()
virtual NativeRegisterContext & GetRegisterContext()=0
lldb::tid_t GetID() const
Status Add(lldb::addr_t addr, size_t size, uint32_t watch_flags, bool hardware)
const WatchpointMap & GetWatchpointMap() const
Status Remove(lldb::addr_t addr)
std::map< lldb::addr_t, NativeWatchpoint > WatchpointMap
void Clear()
Clear the object state.
llvm::Error ToError() const
bool Fail() const
Test for error condition.
bool Success() const
Test for success condition.
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
bool StateIsStoppedState(lldb::StateType state, bool must_exist)
Check if a state represents a state where the process or thread is stopped.
std::map< lldb::addr_t, HardwareBreakpoint > HardwareBreakpointMap
StateType
Process and Thread States.
@ eStateUnloaded
Process is object is valid, but not currently loaded.
@ eStateDetached
Process has been detached and can't be examined.
@ eStateStopped
Process or thread is stopped and can be examined.
@ eStateExited
Process has exited and can't be examined.
@ eStateCrashed
Process or thread has crashed and can be examined.