Skip to content

Commit

Permalink
[Debug] Processor: breakpoint address fixes
Browse files Browse the repository at this point in the history
- Update OnThreadBreakpointHit to use bp->ContainsHostAddress

Sometimes guest PC doesn't map to the x64 exactly, so ResolveStack may return
a guest PC different to the breakpoint that triggered it

Processor code would just skip the breakpoint entirely if PC didn't match
so any debugger wouldn't know what BP caused it

Luckily BP does have a ContainsHostAddress func which seems perfect, but was
somehow left unused

Also added a hack to nudge the PC we tell GDB about to the BP's PC

- Update StepGuest/HostInstruction to add step BP to front of map

Fixes an issue where a different BP might get triggered before the step BP
which could cause debugger to step again, causing assert error as a step BP
already existed

I noticed one more issue, if BPs are set on instructions very close together
it might act on the same x64 instruction, causing assert when a BP is already
placed and another BP tries to set on the same addr...

Not really sure what best way to fix that is yet, for now just don't place
BPs too close together!
  • Loading branch information
emoose committed Oct 6, 2024
1 parent c42c15d commit 3b80d86
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 7 deletions.
13 changes: 10 additions & 3 deletions src/xenia/cpu/processor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,8 @@ bool Processor::OnThreadBreakpointHit(Exception* ex) {
if ((scan_breakpoint->address_type() == Breakpoint::AddressType::kGuest &&
scan_breakpoint->guest_address() == frame.guest_pc) ||
(scan_breakpoint->address_type() == Breakpoint::AddressType::kHost &&
scan_breakpoint->host_address() == frame.host_pc)) {
scan_breakpoint->host_address() == frame.host_pc) ||
scan_breakpoint->ContainsHostAddress(frame.host_pc)) {
breakpoint = scan_breakpoint;
break;
}
Expand Down Expand Up @@ -937,7 +938,10 @@ void Processor::StepHostInstruction(uint32_t thread_id) {
thread_info->step_breakpoint.reset();
OnStepCompleted(thread_info);
}));
AddBreakpoint(thread_info->step_breakpoint.get());

// Add to front of breakpoints map, so this should get evaluated first
breakpoints_.insert(breakpoints_.begin(), thread_info->step_breakpoint.get());

thread_info->step_breakpoint->Resume();

// ResumeAllBreakpoints();
Expand Down Expand Up @@ -970,7 +974,10 @@ void Processor::StepGuestInstruction(uint32_t thread_id) {
thread_info->step_breakpoint.reset();
OnStepCompleted(thread_info);
}));
AddBreakpoint(thread_info->step_breakpoint.get());

// Add to front of breakpoints map, so this should get evaluated first
breakpoints_.insert(breakpoints_.begin(), thread_info->step_breakpoint.get());

thread_info->step_breakpoint->Resume();

// ResumeAllBreakpoints();
Expand Down
27 changes: 23 additions & 4 deletions src/xenia/debug/gdb/gdbstub.cc
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,8 @@ uint8_t from_hexchar(char c) {
return 0;
}

std::string get_reg(xe::cpu::ThreadDebugInfo* thread, uint32_t rid) {
std::string GDBStub::ReadRegister(xe::cpu::ThreadDebugInfo* thread,
uint32_t rid) {
// Send registers as 32-bit, otherwise some debuggers will switch to 64-bit
// mode (eg. IDA will switch to 64-bit and refuse to allow decompiler to work
// with it)
Expand All @@ -195,6 +196,16 @@ std::string get_reg(xe::cpu::ThreadDebugInfo* thread, uint32_t rid) {
switch (rid) {
// pc
case 64: {
// If we recently hit a BP then debugger is likely asking for registers
// for it
//
// Lie about the PC and say it's the BP address, since PC might not always
// match
if (cache_.notify_bp_guest_address != -1) {
auto ret = u32_to_padded_hex((uint32_t)cache_.notify_bp_guest_address);
cache_.notify_bp_guest_address = -1;
return ret;
}
// Search for first frame that has guest_pc attached, GDB doesn't care
// about host
for (auto& frame : thread->frames) {
Expand Down Expand Up @@ -560,7 +571,7 @@ void GDBStub::UpdateCache() {

std::string GDBStub::ReadRegister(const std::string& data) {
uint32_t rid = hex_to_u32(data);
std::string result = get_reg(cache_.cur_thread_info(), rid);
std::string result = ReadRegister(cache_.cur_thread_info(), rid);
if (result.empty()) {
return kGdbReplyError; // TODO: is this error correct?
}
Expand All @@ -571,7 +582,7 @@ std::string GDBStub::ReadRegisters() {
std::string result;
result.reserve(68 * 16 + 3 * 8);
for (int i = 0; i < 71; ++i) {
result += get_reg(cache_.cur_thread_info(), i);
result += ReadRegister(cache_.cur_thread_info(), i);
}
return result;
}
Expand All @@ -594,7 +605,8 @@ std::string GDBStub::ExecutionContinue() {

std::string GDBStub::ExecutionStep() {
#ifdef DEBUG
debugging::DebugPrint("GDBStub: ExecutionStep (thread {})\n", cache_.last_bp_thread_id);
debugging::DebugPrint("GDBStub: ExecutionStep (thread {})\n",
cache_.last_bp_thread_id);
#endif

if (cache_.last_bp_thread_id != -1)
Expand Down Expand Up @@ -666,6 +678,12 @@ std::string GDBStub::GetThreadStateReply(uint32_t thread_id, uint8_t signal) {
}
}

// If BP was hit use the address of it, so debugger can match it up to its
// BP list
if (cache_.notify_bp_guest_address != -1) {
pc_value = cache_.notify_bp_guest_address;
}

return fmt::format(
"T{:02x}{:02x}:{};{:02x}:{};thread:{:x};", signal, PC_REGISTER,
u32_to_padded_hex((uint32_t)pc_value), LR_REGISTER,
Expand Down Expand Up @@ -786,6 +804,7 @@ void GDBStub::OnBreakpointHit(Breakpoint* breakpoint,
breakpoint->address(), thread_info->thread_id);
#endif

cache_.notify_bp_guest_address = breakpoint->address();
cache_.notify_bp_thread_id = thread_info->thread_id;
cache_.last_bp_thread_id = thread_info->thread_id;
UpdateCache();
Expand Down
2 changes: 2 additions & 0 deletions src/xenia/debug/gdb/gdbstub.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class GDBStub : public cpu::DebugListener {

void UpdateCache();

std::string ReadRegister(xe::cpu::ThreadDebugInfo* thread, uint32_t rid);
std::string ReadRegister(const std::string& data);
std::string ReadRegisters();
std::string ExecutionPause();
Expand Down Expand Up @@ -93,6 +94,7 @@ class GDBStub : public cpu::DebugListener {
uint32_t cur_thread_id = -1;
uint32_t last_bp_thread_id = -1;

uint64_t notify_bp_guest_address = -1;
uint32_t notify_bp_thread_id = -1;
bool notify_stopped = false;

Expand Down

0 comments on commit 3b80d86

Please sign in to comment.