20#include "llvm/Support/Process.h"
60 size_t len, std::vector<uint8_t> &tags) {
67 const std::vector<uint8_t> &tags) {
79 bool bNotifyStateChange) {
81 LLDB_LOG(log,
"status = {0}, notify = {1}", status, bNotifyStateChange);
88 LLDB_LOG(log,
"state is exited, but status not set");
95 if (bNotifyStateChange)
111 if (thread->GetID() == tid)
132std::optional<std::pair<uint32_t, uint32_t>>
140 LLDB_LOG(log,
"failed to find a thread to grab a NativeRegisterContext!");
150 uint32_t watch_flags,
167 std::vector<NativeThreadProtocol *> watchpoint_established_threads;
176 assert(thread &&
"thread list should not have a NULL thread!");
179 thread->SetWatchpoint(addr, size, watch_flags, hardware);
180 if (thread_error.
Fail() && hardware) {
183 thread_error = thread->SetWatchpoint(addr, size, watch_flags,
false);
186 "hardware watchpoint requested but software watchpoint set");
192 watchpoint_established_threads.push_back(thread.get());
196 for (
auto unwatch_thread_sp : watchpoint_established_threads) {
197 Status remove_error = unwatch_thread_sp->RemoveWatchpoint(addr);
198 if (remove_error.
Fail())
199 LLDB_LOG(log,
"RemoveWatchpoint failed for pid={0}, tid={1}: {2}",
200 GetID(), unwatch_thread_sp->GetID(), remove_error);
217 assert(thread &&
"thread list should not have a NULL thread!");
219 Status thread_error = thread->RemoveWatchpoint(addr);
220 if (thread_error.
Fail()) {
224 if (!overall_error.
Fail())
225 overall_error = std::move(thread_error);
229 return overall_error.
Fail() ? std::move(overall_error) : std::move(
error);
250 if (hw_debug_cap == std::nullopt || hw_debug_cap->first == 0 ||
253 "Target does not have required no of hardware breakpoints");
259 std::vector<NativeThreadProtocol *> breakpoint_established_threads;
264 assert(thread &&
"thread list should not have a NULL thread!");
266 Status thread_error = thread->SetHardwareBreakpoint(addr, size);
270 breakpoint_established_threads.push_back(thread.get());
275 for (
auto rollback_thread_sp : breakpoint_established_threads) {
277 rollback_thread_sp->RemoveHardwareBreakpoint(addr);
278 if (remove_error.
Fail())
280 "RemoveHardwareBreakpoint failed for pid={0}, tid={1}: {2}",
281 GetID(), rollback_thread_sp->GetID(), remove_error);
303 assert(thread &&
"thread list should not have a NULL thread!");
304 error = thread->RemoveHardwareBreakpoint(addr);
329 LLDB_LOG(log,
"sent state notification [{0}] from process {1}", state,
343 uint32_t size_hint) {
345 LLDB_LOG(log,
"addr = {0:x}, size_hint = {1}", addr, size_hint);
360 LLDB_LOG(log,
"addr = {0:x}", addr);
379 size_t bytes_read = 0;
381 ReadMemory(addr, curr_break_op.data(), curr_break_op.size(), bytes_read);
382 if (
error.Fail() || bytes_read < curr_break_op.size()) {
384 "addr=0x%" PRIx64
": tried to read %zu bytes but only read %zu", addr,
385 curr_break_op.size(), bytes_read);
392 "Original breakpoint trap is no longer in memory.");
394 "Saved opcodes ({0:@[x]}) have already been restored at {1:x}.",
395 llvm::make_range(saved.begin(), saved.end()), addr);
399 size_t bytes_written = 0;
401 if (
error.Fail() || bytes_written < saved.size()) {
403 "addr=0x%" PRIx64
": tried to write %zu bytes but only wrote %zu",
404 addr, saved.size(), bytes_written);
408 llvm::SmallVector<uint8_t, 4> verify_opcode(saved.size(), 0);
409 size_t verify_bytes_read = 0;
412 if (
error.Fail() || verify_bytes_read < verify_opcode.size()) {
415 ": tried to read %zu verification bytes but only read %zu",
416 addr, verify_opcode.size(), verify_bytes_read);
418 if (verify_opcode != saved)
419 LLDB_LOG(log,
"Restoring bytes at {0:x}: {1:@[x]}", addr,
420 llvm::make_range(saved.begin(), saved.end()));
426llvm::Expected<NativeProcessProtocol::SoftwareBreakpoint>
428 uint32_t size_hint) {
433 return expected_trap.takeError();
435 llvm::SmallVector<uint8_t, 4> saved_opcode_bytes(expected_trap->size(), 0);
437 size_t bytes_read = 0;
439 saved_opcode_bytes.size(), bytes_read);
441 return error.ToError();
444 if (bytes_read != saved_opcode_bytes.size()) {
445 return llvm::createStringError(
446 llvm::inconvertibleErrorCode(),
447 "Failed to read memory while attempting to set breakpoint: attempted "
448 "to read {0} bytes but only read {1}.",
449 saved_opcode_bytes.size(), bytes_read);
453 log,
"Overwriting bytes at {0:x}: {1:@[x]}", addr,
454 llvm::make_range(saved_opcode_bytes.begin(), saved_opcode_bytes.end()));
457 size_t bytes_written = 0;
461 return error.ToError();
464 if (bytes_written != expected_trap->size()) {
465 return llvm::createStringError(
466 llvm::inconvertibleErrorCode(),
467 "Failed write memory while attempting to set "
468 "breakpoint: attempted to write {0} bytes but only wrote {1}",
469 expected_trap->size(), bytes_written);
472 llvm::SmallVector<uint8_t, 4> verify_bp_opcode_bytes(expected_trap->size(),
474 size_t verify_bytes_read = 0;
476 verify_bp_opcode_bytes.size(), verify_bytes_read);
478 return error.ToError();
481 if (verify_bytes_read != verify_bp_opcode_bytes.size()) {
482 return llvm::createStringError(
483 llvm::inconvertibleErrorCode(),
484 "Failed to read memory while "
485 "attempting to verify breakpoint: attempted to read {0} bytes "
487 verify_bp_opcode_bytes.size(), verify_bytes_read);
490 if (llvm::ArrayRef(verify_bp_opcode_bytes.data(), verify_bytes_read) !=
492 return llvm::createStringError(
493 llvm::inconvertibleErrorCode(),
494 "Verification of software breakpoint "
495 "writing failed - trap opcodes not successfully read back "
496 "after writing when setting breakpoint at {0:x}",
504llvm::Expected<llvm::ArrayRef<uint8_t>>
506 static const uint8_t g_aarch64_opcode[] = {0x00, 0x00, 0x20, 0xd4};
507 static const uint8_t g_i386_opcode[] = {0xCC};
508 static const uint8_t g_mips64_opcode[] = {0x00, 0x00, 0x00, 0x0d};
509 static const uint8_t g_mips64el_opcode[] = {0x0d, 0x00, 0x00, 0x00};
510 static const uint8_t g_msp430_opcode[] = {0x43, 0x43};
511 static const uint8_t g_s390x_opcode[] = {0x00, 0x01};
512 static const uint8_t g_ppc_opcode[] = {0x7f, 0xe0, 0x00, 0x08};
513 static const uint8_t g_ppcle_opcode[] = {0x08, 0x00, 0xe0, 0x7f};
514 static const uint8_t g_riscv_opcode[] = {0x73, 0x00, 0x10, 0x00};
515 static const uint8_t g_riscv_opcode_c[] = {0x02, 0x90};
516 static const uint8_t g_loongarch_opcode[] = {0x05, 0x00, 0x2a,
520 case llvm::Triple::aarch64:
521 case llvm::Triple::aarch64_32:
522 return llvm::ArrayRef(g_aarch64_opcode);
524 case llvm::Triple::x86:
525 case llvm::Triple::x86_64:
526 return llvm::ArrayRef(g_i386_opcode);
528 case llvm::Triple::mips:
529 case llvm::Triple::mips64:
530 return llvm::ArrayRef(g_mips64_opcode);
532 case llvm::Triple::mipsel:
533 case llvm::Triple::mips64el:
534 return llvm::ArrayRef(g_mips64el_opcode);
536 case llvm::Triple::msp430:
537 return llvm::ArrayRef(g_msp430_opcode);
539 case llvm::Triple::systemz:
540 return llvm::ArrayRef(g_s390x_opcode);
542 case llvm::Triple::ppc:
543 case llvm::Triple::ppc64:
544 return llvm::ArrayRef(g_ppc_opcode);
546 case llvm::Triple::ppc64le:
547 return llvm::ArrayRef(g_ppcle_opcode);
549 case llvm::Triple::riscv32:
550 case llvm::Triple::riscv64: {
551 return size_hint == 2 ? llvm::ArrayRef(g_riscv_opcode_c)
552 : llvm::ArrayRef(g_riscv_opcode);
555 case llvm::Triple::loongarch32:
556 case llvm::Triple::loongarch64:
557 return llvm::ArrayRef(g_loongarch_opcode);
560 return llvm::createStringError(llvm::inconvertibleErrorCode(),
561 "CPU type not supported!");
567 case llvm::Triple::x86:
568 case llvm::Triple::x86_64:
569 case llvm::Triple::systemz:
573 case llvm::Triple::arm:
574 case llvm::Triple::aarch64:
575 case llvm::Triple::aarch64_32:
576 case llvm::Triple::mips64:
577 case llvm::Triple::mips64el:
578 case llvm::Triple::mips:
579 case llvm::Triple::mipsel:
580 case llvm::Triple::ppc:
581 case llvm::Triple::ppc64:
582 case llvm::Triple::ppc64le:
583 case llvm::Triple::riscv32:
584 case llvm::Triple::riscv64:
585 case llvm::Triple::loongarch32:
586 case llvm::Triple::loongarch64:
591 llvm_unreachable(
"CPU type not supported!");
606 LLDB_LOG(log,
"breakpoint size: {0}", breakpoint_size);
607 if (breakpoint_size == 0)
615 if (breakpoint_addr >= breakpoint_size)
616 breakpoint_addr -= breakpoint_size;
621 "pid {0} no lldb software breakpoint found at current pc with "
623 GetID(), breakpoint_addr);
632 LLDB_LOG(log,
"pid {0} tid {1}: changing PC from {2:x} to {3:x}",
GetID(),
633 thread.GetID(), initial_pc_addr, breakpoint_addr);
640 LLDB_LOG(log,
"pid {0} tid {1}: failed to set PC: {2}",
GetID(),
641 thread.GetID(),
error);
654 size_t size,
size_t &bytes_written) {
665 std::map<lldb::addr_t, SoftwareBreakpoint>::iterator bkpt =
671 bkpt = std::prev(bkpt);
673 const uint8_t *byte_buf =
static_cast<const uint8_t *
>(buf);
675 auto &[sbp_addr, sbp_data] = *bkpt;
678 if (addr < sbp_addr) {
679 const size_t to_write =
680 std::min(
static_cast<addr_t>(size), sbp_addr - addr);
681 size_t part_bytes_written = 0;
683 bytes_written += part_bytes_written;
685 if (
error.Fail() || part_bytes_written < to_write) {
687 "addr=0x%" PRIx64
": tried to write %zu bytes but only wrote %zu",
688 addr, to_write, part_bytes_written);
691 byte_buf += to_write;
701 if ((addr >= sbp_addr) &&
702 (addr < (sbp_addr + sbp_data.saved_opcodes.size()))) {
705 const size_t idx = addr - sbp_addr;
706 const size_t to_write =
707 std::min(size, sbp_data.saved_opcodes.size() - idx);
708 for (
size_t copied = 0; copied < to_write;
709 ++bytes_written, ++byte_buf, ++addr, --size, ++copied)
710 sbp_data.saved_opcodes[idx + copied] = *byte_buf;
722 size_t part_bytes_written = 0;
724 bytes_written += part_bytes_written;
725 if (
error.Fail() || part_bytes_written < size) {
727 "addr=0x%" PRIx64
": tried to write %zu bytes but only wrote %zu",
728 addr, size, part_bytes_written);
737 void *buf,
size_t size,
738 size_t &bytes_read) {
744 llvm::MutableArrayRef data(
static_cast<uint8_t *
>(buf), bytes_read);
747 auto saved_opcodes = llvm::ArrayRef(pair.second.saved_opcodes);
749 if (bp_addr + saved_opcodes.size() < addr || addr + bytes_read <= bp_addr)
752 if (bp_addr < addr) {
753 saved_opcodes = saved_opcodes.drop_front(addr - bp_addr);
756 auto bp_data = data.drop_front(bp_addr - addr);
757 std::copy_n(saved_opcodes.begin(),
758 std::min(saved_opcodes.size(), bp_data.size()),
764llvm::Expected<llvm::StringRef>
767 size_t &total_bytes_read) {
768 static const size_t cache_line_size =
769 llvm::sys::Process::getPageSizeEstimate();
770 size_t bytes_read = 0;
771 size_t bytes_left = max_size;
774 char *curr_buffer = buffer;
775 total_bytes_read = 0;
778 while (bytes_left > 0 && status.
Success()) {
779 addr_t cache_line_bytes_left =
780 cache_line_size - (curr_addr % cache_line_size);
781 addr_t bytes_to_read = std::min<addr_t>(bytes_left, cache_line_bytes_left);
782 status =
ReadMemory(curr_addr,
static_cast<void *
>(curr_buffer),
783 bytes_to_read, bytes_read);
788 void *str_end = std::memchr(curr_buffer,
'\0', bytes_read);
789 if (str_end !=
nullptr) {
791 static_cast<size_t>((
static_cast<char *
>(str_end) - buffer + 1));
796 total_bytes_read += bytes_read;
797 curr_buffer += bytes_read;
798 curr_addr += bytes_read;
799 bytes_left -= bytes_read;
802 string_size = total_bytes_read - 1;
805 if (bytes_left == 0 && max_size > 0 && buffer[max_size - 1] !=
'\0') {
806 buffer[max_size - 1] =
'\0';
813 return llvm::StringRef(buffer, string_size);
822 bool notify_delegates) {
841 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 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.
virtual Status DoWriteMemory(lldb::addr_t addr, const void *buf, size_t size, size_t &bytes_written)=0
NativeThreadProtocol * GetThreadByID(lldb::tid_t tid)
Status ReadMemoryWithoutTrap(const ProcessAddress &addr, void *buf, size_t size, size_t &bytes_read)
void SynchronouslyNotifyProcessStateChanged(lldb::StateType state)
uint32_t GetStopID() const
Status WriteMemory(lldb::addr_t addr, const void *buf, size_t size, size_t &bytes_written)
Write memory while not overwriting breakpoints in memory.
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 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 Status ReadMemory(const ProcessAddress &addr, void *buf, size_t size, size_t &bytes_read)=0
virtual bool IsAlive() const
virtual Status SetHardwareBreakpoint(lldb::addr_t addr, size_t size)
std::map< lldb::addr_t, SoftwareBreakpoint > m_software_breakpoints
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)
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()
std::map< lldb::addr_t, NativeWatchpoint > WatchpointMap
An address in a process, qualified by an address space.
lldb::addr_t GetValue() const
void Clear()
Clear the object state.
llvm::Error ToError() const
FIXME: Replace all uses with takeError() instead.
static Status FromErrorStringWithFormat(const char *format,...) __attribute__((format(printf
static Status FromErrorString(const char *str)
bool Fail() const
Test for error condition.
static Status FromError(llvm::Error error)
Avoid using this in new code. Migrate APIs to llvm::Expected instead.
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.
llvm::ArrayRef< uint8_t > breakpoint_opcodes
llvm::SmallVector< uint8_t, 4 > saved_opcodes