2021-07-16 08:31:17 +01:00
|
|
|
/*
|
2021-10-04 20:59:10 +01:00
|
|
|
* Copyright (c) Atmosphère-NX
|
2021-07-16 08:31:17 +01:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms and conditions of the GNU General Public License,
|
|
|
|
* version 2, as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
* more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
#include <stratosphere.hpp>
|
|
|
|
#include "dmnt2_debug_log.hpp"
|
2021-10-31 22:47:08 +00:00
|
|
|
#include "dmnt2_transport_layer.hpp"
|
2021-07-17 14:27:40 +01:00
|
|
|
#include "dmnt2_gdb_server.hpp"
|
|
|
|
#include "dmnt2_gdb_server_impl.hpp"
|
2021-07-16 08:31:17 +01:00
|
|
|
|
|
|
|
namespace ams::dmnt {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2021-07-16 10:40:08 +01:00
|
|
|
constexpr size_t ServerThreadStackSize = util::AlignUp(4 * GdbPacketBufferSize + os::MemoryPageSize, os::ThreadStackAlignment);
|
2021-07-16 08:31:17 +01:00
|
|
|
|
2021-07-17 14:27:40 +01:00
|
|
|
alignas(os::ThreadStackAlignment) constinit u8 g_server_thread_stack[ServerThreadStackSize];
|
2021-07-20 09:29:00 +01:00
|
|
|
alignas(os::ThreadStackAlignment) constinit u8 g_events_thread_stack[util::AlignUp(2 * GdbPacketBufferSize + os::MemoryPageSize, os::ThreadStackAlignment)];
|
2021-07-16 08:31:17 +01:00
|
|
|
|
2021-07-17 14:27:40 +01:00
|
|
|
constinit os::ThreadType g_server_thread;
|
2021-07-16 10:40:08 +01:00
|
|
|
|
2021-07-17 14:27:40 +01:00
|
|
|
constinit util::TypedStorage<GdbServerImpl> g_gdb_server;
|
2021-07-16 08:31:17 +01:00
|
|
|
|
2021-07-17 14:27:40 +01:00
|
|
|
void GdbServerThreadFunction(void *) {
|
2021-07-16 08:31:17 +01:00
|
|
|
/* Loop forever, servicing our gdb server. */
|
|
|
|
while (true) {
|
|
|
|
/* Get a socket. */
|
|
|
|
int fd;
|
2021-10-31 22:47:08 +00:00
|
|
|
while ((fd = transport::Socket()) == -1) {
|
2021-07-16 08:31:17 +01:00
|
|
|
os::SleepThread(TimeSpan::FromSeconds(1));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Ensure we cleanup the socket when we're done with it. */
|
|
|
|
ON_SCOPE_EXIT {
|
2021-10-31 22:47:08 +00:00
|
|
|
transport::Close(fd);
|
2021-07-16 08:31:17 +01:00
|
|
|
os::SleepThread(TimeSpan::FromSeconds(1));
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Bind. */
|
2021-10-31 22:47:08 +00:00
|
|
|
if (transport::Bind(fd, transport::PortName_GdbServer) == -1) {
|
2021-07-16 08:31:17 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Listen on our port. */
|
2021-10-31 22:47:08 +00:00
|
|
|
while (transport::Listen(fd, 0) == 0) {
|
2021-07-16 08:31:17 +01:00
|
|
|
/* Continue accepting clients, so long as we can. */
|
|
|
|
int client_fd;
|
|
|
|
while (true) {
|
|
|
|
/* Try to accept a client. */
|
2021-10-31 22:47:08 +00:00
|
|
|
if (client_fd = transport::Accept(fd); client_fd < 0) {
|
2021-07-16 08:31:17 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-07-17 14:27:40 +01:00
|
|
|
{
|
|
|
|
/* Create gdb server for the socket. */
|
2021-07-19 07:22:44 +01:00
|
|
|
util::ConstructAt(g_gdb_server, client_fd, g_events_thread_stack, sizeof(g_events_thread_stack));
|
2021-07-17 14:27:40 +01:00
|
|
|
ON_SCOPE_EXIT { util::DestroyAt(g_gdb_server); };
|
2021-07-16 10:40:08 +01:00
|
|
|
|
2021-07-17 14:27:40 +01:00
|
|
|
/* Process for the server. */
|
|
|
|
util::GetReference(g_gdb_server).LoopProcess();
|
|
|
|
}
|
2021-07-16 08:31:17 +01:00
|
|
|
|
|
|
|
/* Close the client socket. */
|
2021-10-31 22:47:08 +00:00
|
|
|
transport::Close(client_fd);
|
2021-07-16 08:31:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void InitializeGdbServer() {
|
2021-07-16 10:40:08 +01:00
|
|
|
/* Create and start gdb server threads. */
|
2021-07-17 14:27:40 +01:00
|
|
|
R_ABORT_UNLESS(os::CreateThread(std::addressof(g_server_thread), GdbServerThreadFunction, nullptr, g_server_thread_stack, sizeof(g_server_thread_stack), os::HighestThreadPriority - 1));
|
|
|
|
os::StartThread(std::addressof(g_server_thread));
|
2021-07-16 08:31:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|