Skip to content

Commit

Permalink
Merge pull request #3265 from Sonicadvance1/gdb_unix_socket
Browse files Browse the repository at this point in the history
GdbServer: Switch over to a unix domain socket
  • Loading branch information
Sonicadvance1 committed Nov 11, 2023
2 parents 3767f36 + c7193b5 commit 0357bb2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 24 deletions.
50 changes: 26 additions & 24 deletions Source/Tools/FEXLoader/LinuxSyscalls/GdbServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ desc: Provides a gdb interface to the guest state
#include <memory>
#include <optional>

#include <Common/FEXServerClient.h>
#include <FEXCore/Config/Config.h>
#include <FEXCore/Core/CodeLoader.h>
#include <FEXCore/Core/Context.h>
Expand All @@ -32,6 +33,7 @@ desc: Provides a gdb interface to the guest state
#include <FEXCore/fextl/sstream.h>
#include <FEXCore/fextl/string.h>
#include <FEXCore/fextl/vector.h>
#include <FEXHeaderUtils/Filesystem.h>

#include <atomic>
#include <cstring>
Expand All @@ -47,6 +49,7 @@ desc: Provides a gdb interface to the guest state
#include <stddef.h>
#include <string_view>
#include <sys/stat.h>
#include <sys/un.h>
#include <unistd.h>
#include <utility>

Expand Down Expand Up @@ -1275,6 +1278,7 @@ void GdbServer::GdbServerLoop() {
}

close(ListenSocket);
unlink(GdbUnixSocketPath.c_str());
}
static void* ThreadHandler(void *Arg) {
auto This = reinterpret_cast<FEX::GdbServer*>(Arg);
Expand All @@ -1289,46 +1293,44 @@ void GdbServer::StartThread() {
}

void GdbServer::OpenListenSocket() {
// getaddrinfo allocates memory that can't be removed.
FEXCore::Allocator::YesIKnowImNotSupposedToUseTheGlibcAllocator glibc;
struct addrinfo hints, *res;

memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;

if(getaddrinfo(NULL, "8086", &hints, &res) < 0) {
perror("getaddrinfo");
const auto GdbUnixPath = fextl::fmt::format("{}/FEX_gdbserver/", FEXServerClient::GetServerMountFolder());
if (FHU::Filesystem::CreateDirectory(GdbUnixPath) == FHU::Filesystem::CreateDirectoryResult::ERROR) {
LogMan::Msg::EFmt("[GdbServer] Couldn't create gdbserver folder {}", GdbUnixPath);
return;
}

int on = 1;
GdbUnixSocketPath = fextl::fmt::format("{}{}-gdb", GdbUnixPath, ::getpid());

ListenSocket = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
if (ListenSocket < 0) {
perror("socket");
}
if(setsockopt(ListenSocket, SOL_SOCKET, SO_REUSEADDR, (char*)&on, sizeof(on)) < 0) {
perror("setsockopt");
close(ListenSocket);
ListenSocket = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
if (ListenSocket == -1) {
LogMan::Msg::EFmt("[GdbServer] Couldn't open AF_UNIX socket {} {}", errno, strerror(errno));
return;
}

if (bind(ListenSocket, res->ai_addr, res->ai_addrlen) < 0) {
perror("bind");
struct sockaddr_un addr{};
addr.sun_family = AF_UNIX;
strncpy(addr.sun_path, GdbUnixSocketPath.data(), sizeof(addr.sun_path));
size_t SizeOfAddr = offsetof(sockaddr_un, sun_path) + GdbUnixSocketPath.size();

// Bind the socket to the path
int Result = bind(ListenSocket, reinterpret_cast<struct sockaddr*>(&addr), SizeOfAddr);
if (Result == -1) {
LogMan::Msg::EFmt("[GdbServer] Couldn't bind AF_UNIX socket '{}': {} {}\n", addr.sun_path, errno, strerror(errno));
close(ListenSocket);
ListenSocket = -1;
return;
}

listen(ListenSocket, 1);

freeaddrinfo(res);
LogMan::Msg::IFmt("[GdbServer] Waiting for connection on {}", GdbUnixSocketPath);
LogMan::Msg::IFmt("[GdbServer] gdb-multiarch -ex \"target extended-remote {}\"", GdbUnixSocketPath);
}

fextl::unique_ptr<std::iostream> GdbServer::OpenSocket() {
// Block until a connection arrives
struct sockaddr_storage their_addr{};
socklen_t addr_size{};

LogMan::Msg::IFmt("GdbServer, waiting for connection on localhost:8086");
int new_fd = accept(ListenSocket, (struct sockaddr *)&their_addr, &addr_size);

return fextl::make_unique<FEXCore::Utils::NetStream>(new_fd);
Expand Down
1 change: 1 addition & 0 deletions Source/Tools/FEXLoader/LinuxSyscalls/GdbServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ class GdbServer {
std::array<bool, FEXCore::SignalDelegator::MAX_SIGNALS + 1> PassSignals{};
uint32_t CurrentDebuggingThread{};
int ListenSocket{};
fextl::string GdbUnixSocketPath{};
FEX_CONFIG_OPT(Filename, APP_FILENAME);
FEX_CONFIG_OPT(Is64BitMode, IS64BIT_MODE);
};
Expand Down

0 comments on commit 0357bb2

Please sign in to comment.