From 989d4a7a41f449af0ea09e34bee331a3a3ac8170 Mon Sep 17 00:00:00 2001 From: Liam Date: Tue, 31 May 2022 14:37:37 -0400 Subject: core/debugger: Improved stepping mechanism and misc fixes --- src/core/debugger/gdbstub.cpp | 99 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 92 insertions(+), 7 deletions(-) (limited to 'src/core/debugger/gdbstub.cpp') diff --git a/src/core/debugger/gdbstub.cpp b/src/core/debugger/gdbstub.cpp index 718c45952..44ebbe3e0 100644 --- a/src/core/debugger/gdbstub.cpp +++ b/src/core/debugger/gdbstub.cpp @@ -6,8 +6,7 @@ #include #include -#include -#include +#include #include "common/hex_util.h" #include "common/logging/log.h" @@ -114,6 +113,11 @@ void GDBStub::ExecuteCommand(std::string_view packet, std::vector= 1) { thread = GetThreadByID(thread_id); + } else { + thread = backend.GetActiveThread(); } if (thread) { @@ -141,6 +147,7 @@ void GDBStub::ExecuteCommand(std::string_view packet, std::vectorGetWaitReasonForDebugging()) { + case Kernel::ThreadWaitReasonForDebugging::Sleep: + return "Sleep"; + case Kernel::ThreadWaitReasonForDebugging::IPC: + return "IPC"; + case Kernel::ThreadWaitReasonForDebugging::Synchronization: + return "Synchronization"; + case Kernel::ThreadWaitReasonForDebugging::ConditionVar: + return "ConditionVar"; + case Kernel::ThreadWaitReasonForDebugging::Arbitration: + return "Arbitration"; + case Kernel::ThreadWaitReasonForDebugging::Suspended: + return "Suspended"; + default: + return "Unknown"; + } +} + +static std::string GetThreadState(const Kernel::KThread* thread) { + switch (thread->GetState()) { + case Kernel::ThreadState::Initialized: + return "Initialized"; + case Kernel::ThreadState::Waiting: + return fmt::format("Waiting ({})", GetThreadWaitReason(thread)); + case Kernel::ThreadState::Runnable: + return "Runnable"; + case Kernel::ThreadState::Terminated: + return "Terminated"; + default: + return "Unknown"; + } +} + void GDBStub::HandleQuery(std::string_view command) { if (command.starts_with("TStatus")) { // no tracepoint support SendReply("T0"); } else if (command.starts_with("Supported")) { - SendReply("PacketSize=4000;qXfer:features:read+;qXfer:threads:read+;qXfer:libraries:read+"); + SendReply("PacketSize=4000;qXfer:features:read+;qXfer:threads:read+;qXfer:libraries:read+;" + "vContSupported+;QStartNoAckMode+"); } else if (command.starts_with("Xfer:features:read:target.xml:")) { const auto offset{command.substr(30)}; const auto amount{command.substr(command.find(',') + 1)}; @@ -297,18 +339,57 @@ void GDBStub::HandleQuery(std::string_view command) { const auto& threads = system.GlobalSchedulerContext().GetThreadList(); for (const auto& thread : threads) { - buffer += - fmt::format(R"()", - thread->GetThreadID(), thread->GetActiveCore(), thread->GetThreadID()); + buffer += fmt::format(R"({})", + thread->GetThreadID(), thread->GetActiveCore(), + thread->GetThreadID(), GetThreadState(thread)); } buffer += ""; SendReply(buffer); + } else if (command.starts_with("Attached")) { + SendReply("0"); + } else if (command.starts_with("StartNoAckMode")) { + no_ack = true; + SendReply(GDB_STUB_REPLY_OK); } else { SendReply(GDB_STUB_REPLY_EMPTY); } } +void GDBStub::HandleVCont(std::string_view command, std::vector& actions) { + if (command == "?") { + // Continuing and stepping are supported + // (signal is ignored, but required for GDB to use vCont) + SendReply("vCont;c;C;s;S"); + return; + } + + Kernel::KThread* stepped_thread{nullptr}; + bool lock_execution{true}; + + std::vector entries; + boost::split(entries, command.substr(1), boost::is_any_of(";")); + for (const auto& thread_action : entries) { + std::vector parts; + boost::split(parts, thread_action, boost::is_any_of(":")); + + if (parts.size() == 1 && (parts[0] == "c" || parts[0].starts_with("C"))) { + lock_execution = false; + } + if (parts.size() == 2 && (parts[0] == "s" || parts[0].starts_with("S"))) { + stepped_thread = GetThreadByID(strtoll(parts[1].data(), nullptr, 16)); + } + } + + if (stepped_thread) { + backend.SetActiveThread(stepped_thread); + actions.push_back(lock_execution ? DebuggerAction::StepThreadLocked + : DebuggerAction::StepThreadUnlocked); + } else { + actions.push_back(DebuggerAction::Continue); + } +} + Kernel::KThread* GDBStub::GetThreadByID(u64 thread_id) { const auto& threads{system.GlobalSchedulerContext().GetThreadList()}; for (auto* thread : threads) { @@ -374,6 +455,10 @@ void GDBStub::SendReply(std::string_view data) { } void GDBStub::SendStatus(char status) { + if (no_ack) { + return; + } + std::array buf = {static_cast(status)}; LOG_TRACE(Debug_GDBStub, "Writing status: {}", status); backend.WriteToClient(buf); -- cgit v1.2.3