Merge pull request #9559 from iwubcode/gdb-stub-raii

Common / Core: add raii object that cleans up WSA on destruction in gdb-stub
This commit is contained in:
JMC47 2021-03-05 05:28:31 -05:00 committed by GitHub
commit fc86e554e0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 67 additions and 26 deletions

View File

@ -110,6 +110,8 @@ add_library(common
SettingsHandler.h SettingsHandler.h
SFMLHelper.cpp SFMLHelper.cpp
SFMLHelper.h SFMLHelper.h
SocketContext.cpp
SocketContext.h
SPSCQueue.h SPSCQueue.h
StringUtil.cpp StringUtil.cpp
StringUtil.h StringUtil.h

View File

@ -0,0 +1,22 @@
// Copyright 2021 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.
#include "Common/SocketContext.h"
namespace Common
{
#ifdef _WIN32
SocketContext::SocketContext()
{
static_cast<void>(WSAStartup(MAKEWORD(2, 2), &m_data));
}
SocketContext::~SocketContext()
{
WSACleanup();
}
#else
SocketContext::SocketContext() = default;
SocketContext::~SocketContext() = default;
#endif
} // namespace Common

View File

@ -0,0 +1,30 @@
// Copyright 2021 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.
#pragma once
#ifdef _WIN32
#include <WinSock2.h>
#endif
namespace Common
{
class SocketContext
{
public:
SocketContext();
~SocketContext();
SocketContext(const SocketContext&) = delete;
SocketContext(SocketContext&&) = delete;
SocketContext& operator=(const SocketContext&) = delete;
SocketContext& operator=(SocketContext&&) = delete;
private:
#ifdef _WIN32
WSADATA m_data;
#endif
};
} // namespace Common

View File

@ -65,17 +65,6 @@ enum SOResultCode : s32
NetIPTopDevice::NetIPTopDevice(Kernel& ios, const std::string& device_name) NetIPTopDevice::NetIPTopDevice(Kernel& ios, const std::string& device_name)
: Device(ios, device_name) : Device(ios, device_name)
{ {
#ifdef _WIN32
const int ret = WSAStartup(MAKEWORD(2, 2), &InitData);
INFO_LOG_FMT(IOS_NET, "WSAStartup: {}", ret);
#endif
}
NetIPTopDevice::~NetIPTopDevice()
{
#ifdef _WIN32
WSACleanup();
#endif
} }
void NetIPTopDevice::DoState(PointerWrap& p) void NetIPTopDevice::DoState(PointerWrap& p)

View File

@ -7,6 +7,7 @@
#include <string> #include <string>
#include "Common/CommonTypes.h" #include "Common/CommonTypes.h"
#include "Common/SocketContext.h"
#include "Core/IOS/Device.h" #include "Core/IOS/Device.h"
#ifdef _WIN32 #ifdef _WIN32
@ -65,7 +66,6 @@ class NetIPTopDevice : public Device
{ {
public: public:
NetIPTopDevice(Kernel& ios, const std::string& device_name); NetIPTopDevice(Kernel& ios, const std::string& device_name);
virtual ~NetIPTopDevice();
void DoState(PointerWrap& p) override; void DoState(PointerWrap& p) override;
std::optional<IPCReply> IOCtl(const IOCtlRequest& request) override; std::optional<IPCReply> IOCtl(const IOCtlRequest& request) override;
@ -99,8 +99,6 @@ private:
IPCReply HandleGetAddressInfoRequest(const IOCtlVRequest& request); IPCReply HandleGetAddressInfoRequest(const IOCtlVRequest& request);
IPCReply HandleICMPPingRequest(const IOCtlVRequest& request); IPCReply HandleICMPPingRequest(const IOCtlVRequest& request);
#ifdef _WIN32 Common::SocketContext m_socket_context;
WSADATA InitData;
#endif
}; };
} // namespace IOS::HLE } // namespace IOS::HLE

View File

@ -4,6 +4,7 @@
// Originally written by Sven Peter <sven@fail0verflow.com> for anergistic. // Originally written by Sven Peter <sven@fail0verflow.com> for anergistic.
#include <optional>
#include <signal.h> #include <signal.h>
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
@ -23,6 +24,7 @@ typedef SSIZE_T ssize_t;
#endif #endif
#include "Common/Logging/Log.h" #include "Common/Logging/Log.h"
#include "Common/SocketContext.h"
#include "Core/HW/CPU.h" #include "Core/HW/CPU.h"
#include "Core/HW/Memmap.h" #include "Core/HW/Memmap.h"
#include "Core/Host.h" #include "Core/Host.h"
@ -31,6 +33,11 @@ typedef SSIZE_T ssize_t;
#include "Core/PowerPC/PPCCache.h" #include "Core/PowerPC/PPCCache.h"
#include "Core/PowerPC/PowerPC.h" #include "Core/PowerPC/PowerPC.h"
namespace
{
std::optional<Common::SocketContext> s_socket_context;
} // namespace
#define GDB_BFR_MAX 10000 #define GDB_BFR_MAX 10000
#define GDB_MAX_BP 10 #define GDB_MAX_BP 10
@ -791,10 +798,6 @@ void gdb_handle_exception()
} }
} }
#ifdef _WIN32
WSADATA InitData;
#endif
// exported functions // exported functions
static void gdb_init_generic(int domain, const sockaddr* server_addr, socklen_t server_addrlen, static void gdb_init_generic(int domain, const sockaddr* server_addr, socklen_t server_addrlen,
@ -833,10 +836,7 @@ void gdb_init(u32 port)
static void gdb_init_generic(int domain, const sockaddr* server_addr, socklen_t server_addrlen, static void gdb_init_generic(int domain, const sockaddr* server_addr, socklen_t server_addrlen,
sockaddr* client_addr, socklen_t* client_addrlen) sockaddr* client_addr, socklen_t* client_addrlen)
{ {
#ifdef _WIN32 s_socket_context.emplace();
WSAStartup(MAKEWORD(2, 2), &InitData);
#endif
memset(bp_x, 0, sizeof bp_x); memset(bp_x, 0, sizeof bp_x);
memset(bp_r, 0, sizeof bp_r); memset(bp_r, 0, sizeof bp_r);
memset(bp_w, 0, sizeof bp_w); memset(bp_w, 0, sizeof bp_w);
@ -884,9 +884,7 @@ void gdb_deinit()
sock = -1; sock = -1;
} }
#ifdef _WIN32 s_socket_context.reset();
WSACleanup();
#endif
} }
bool gdb_active() bool gdb_active()

View File

@ -142,6 +142,7 @@
<ClInclude Include="Common\Semaphore.h" /> <ClInclude Include="Common\Semaphore.h" />
<ClInclude Include="Common\SettingsHandler.h" /> <ClInclude Include="Common\SettingsHandler.h" />
<ClInclude Include="Common\SFMLHelper.h" /> <ClInclude Include="Common\SFMLHelper.h" />
<ClInclude Include="Common\SocketContext.h" />
<ClInclude Include="Common\SPSCQueue.h" /> <ClInclude Include="Common\SPSCQueue.h" />
<ClInclude Include="Common\StringUtil.h" /> <ClInclude Include="Common\StringUtil.h" />
<ClInclude Include="Common\Swap.h" /> <ClInclude Include="Common\Swap.h" />
@ -718,6 +719,7 @@
<ClCompile Include="Common\SDCardUtil.cpp" /> <ClCompile Include="Common\SDCardUtil.cpp" />
<ClCompile Include="Common\SettingsHandler.cpp" /> <ClCompile Include="Common\SettingsHandler.cpp" />
<ClCompile Include="Common\SFMLHelper.cpp" /> <ClCompile Include="Common\SFMLHelper.cpp" />
<ClCompile Include="Common\SocketContext.cpp" />
<ClCompile Include="Common\StringUtil.cpp" /> <ClCompile Include="Common\StringUtil.cpp" />
<ClCompile Include="Common\SymbolDB.cpp" /> <ClCompile Include="Common\SymbolDB.cpp" />
<ClCompile Include="Common\Thread.cpp" /> <ClCompile Include="Common\Thread.cpp" />