From de6c0defb358b6d12831a594b1b4c615003566b8 Mon Sep 17 00:00:00 2001 From: Liam Date: Fri, 10 Jun 2022 09:11:02 -0400 Subject: [PATCH 1/3] core/debugger: support operation in yuzu-cmd --- src/yuzu_cmd/config.cpp | 2 ++ src/yuzu_cmd/default_ini.h | 5 +++++ src/yuzu_cmd/yuzu.cpp | 8 ++++++++ 3 files changed, 15 insertions(+) diff --git a/src/yuzu_cmd/config.cpp b/src/yuzu_cmd/config.cpp index fc16f0f0c4..fc4744fb02 100644 --- a/src/yuzu_cmd/config.cpp +++ b/src/yuzu_cmd/config.cpp @@ -344,6 +344,8 @@ void Config::ReadValues() { ReadSetting("Debugging", Settings::values.use_debug_asserts); ReadSetting("Debugging", Settings::values.use_auto_stub); ReadSetting("Debugging", Settings::values.disable_macro_jit); + ReadSetting("Debugging", Settings::values.use_gdbstub); + ReadSetting("Debugging", Settings::values.gdbstub_port); const auto title_list = sdl2_config->Get("AddOns", "title_ids", ""); std::stringstream ss(title_list); diff --git a/src/yuzu_cmd/default_ini.h b/src/yuzu_cmd/default_ini.h index f34d6b7281..d5281863fb 100644 --- a/src/yuzu_cmd/default_ini.h +++ b/src/yuzu_cmd/default_ini.h @@ -437,6 +437,11 @@ disable_macro_jit=false # Presents guest frames as they become available. Experimental. # false: Disabled (default), true: Enabled disable_fps_limit=false +# Determines whether to enable the GDB stub and wait for the debugger to attach before running. +# false: Disabled (default), true: Enabled +use_gdbstub=false +# The port to use for the GDB server, if it is enabled. +gdbstub_port=6543 [WebService] # Whether or not to enable telemetry diff --git a/src/yuzu_cmd/yuzu.cpp b/src/yuzu_cmd/yuzu.cpp index ab12dd15d7..a0d619c48a 100644 --- a/src/yuzu_cmd/yuzu.cpp +++ b/src/yuzu_cmd/yuzu.cpp @@ -217,7 +217,15 @@ int main(int argc, char** argv) { [](VideoCore::LoadCallbackStage, size_t value, size_t total) {}); } + system.RegisterExitCallback([&] { + // Just exit right away. + exit(0); + }); + void(system.Run()); + if (system.DebuggerEnabled()) { + system.InitializeDebugger(); + } while (emu_window->IsOpen()) { emu_window->WaitEvent(); } From 1f0fee33edf06bb237a952b78b6e117ba81cbdbb Mon Sep 17 00:00:00 2001 From: Liam Date: Fri, 10 Jun 2022 09:17:12 -0400 Subject: [PATCH 2/3] core/debugger: fix a number of shutdown deadlocks --- src/core/core.cpp | 6 +++ src/core/core.h | 3 ++ src/core/debugger/debugger.cpp | 57 ++++++++++++++++++++++---- src/core/debugger/debugger.h | 5 +++ src/core/debugger/debugger_interface.h | 5 +++ src/core/debugger/gdbstub.cpp | 2 + src/core/debugger/gdbstub.h | 1 + src/yuzu/main.cpp | 1 + src/yuzu_cmd/yuzu.cpp | 1 + 9 files changed, 73 insertions(+), 8 deletions(-) diff --git a/src/core/core.cpp b/src/core/core.cpp index 7d974ba65e..954136adb6 100644 --- a/src/core/core.cpp +++ b/src/core/core.cpp @@ -493,6 +493,12 @@ void System::Shutdown() { impl->Shutdown(); } +void System::DetachDebugger() { + if (impl->debugger) { + impl->debugger->NotifyShutdown(); + } +} + std::unique_lock System::StallCPU() { return impl->StallCPU(); } diff --git a/src/core/core.h b/src/core/core.h index 94477206e5..5c367349e2 100644 --- a/src/core/core.h +++ b/src/core/core.h @@ -160,6 +160,9 @@ public: /// Shutdown the emulated system. void Shutdown(); + /// Forcibly detach the debugger if it is running. + void DetachDebugger(); + std::unique_lock StallCPU(); void UnstallCPU(); diff --git a/src/core/debugger/debugger.cpp b/src/core/debugger/debugger.cpp index 8d64990edc..1d7f9a775b 100644 --- a/src/core/debugger/debugger.cpp +++ b/src/core/debugger/debugger.cpp @@ -42,6 +42,16 @@ static std::span ReceiveInto(Readable& r, Buffer& buffer) { return received_data; } +enum class SignalType { + Stopped, + ShuttingDown, +}; + +struct SignalInfo { + SignalType type; + Kernel::KThread* thread; +}; + namespace Core { class DebuggerImpl : public DebuggerBackend { @@ -56,7 +66,7 @@ public: ShutdownServer(); } - bool NotifyThreadStopped(Kernel::KThread* thread) { + bool SignalDebugger(SignalInfo signal_info) { std::scoped_lock lk{connection_lock}; if (stopped) { @@ -64,9 +74,13 @@ public: // It should be ignored. return false; } - stopped = true; - boost::asio::write(signal_pipe, boost::asio::buffer(&thread, sizeof(thread))); + // Set up the state. + stopped = true; + info = signal_info; + + // Write a single byte into the pipe to wake up the debug interface. + boost::asio::write(signal_pipe, boost::asio::buffer(&stopped, sizeof(stopped))); return true; } @@ -124,7 +138,7 @@ private: Common::SetCurrentThreadName("yuzu:Debugger"); // Set up the client signals for new data. - AsyncReceiveInto(signal_pipe, active_thread, [&](auto d) { PipeData(d); }); + AsyncReceiveInto(signal_pipe, pipe_data, [&](auto d) { PipeData(d); }); AsyncReceiveInto(client_socket, client_data, [&](auto d) { ClientData(d); }); // Stop the emulated CPU. @@ -142,9 +156,28 @@ private: } void PipeData(std::span data) { - AllCoreStop(); - UpdateActiveThread(); - frontend->Stopped(active_thread); + switch (info.type) { + case SignalType::Stopped: + // Stop emulation. + AllCoreStop(); + + // Notify the client. + active_thread = info.thread; + UpdateActiveThread(); + frontend->Stopped(active_thread); + + break; + case SignalType::ShuttingDown: + frontend->ShuttingDown(); + + // Wait for emulation to shut down gracefully now. + suspend.reset(); + signal_pipe.close(); + client_socket.shutdown(boost::asio::socket_base::shutdown_both); + LOG_INFO(Debug_GDBStub, "Shut down server"); + + break; + } } void ClientData(std::span data) { @@ -246,7 +279,9 @@ private: boost::asio::ip::tcp::socket client_socket; std::optional> suspend; + SignalInfo info; Kernel::KThread* active_thread; + bool pipe_data; bool stopped; std::array client_data; @@ -263,7 +298,13 @@ Debugger::Debugger(Core::System& system, u16 port) { Debugger::~Debugger() = default; bool Debugger::NotifyThreadStopped(Kernel::KThread* thread) { - return impl && impl->NotifyThreadStopped(thread); + return impl && impl->SignalDebugger(SignalInfo{SignalType::Stopped, thread}); +} + +void Debugger::NotifyShutdown() { + if (impl) { + impl->SignalDebugger(SignalInfo{SignalType::ShuttingDown, nullptr}); + } } } // namespace Core diff --git a/src/core/debugger/debugger.h b/src/core/debugger/debugger.h index ea36c6ab20..f9738ca3db 100644 --- a/src/core/debugger/debugger.h +++ b/src/core/debugger/debugger.h @@ -35,6 +35,11 @@ public: */ bool NotifyThreadStopped(Kernel::KThread* thread); + /** + * Notify the debugger that a shutdown is being performed now and disconnect. + */ + void NotifyShutdown(); + private: std::unique_ptr impl; }; diff --git a/src/core/debugger/debugger_interface.h b/src/core/debugger/debugger_interface.h index 35ba0bc619..c0bb4ecafe 100644 --- a/src/core/debugger/debugger_interface.h +++ b/src/core/debugger/debugger_interface.h @@ -66,6 +66,11 @@ public: */ virtual void Stopped(Kernel::KThread* thread) = 0; + /** + * Called when emulation is shutting down. + */ + virtual void ShuttingDown() = 0; + /** * Called when new data is asynchronously received on the client socket. * A list of actions to perform is returned. diff --git a/src/core/debugger/gdbstub.cpp b/src/core/debugger/gdbstub.cpp index f52d78829c..52e76f6590 100644 --- a/src/core/debugger/gdbstub.cpp +++ b/src/core/debugger/gdbstub.cpp @@ -106,6 +106,8 @@ GDBStub::~GDBStub() = default; void GDBStub::Connected() {} +void GDBStub::ShuttingDown() {} + void GDBStub::Stopped(Kernel::KThread* thread) { SendReply(arch->ThreadStatus(thread, GDB_STUB_SIGTRAP)); } diff --git a/src/core/debugger/gdbstub.h b/src/core/debugger/gdbstub.h index 1bb638187d..ec934c77e7 100644 --- a/src/core/debugger/gdbstub.h +++ b/src/core/debugger/gdbstub.h @@ -23,6 +23,7 @@ public: void Connected() override; void Stopped(Kernel::KThread* thread) override; + void ShuttingDown() override; std::vector ClientData(std::span data) override; private: diff --git a/src/yuzu/main.cpp b/src/yuzu/main.cpp index 80e6d23a54..c9288b4fe6 100644 --- a/src/yuzu/main.cpp +++ b/src/yuzu/main.cpp @@ -1545,6 +1545,7 @@ void GMainWindow::ShutdownGame() { AllowOSSleep(); + system->DetachDebugger(); discord_rpc->Pause(); emu_thread->RequestStop(); diff --git a/src/yuzu_cmd/yuzu.cpp b/src/yuzu_cmd/yuzu.cpp index a0d619c48a..0dce5e2744 100644 --- a/src/yuzu_cmd/yuzu.cpp +++ b/src/yuzu_cmd/yuzu.cpp @@ -229,6 +229,7 @@ int main(int argc, char** argv) { while (emu_window->IsOpen()) { emu_window->WaitEvent(); } + system.DetachDebugger(); void(system.Pause()); system.Shutdown(); From c3cc65a11eddc0a72b31e1c1ff5fae997be21016 Mon Sep 17 00:00:00 2001 From: Liam Date: Fri, 10 Jun 2022 12:49:18 -0400 Subject: [PATCH 3/3] yuzu-cmd: ignore bogus timeous from SDL --- src/yuzu_cmd/emu_window/emu_window_sdl2.cpp | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/yuzu_cmd/emu_window/emu_window_sdl2.cpp b/src/yuzu_cmd/emu_window/emu_window_sdl2.cpp index 71c413e647..8e38724dbc 100644 --- a/src/yuzu_cmd/emu_window/emu_window_sdl2.cpp +++ b/src/yuzu_cmd/emu_window/emu_window_sdl2.cpp @@ -162,7 +162,15 @@ void EmuWindow_SDL2::WaitEvent() { SDL_Event event; if (!SDL_WaitEvent(&event)) { - LOG_CRITICAL(Frontend, "SDL_WaitEvent failed: {}", SDL_GetError()); + const char* error = SDL_GetError(); + if (!error || strcmp(error, "") == 0) { + // https://github.com/libsdl-org/SDL/issues/5780 + // Sometimes SDL will return without actually having hit an error condition; + // just ignore it in this case. + return; + } + + LOG_CRITICAL(Frontend, "SDL_WaitEvent failed: {}", error); exit(1); }