From 499a40ecb545d6f55a4d246dbbdc860f6eae37da Mon Sep 17 00:00:00 2001 From: Ryan Houdek Date: Fri, 8 Dec 2023 06:03:58 -0800 Subject: [PATCH] GdbServer: Fixes crash on gdb detach GdbServer object was getting deleted before the socket thread was shutdown which was causing a crash on detach. Now on destructor it will wait for the thread to thread to exit. --- .../Tools/FEXLoader/LinuxSyscalls/GdbServer.cpp | 17 ++++++++++++++--- .../Tools/FEXLoader/LinuxSyscalls/GdbServer.h | 1 + 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/Source/Tools/FEXLoader/LinuxSyscalls/GdbServer.cpp b/Source/Tools/FEXLoader/LinuxSyscalls/GdbServer.cpp index 43b156a5a7..aae256ba7d 100644 --- a/Source/Tools/FEXLoader/LinuxSyscalls/GdbServer.cpp +++ b/Source/Tools/FEXLoader/LinuxSyscalls/GdbServer.cpp @@ -127,8 +127,12 @@ void GdbServer::WaitForThreadWakeup() { } GdbServer::~GdbServer() { + CloseListenSocket(); CoreShuttingDown = true; - close(ListenSocket); + + if (gdbServerThread->joinable()) { + gdbServerThread->join(nullptr); + } } GdbServer::GdbServer(FEXCore::Context::Context *ctx, FEX::HLE::SignalDelegator *SignalDelegation, FEXCore::HLE::SyscallHandler *const SyscallHandler) @@ -1432,8 +1436,7 @@ void GdbServer::GdbServerLoop() { } } - close(ListenSocket); - unlink(GdbUnixSocketPath.c_str()); + CloseListenSocket(); } static void* ThreadHandler(void *Arg) { FEXCore::Threads::SetThreadName("FEX:gdbserver"); @@ -1493,6 +1496,14 @@ void GdbServer::OpenListenSocket() { LogMan::Msg::IFmt("[GdbServer] gdb-multiarch -ex \"target extended-remote {}\"", GdbUnixSocketPath); } +void GdbServer::CloseListenSocket() { + if (ListenSocket != -1) { + close(ListenSocket); + ListenSocket = -1; + } + unlink(GdbUnixSocketPath.c_str()); +} + fextl::unique_ptr GdbServer::OpenSocket() { // Block until a connection arrives struct sockaddr_storage their_addr{}; diff --git a/Source/Tools/FEXLoader/LinuxSyscalls/GdbServer.h b/Source/Tools/FEXLoader/LinuxSyscalls/GdbServer.h index b21308e32d..a6db0d2ec5 100644 --- a/Source/Tools/FEXLoader/LinuxSyscalls/GdbServer.h +++ b/Source/Tools/FEXLoader/LinuxSyscalls/GdbServer.h @@ -39,6 +39,7 @@ class GdbServer { void Break(int signal); void OpenListenSocket(); + void CloseListenSocket(); enum class WaitForConnectionResult { CONNECTION, ERROR,