Skip to content

Commit

Permalink
[GDBStub] Add memory map, MSR/FPSCR, handle Kill request
Browse files Browse the repository at this point in the history
IDA seems to ignore the map though, sad!
  • Loading branch information
emoose committed Oct 6, 2024
1 parent 7c0587c commit 416b2d0
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
35 changes: 30 additions & 5 deletions src/xenia/debug/gdb/gdbstub.cc
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,20 @@ constexpr const char* kGdbReplyError = "E01";

constexpr int kSignalSigtrap = 5;

constexpr char memory_map[] =
R"(l<?xml version="1.0"?>
<memory-map>
<!-- Based on memory.cc Initialize() -->
<memory type="ram" start="0x10000" length="0x3FFF0000"/>
<memory type="ram" start="0x40000000" length="0x3F000000"/>
<memory type="ram" start="0x80000000" length="0x10000000"/>
<memory type="ram" start="0x90000000" length="0x10000000"/>
<memory type="ram" start="0xA0000000" length="0x20000000"/>
<memory type="ram" start="0xC0000000" length="0x20000000"/>
<memory type="ram" start="0xE0000000" length="0x1FD00000"/>
</memory-map>
)";

// must start with l for debugger to accept it
// TODO: add power-altivec.xml (and update ReadRegister to support it)
constexpr char target_xml[] =
Expand Down Expand Up @@ -228,9 +242,8 @@ std::string GDBStub::ReadRegister(xe::cpu::ThreadDebugInfo* thread,
}
return u32_to_padded_hex(0);
}
// msr?
case 65:
return std::string(8, 'x');
return u32_to_padded_hex((uint32_t)thread->guest_context.msr);
case 66:
return u32_to_padded_hex((uint32_t)thread->guest_context.cr());
case 67:
Expand All @@ -242,7 +255,7 @@ std::string GDBStub::ReadRegister(xe::cpu::ThreadDebugInfo* thread,
return std::string(8, 'x');
// fpscr
case 70:
return std::string(8, 'x');
return u32_to_padded_hex(thread->guest_context.fpscr.value);
default:
if (rid > 70) return "";
return (rid > 31) ? u64_to_padded_hex(*(uint64_t*)&(
Expand Down Expand Up @@ -298,7 +311,7 @@ void GDBStub::Listen(std::unique_ptr<Socket>& client) {
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}

// Check if we need to notify client about anything...
// Check if we need to notify client about anything
{
std::unique_lock<std::mutex> lock(mtx_);
if (cache_.notify_stopped) {
Expand Down Expand Up @@ -349,6 +362,7 @@ std::string GetPacketFriendlyName(const std::string& packetCommand) {
{"qfThreadInfo", "qfThreadInfo"},
{"qC", "GetThreadId"},
{"D", "Detach"},
{"k", "KillRequest"},
{"\03", "Break"},
};

Expand Down Expand Up @@ -557,6 +571,9 @@ std::string GDBStub::ExecutionPause() {
#ifdef DEBUG
debugging::DebugPrint("GDBStub: ExecutionPause\n");
#endif
if (processor_->execution_state() != cpu::ExecutionState::kRunning) {
return kGdbReplyError;
}
processor_->Pause();
return kGdbReplyOK;
}
Expand Down Expand Up @@ -612,6 +629,8 @@ std::string GDBStub::ReadMemory(const std::string& data) {
return result;
}

std::string GDBStub::BuildMemoryMap() { return memory_map; }

std::string GDBStub::BuildTargetXml() { return target_xml; }

std::string GDBStub::BuildThreadList() {
Expand Down Expand Up @@ -804,6 +823,9 @@ std::string GDBStub::HandleGDBCommand(const GDBCommand& command) {
// Detach
{"D", [&](const GDBCommand& cmd) { return DebuggerDetached(); }},

// Kill request (just treat as detach for now)
{"k", [&](const GDBCommand& cmd) { return DebuggerDetached(); }},

// Enable extended mode
{"!", [&](const GDBCommand& cmd) { return kGdbReplyOK; }},

Expand Down Expand Up @@ -887,6 +909,8 @@ std::string GDBStub::HandleGDBCommand(const GDBCommand& command) {
auto sub_cmd = param.substr(0, param.find(':'));
if (sub_cmd == "features") {
return BuildTargetXml();
} else if (sub_cmd == "memory-map") {
return BuildMemoryMap();
} else if (sub_cmd == "threads") {
return BuildThreadList();
}
Expand All @@ -895,7 +919,8 @@ std::string GDBStub::HandleGDBCommand(const GDBCommand& command) {
// Supported features (TODO: memory map)
{"qSupported",
[&](const GDBCommand& cmd) {
return "PacketSize=1024;qXfer:features:read+;qXfer:threads:read+";
return "PacketSize=1024;qXfer:features:read+;qXfer:threads:read+;"
"qXfer:memory-map:read+";
}},
// Thread list (IDA requests this but ignores in favor of qXfer?)
{"qfThreadInfo",
Expand Down
1 change: 1 addition & 0 deletions src/xenia/debug/gdb/gdbstub.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ class GDBStub : public cpu::DebugListener {
std::string ExecutionContinue();
std::string ExecutionStep();
std::string ReadMemory(const std::string& data);
std::string BuildMemoryMap();
std::string BuildTargetXml();
std::string BuildThreadList();

Expand Down

0 comments on commit 416b2d0

Please sign in to comment.