20#include "llvm/Support/Process.h"
30 : m_pid(pid), m_delegate(delegate), m_terminal_fd(terminal_fd) {
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)
373 llvm::SmallVector<uint8_t, 4> curr_break_op(
374 it->second.breakpoint_opcodes.size(), 0);
377 size_t bytes_read = 0;
379 ReadMemory(addr, curr_break_op.data(), curr_break_op.size(), bytes_read);
380 if (
error.Fail() || bytes_read < curr_break_op.size()) {
382 "addr=0x%" PRIx64
": tried to read %zu bytes but only read %zu", addr,
383 curr_break_op.size(), bytes_read);
385 const auto &saved = it->second.saved_opcodes;
387 if (llvm::ArrayRef(curr_break_op) != it->second.breakpoint_opcodes) {
388 if (curr_break_op != it->second.saved_opcodes)
390 "Original breakpoint trap is no longer in memory.");
392 "Saved opcodes ({0:@[x]}) have already been restored at {1:x}.",
393 llvm::make_range(saved.begin(), saved.end()), addr);
397 size_t bytes_written = 0;
399 if (
error.Fail() || bytes_written < saved.size()) {
401 "addr=0x%" PRIx64
": tried to write %zu bytes but only wrote %zu",
402 addr, saved.size(), bytes_written);
406 llvm::SmallVector<uint8_t, 4> verify_opcode(saved.size(), 0);
407 size_t verify_bytes_read = 0;
410 if (
error.Fail() || verify_bytes_read < verify_opcode.size()) {
413 ": tried to read %zu verification bytes but only read %zu",
414 addr, verify_opcode.size(), verify_bytes_read);
416 if (verify_opcode != saved)
417 LLDB_LOG(log,
"Restoring bytes at {0:x}: {1:@[x]}", addr,
418 llvm::make_range(saved.begin(), saved.end()));
425llvm::Expected<NativeProcessProtocol::SoftwareBreakpoint>
427 uint32_t size_hint) {
432 return expected_trap.takeError();
434 llvm::SmallVector<uint8_t, 4> saved_opcode_bytes(expected_trap->size(), 0);
436 size_t bytes_read = 0;
438 saved_opcode_bytes.size(), bytes_read);
440 return error.ToError();
443 if (bytes_read != saved_opcode_bytes.size()) {
444 return llvm::createStringError(
445 llvm::inconvertibleErrorCode(),
446 "Failed to read memory while attempting to set breakpoint: attempted "
447 "to read {0} bytes but only read {1}.",
448 saved_opcode_bytes.size(), bytes_read);
452 log,
"Overwriting bytes at {0:x}: {1:@[x]}", addr,
453 llvm::make_range(saved_opcode_bytes.begin(), saved_opcode_bytes.end()));
456 size_t bytes_written = 0;
460 return error.ToError();
463 if (bytes_written != expected_trap->size()) {
464 return llvm::createStringError(
465 llvm::inconvertibleErrorCode(),
466 "Failed write memory while attempting to set "
467 "breakpoint: attempted to write {0} bytes but only wrote {1}",
468 expected_trap->size(), bytes_written);
471 llvm::SmallVector<uint8_t, 4> verify_bp_opcode_bytes(expected_trap->size(),
473 size_t verify_bytes_read = 0;
475 verify_bp_opcode_bytes.size(), verify_bytes_read);
477 return error.ToError();
480 if (verify_bytes_read != verify_bp_opcode_bytes.size()) {
481 return llvm::createStringError(
482 llvm::inconvertibleErrorCode(),
483 "Failed to read memory while "
484 "attempting to verify breakpoint: attempted to read {0} bytes "
486 verify_bp_opcode_bytes.size(), verify_bytes_read);
489 if (llvm::ArrayRef(verify_bp_opcode_bytes.data(), verify_bytes_read) !=
491 return llvm::createStringError(
492 llvm::inconvertibleErrorCode(),
493 "Verification of software breakpoint "
494 "writing failed - trap opcodes not successfully read back "
495 "after writing when setting breakpoint at {0:x}",
499 LLDB_LOG(log,
"addr = {0:x}: SUCCESS", addr);
500 return SoftwareBreakpoint{1, saved_opcode_bytes, *expected_trap};
503llvm::Expected<llvm::ArrayRef<uint8_t>>
505 static const uint8_t g_aarch64_opcode[] = {0x00, 0x00, 0x20, 0xd4};
506 static const uint8_t g_i386_opcode[] = {0xCC};
507 static const uint8_t g_mips64_opcode[] = {0x00, 0x00, 0x00, 0x0d};
508 static const uint8_t g_mips64el_opcode[] = {0x0d, 0x00, 0x00, 0x00};
509 static const uint8_t g_msp430_opcode[] = {0x43, 0x43};
510 static const uint8_t g_s390x_opcode[] = {0x00, 0x01};
511 static const uint8_t g_ppc_opcode[] = {0x7f, 0xe0, 0x00, 0x08};
512 static const uint8_t g_ppcle_opcode[] = {0x08, 0x00, 0xe0, 0x7f};
513 static const uint8_t g_riscv_opcode[] = {0x73, 0x00, 0x10, 0x00};
514 static const uint8_t g_riscv_opcode_c[] = {0x02, 0x90};
515 static const uint8_t g_loongarch_opcode[] = {0x05, 0x00, 0x2a,
519 case llvm::Triple::aarch64:
520 case llvm::Triple::aarch64_32:
521 return llvm::ArrayRef(g_aarch64_opcode);
523 case llvm::Triple::x86:
524 case llvm::Triple::x86_64:
525 return llvm::ArrayRef(g_i386_opcode);
527 case llvm::Triple::mips:
528 case llvm::Triple::mips64:
529 return llvm::ArrayRef(g_mips64_opcode);
531 case llvm::Triple::mipsel:
532 case llvm::Triple::mips64el:
533 return llvm::ArrayRef(g_mips64el_opcode);
535 case llvm::Triple::msp430:
536 return llvm::ArrayRef(g_msp430_opcode);
538 case llvm::Triple::systemz:
539 return llvm::ArrayRef(g_s390x_opcode);
541 case llvm::Triple::ppc:
542 case llvm::Triple::ppc64:
543 return llvm::ArrayRef(g_ppc_opcode);
545 case llvm::Triple::ppc64le:
546 return llvm::ArrayRef(g_ppcle_opcode);
548 case llvm::Triple::riscv32:
549 case llvm::Triple::riscv64: {
550 return size_hint == 2 ? llvm::ArrayRef(g_riscv_opcode_c)
551 : llvm::ArrayRef(g_riscv_opcode);
554 case llvm::Triple::loongarch32:
555 case llvm::Triple::loongarch64:
556 return llvm::ArrayRef(g_loongarch_opcode);
559 return llvm::createStringError(llvm::inconvertibleErrorCode(),
560 "CPU type not supported!");
566 case llvm::Triple::x86:
567 case llvm::Triple::x86_64:
568 case llvm::Triple::systemz:
572 case llvm::Triple::arm:
573 case llvm::Triple::aarch64:
574 case llvm::Triple::aarch64_32:
575 case llvm::Triple::mips64:
576 case llvm::Triple::mips64el:
577 case llvm::Triple::mips:
578 case llvm::Triple::mipsel:
579 case llvm::Triple::ppc:
580 case llvm::Triple::ppc64:
581 case llvm::Triple::ppc64le:
582 case llvm::Triple::riscv32:
583 case llvm::Triple::riscv64:
584 case llvm::Triple::loongarch32:
585 case llvm::Triple::loongarch64:
590 llvm_unreachable(
"CPU type not supported!");
605 LLDB_LOG(log,
"breakpoint size: {0}", breakpoint_size);
606 if (breakpoint_size == 0)
614 if (breakpoint_addr >= breakpoint_size)
615 breakpoint_addr -= breakpoint_size;
620 "pid {0} no lldb software breakpoint found at current pc with "
622 GetID(), breakpoint_addr);
631 LLDB_LOG(log,
"pid {0} tid {1}: changing PC from {2:x} to {3:x}",
GetID(),
632 thread.
GetID(), initial_pc_addr, breakpoint_addr);
639 LLDB_LOG(log,
"pid {0} tid {1}: failed to set PC: {2}",
GetID(),
653 void *buf,
size_t size,
654 size_t &bytes_read) {
659 llvm::MutableArrayRef data(
static_cast<uint8_t *
>(buf), bytes_read);
662 auto saved_opcodes = llvm::ArrayRef(pair.second.saved_opcodes);
664 if (bp_addr + saved_opcodes.size() < addr || addr + bytes_read <= bp_addr)
667 if (bp_addr < addr) {
668 saved_opcodes = saved_opcodes.drop_front(addr - bp_addr);
671 auto bp_data = data.drop_front(bp_addr - addr);
672 std::copy_n(saved_opcodes.begin(),
673 std::min(saved_opcodes.size(), bp_data.size()),
679llvm::Expected<llvm::StringRef>
682 size_t &total_bytes_read) {
683 static const size_t cache_line_size =
684 llvm::sys::Process::getPageSizeEstimate();
685 size_t bytes_read = 0;
686 size_t bytes_left = max_size;
689 char *curr_buffer = buffer;
690 total_bytes_read = 0;
693 while (bytes_left > 0 && status.
Success()) {
694 addr_t cache_line_bytes_left =
695 cache_line_size - (curr_addr % cache_line_size);
696 addr_t bytes_to_read = std::min<addr_t>(bytes_left, cache_line_bytes_left);
697 status =
ReadMemory(curr_addr,
static_cast<void *
>(curr_buffer),
698 bytes_to_read, bytes_read);
703 void *str_end = std::memchr(curr_buffer,
'\0', bytes_read);
704 if (str_end !=
nullptr) {
706 static_cast<size_t>((
static_cast<char *
>(str_end) - buffer + 1));
711 total_bytes_read += bytes_read;
712 curr_buffer += bytes_read;
713 curr_addr += bytes_read;
714 bytes_left -= bytes_read;
717 string_size = total_bytes_read - 1;
720 if (bytes_left == 0 && max_size > 0 && buffer[max_size - 1] !=
'\0') {
721 buffer[max_size - 1] =
'\0';
728 return llvm::StringRef(buffer, string_size);
737 bool notify_delegates) {
756 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
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.