dmnt: begin working on packet parser

This commit is contained in:
Michael Scire 2021-07-16 02:40:08 -07:00 committed by SciresM
parent f85df27875
commit a7f9729f63
3 changed files with 501 additions and 19 deletions

View file

@ -15,6 +15,7 @@
*/
#include <stratosphere.hpp>
#include "dmnt2_gdb_packet_io.hpp"
#include "dmnt2_gdb_packet_parser.hpp"
#include "dmnt2_gdb_server.hpp"
#include "dmnt2_debug_log.hpp"
@ -22,20 +23,19 @@ namespace ams::dmnt {
namespace {
constexpr size_t ServerThreadStackSize = util::AlignUp(3 * GdbPacketBufferSize + os::MemoryPageSize, os::ThreadStackAlignment);
constexpr size_t ServerThreadStackSize = util::AlignUp(4 * GdbPacketBufferSize + os::MemoryPageSize, os::ThreadStackAlignment);
alignas(os::ThreadStackAlignment) constinit u8 g_server_thread_stack[ServerThreadStackSize];
constinit os::ThreadType g_server_thread;
alignas(os::ThreadStackAlignment) constinit u8 g_server_thread32_stack[ServerThreadStackSize];
alignas(os::ThreadStackAlignment) constinit u8 g_server_thread64_stack[ServerThreadStackSize];
constinit util::TypedStorage<HtcsSession> g_session;
constinit os::ThreadType g_server_thread32;
constinit os::ThreadType g_server_thread64;
void OnClientSocketAccepted(int fd) {
/* Create htcs session for the socket. */
util::ConstructAt(g_session, fd);
ON_SCOPE_EXIT { util::DestroyAt(g_session); };
constinit util::TypedStorage<HtcsSession> g_session32;
constinit util::TypedStorage<HtcsSession> g_session64;
HtcsSession *session = util::GetPointer(g_session);
void ProcessForHtcsSession(HtcsSession *session, bool is_64_bit) {
/* Create packet io handler. */
GdbPacketIo packet_io;
@ -47,15 +47,21 @@ namespace ams::dmnt {
char *packet = packet_io.ReceivePacket(std::addressof(do_break), recv_buf, sizeof(recv_buf), session);
if (!do_break && packet != nullptr) {
AMS_DMNT2_GDB_LOG_DEBUG("Received Packet %s\n", packet);
/* Create a packet parser. */
char reply_buffer[GdbPacketBufferSize];
GdbPacketParser parser(packet, reply_buffer, is_64_bit);
/* TODO: Process packets. */
packet_io.SendPacket(std::addressof(do_break), "OK", session);
/* Process the packet. */
parser.Process();
/* Send packet. */
packet_io.SendPacket(std::addressof(do_break), reply_buffer, session);
}
}
}
void GdbServerThreadFunction(void *) {
template<bool Is64Bit>
void GdbServerThreadFunction() {
/* Loop forever, servicing our gdb server. */
while (true) {
/* Get a socket. */
@ -74,7 +80,7 @@ namespace ams::dmnt {
htcs::SockAddrHtcs addr;
addr.family = htcs::HTCS_AF_HTCS;
addr.peer_name = htcs::GetPeerNameAny();
std::strcpy(addr.port_name.name, "iywys@$gdb");
std::strcpy(addr.port_name.name, Is64Bit ? "iywys@$gdb-aarch64" : "iywys@$gdb-aarch32");
/* Bind. */
if (htcs::Bind(fd, std::addressof(addr)) == -1) {
@ -91,8 +97,13 @@ namespace ams::dmnt {
break;
}
/* Handle the client. */
OnClientSocketAccepted(client_fd);
/* Create htcs session for the socket. */
auto &session_storage = Is64Bit ? g_session64 : g_session32;
util::ConstructAt(session_storage, client_fd);
ON_SCOPE_EXIT { util::DestroyAt(session_storage); };
/* Process for the session. */
ProcessForHtcsSession(util::GetPointer(session_storage), Is64Bit);
/* Close the client socket. */
htcs::Close(client_fd);
@ -101,14 +112,22 @@ namespace ams::dmnt {
}
}
void GdbServerThreadFunction64(void *) {
GdbServerThreadFunction<true>();
}
void GdbServerThreadFunction32(void *) {
GdbServerThreadFunction<false>();
}
}
void InitializeGdbServer() {
/* Create and start gdb server thread. */
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));
/* Create and start gdb server threads. */
R_ABORT_UNLESS(os::CreateThread(std::addressof(g_server_thread64), GdbServerThreadFunction64, nullptr, g_server_thread64_stack, sizeof(g_server_thread64_stack), os::HighestThreadPriority - 1));
R_ABORT_UNLESS(os::CreateThread(std::addressof(g_server_thread32), GdbServerThreadFunction32, nullptr, g_server_thread32_stack, sizeof(g_server_thread32_stack), os::HighestThreadPriority - 1));
os::StartThread(std::addressof(g_server_thread64));
os::StartThread(std::addressof(g_server_thread32));
}
}