20#include "llvm/Support/Process.h"
59 size_t len, std::vector<uint8_t> &tags) {
66 const std::vector<uint8_t> &tags) {
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 Status thread_error = thread->RemoveWatchpoint(addr);
219 if (thread_error.
Fail()) {
223 if (!overall_error.
Fail())
224 overall_error = std::move(thread_error);
228 return overall_error.
Fail() ? std::move(overall_error) : std::move(
error);
249 if (hw_debug_cap == std::nullopt || hw_debug_cap->first == 0 ||
252 "Target does not have required no of hardware breakpoints");
258 std::vector<NativeThreadProtocol *> breakpoint_established_threads;
263 assert(thread &&
"thread list should not have a NULL thread!");
265 Status thread_error = thread->SetHardwareBreakpoint(addr, size);
269 breakpoint_established_threads.push_back(thread.get());
274 for (
auto rollback_thread_sp : breakpoint_established_threads) {
276 rollback_thread_sp->RemoveHardwareBreakpoint(addr);
277 if (remove_error.
Fail())
279 "RemoveHardwareBreakpoint failed for pid={0}, tid={1}: {2}",
280 GetID(), rollback_thread_sp->GetID(), remove_error);
302 assert(thread &&
"thread list should not have a NULL thread!");
303 error = thread->RemoveHardwareBreakpoint(addr);
328 LLDB_LOG(log,
"sent state notification [{0}] from process {1}", state,
342 uint32_t size_hint) {
344 LLDB_LOG(log,
"addr = {0:x}, size_hint = {1}", addr, size_hint);
348 ++it->second.ref_count;
361 LLDB_LOG(log,
"addr = {0:x}", addr);
365 assert(it->second.ref_count > 0);
366 if (--it->second.ref_count > 0)
384 size_t bytes_read = 0;
386 ReadMemory(addr, curr_break_op.data(), curr_break_op.size(), bytes_read);
387 if (
error.Fail() || bytes_read < curr_break_op.size()) {
389 "addr=0x%" PRIx64
": tried to read %zu bytes but only read %zu", addr,
390 curr_break_op.size(), bytes_read);
397 "Original breakpoint trap is no longer in memory.");
399 "Saved opcodes ({0:@[x]}) have already been restored at {1:x}.",
400 llvm::make_range(saved.begin(), saved.end()), addr);
404 size_t bytes_written = 0;
406 if (
error.Fail() || bytes_written < saved.size()) {
408 "addr=0x%" PRIx64
": tried to write %zu bytes but only wrote %zu",
409 addr, saved.size(), bytes_written);
413 llvm::SmallVector<uint8_t, 4> verify_opcode(saved.size(), 0);
414 size_t verify_bytes_read = 0;
417 if (
error.Fail() || verify_bytes_read < verify_opcode.size()) {
420 ": tried to read %zu verification bytes but only read %zu",
421 addr, verify_opcode.size(), verify_bytes_read);
423 if (verify_opcode != saved)
424 LLDB_LOG(log,
"Restoring bytes at {0:x}: {1:@[x]}", addr,
425 llvm::make_range(saved.begin(), saved.end()));
431llvm::Expected<NativeProcessProtocol::SoftwareBreakpoint>
433 uint32_t size_hint) {
438 return expected_trap.takeError();
440 llvm::SmallVector<uint8_t, 4> saved_opcode_bytes(expected_trap->size(), 0);
442 size_t bytes_read = 0;
444 saved_opcode_bytes.size(), bytes_read);
446 return error.ToError();
449 if (bytes_read != saved_opcode_bytes.size()) {
450 return llvm::createStringError(
451 llvm::inconvertibleErrorCode(),
452 "Failed to read memory while attempting to set breakpoint: attempted "
453 "to read {0} bytes but only read {1}.",
454 saved_opcode_bytes.size(), bytes_read);
458 log,
"Overwriting bytes at {0:x}: {1:@[x]}", addr,
459 llvm::make_range(saved_opcode_bytes.begin(), saved_opcode_bytes.end()));
462 size_t bytes_written = 0;
466 return error.ToError();
469 if (bytes_written != expected_trap->size()) {
470 return llvm::createStringError(
471 llvm::inconvertibleErrorCode(),
472 "Failed write memory while attempting to set "
473 "breakpoint: attempted to write {0} bytes but only wrote {1}",
474 expected_trap->size(), bytes_written);
477 llvm::SmallVector<uint8_t, 4> verify_bp_opcode_bytes(expected_trap->size(),
479 size_t verify_bytes_read = 0;
481 verify_bp_opcode_bytes.size(), verify_bytes_read);
483 return error.ToError();
486 if (verify_bytes_read != verify_bp_opcode_bytes.size()) {
487 return llvm::createStringError(
488 llvm::inconvertibleErrorCode(),
489 "Failed to read memory while "
490 "attempting to verify breakpoint: attempted to read {0} bytes "
492 verify_bp_opcode_bytes.size(), verify_bytes_read);
495 if (llvm::ArrayRef(verify_bp_opcode_bytes.data(), verify_bytes_read) !=
497 return llvm::createStringError(
498 llvm::inconvertibleErrorCode(),
499 "Verification of software breakpoint "
500 "writing failed - trap opcodes not successfully read back "
501 "after writing when setting breakpoint at {0:x}",
505 LLDB_LOG(log,
"addr = {0:x}: SUCCESS", addr);
509llvm::Expected<llvm::ArrayRef<uint8_t>>
511 static const uint8_t g_aarch64_opcode[] = {0x00, 0x00, 0x20, 0xd4};
512 static const uint8_t g_i386_opcode[] = {0xCC};
513 static const uint8_t g_mips64_opcode[] = {0x00, 0x00, 0x00, 0x0d};
514 static const uint8_t g_mips64el_opcode[] = {0x0d, 0x00, 0x00, 0x00};
515 static const uint8_t g_msp430_opcode[] = {0x43, 0x43};
516 static const uint8_t g_s390x_opcode[] = {0x00, 0x01};
517 static const uint8_t g_ppc_opcode[] = {0x7f, 0xe0, 0x00, 0x08};
518 static const uint8_t g_ppcle_opcode[] = {0x08, 0x00, 0xe0, 0x7f};
519 static const uint8_t g_riscv_opcode[] = {0x73, 0x00, 0x10, 0x00};
520 static const uint8_t g_riscv_opcode_c[] = {0x02, 0x90};
521 static const uint8_t g_loongarch_opcode[] = {0x05, 0x00, 0x2a,
525 case llvm::Triple::aarch64:
526 case llvm::Triple::aarch64_32:
527 return llvm::ArrayRef(g_aarch64_opcode);
529 case llvm::Triple::x86:
530 case llvm::Triple::x86_64:
531 return llvm::ArrayRef(g_i386_opcode);
533 case llvm::Triple::mips:
534 case llvm::Triple::mips64:
535 return llvm::ArrayRef(g_mips64_opcode);
537 case llvm::Triple::mipsel:
538 case llvm::Triple::mips64el:
539 return llvm::ArrayRef(g_mips64el_opcode);
541 case llvm::Triple::msp430:
542 return llvm::ArrayRef(g_msp430_opcode);
544 case llvm::Triple::systemz:
545 return llvm::ArrayRef(g_s390x_opcode);
547 case llvm::Triple::ppc:
548 case llvm::Triple::ppc64:
549 return llvm::ArrayRef(g_ppc_opcode);
551 case llvm::Triple::ppc64le:
552 return llvm::ArrayRef(g_ppcle_opcode);
554 case llvm::Triple::riscv32:
555 case llvm::Triple::riscv64: {
556 return size_hint == 2 ? llvm::ArrayRef(g_riscv_opcode_c)
557 : llvm::ArrayRef(g_riscv_opcode);
560 case llvm::Triple::loongarch32:
561 case llvm::Triple::loongarch64:
562 return llvm::ArrayRef(g_loongarch_opcode);
565 return llvm::createStringError(llvm::inconvertibleErrorCode(),
566 "CPU type not supported!");
572 case llvm::Triple::x86:
573 case llvm::Triple::x86_64:
574 case llvm::Triple::systemz:
578 case llvm::Triple::arm:
579 case llvm::Triple::aarch64:
580 case llvm::Triple::aarch64_32:
581 case llvm::Triple::mips64:
582 case llvm::Triple::mips64el:
583 case llvm::Triple::mips:
584 case llvm::Triple::mipsel:
585 case llvm::Triple::ppc:
586 case llvm::Triple::ppc64:
587 case llvm::Triple::ppc64le:
588 case llvm::Triple::riscv32:
589 case llvm::Triple::riscv64:
590 case llvm::Triple::loongarch32:
591 case llvm::Triple::loongarch64:
596 llvm_unreachable(
"CPU type not supported!");
611 LLDB_LOG(log,
"breakpoint size: {0}", breakpoint_size);
612 if (breakpoint_size == 0)
620 if (breakpoint_addr >= breakpoint_size)
621 breakpoint_addr -= breakpoint_size;
626 "pid {0} no lldb software breakpoint found at current pc with "
628 GetID(), breakpoint_addr);
637 LLDB_LOG(log,
"pid {0} tid {1}: changing PC from {2:x} to {3:x}",
GetID(),
638 thread.GetID(), initial_pc_addr, breakpoint_addr);
645 LLDB_LOG(log,
"pid {0} tid {1}: failed to set PC: {2}",
GetID(),
646 thread.GetID(),
error);
659 void *buf,
size_t size,
660 size_t &bytes_read) {
665 llvm::MutableArrayRef data(
static_cast<uint8_t *
>(buf), bytes_read);
668 auto saved_opcodes = llvm::ArrayRef(pair.second.saved_opcodes);
670 if (bp_addr + saved_opcodes.size() < addr || addr + bytes_read <= bp_addr)
673 if (bp_addr < addr) {
674 saved_opcodes = saved_opcodes.drop_front(addr - bp_addr);
677 auto bp_data = data.drop_front(bp_addr - addr);
678 std::copy_n(saved_opcodes.begin(),
679 std::min(saved_opcodes.size(), bp_data.size()),
685llvm::Expected<llvm::StringRef>
688 size_t &total_bytes_read) {
689 static const size_t cache_line_size =
690 llvm::sys::Process::getPageSizeEstimate();
691 size_t bytes_read = 0;
692 size_t bytes_left = max_size;
695 char *curr_buffer = buffer;
696 total_bytes_read = 0;
699 while (bytes_left > 0 && status.
Success()) {
700 addr_t cache_line_bytes_left =
701 cache_line_size - (curr_addr % cache_line_size);
702 addr_t bytes_to_read = std::min<addr_t>(bytes_left, cache_line_bytes_left);
703 status =
ReadMemory(curr_addr,
static_cast<void *
>(curr_buffer),
704 bytes_to_read, bytes_read);
709 void *str_end = std::memchr(curr_buffer,
'\0', bytes_read);
710 if (str_end !=
nullptr) {
712 static_cast<size_t>((
static_cast<char *
>(str_end) - buffer + 1));
717 total_bytes_read += bytes_read;
718 curr_buffer += bytes_read;
719 curr_addr += bytes_read;
720 bytes_left -= bytes_read;
723 string_size = total_bytes_read - 1;
726 if (bytes_left == 0 && max_size > 0 && buffer[max_size - 1] !=
'\0') {
727 buffer[max_size - 1] =
'\0';
734 return llvm::StringRef(buffer, string_size);
743 bool notify_delegates) {
762 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.
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()
std::map< lldb::addr_t, NativeWatchpoint > WatchpointMap
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