From e0e86c46a71b6191d2a5a2db1cc4f1b5567b49bc Mon Sep 17 00:00:00 2001 From: PabloMK7 Date: Fri, 27 Sep 2024 16:32:07 +0200 Subject: [PATCH] rosalina: Add debug flag to dump gdb communications --- sysmodules/rosalina/include/gdb.h | 7 +++++ sysmodules/rosalina/include/ifile.h | 1 + sysmodules/rosalina/source/gdb.c | 8 ++++++ sysmodules/rosalina/source/gdb/net.c | 38 ++++++++++++++++++++++++++-- sysmodules/rosalina/source/ifile.c | 5 ++++ 5 files changed, 57 insertions(+), 2 deletions(-) diff --git a/sysmodules/rosalina/include/gdb.h b/sysmodules/rosalina/include/gdb.h index cbc7d7cf..2d699198 100644 --- a/sysmodules/rosalina/include/gdb.h +++ b/sysmodules/rosalina/include/gdb.h @@ -16,6 +16,9 @@ #include "memory.h" #include "ifile.h" +// Uncomment the line below to dump GDB communications to a file +//#define DEBUG_GDB_COMMUNICATIONS + #define MAX_DEBUG 3 #define MAX_DEBUG_THREAD 127 #define MAX_BREAKPOINT 64 @@ -154,6 +157,10 @@ typedef struct GDBContext char memoryOsInfoXmlData[0x800]; char processesOsInfoXmlData[0x1800]; + +#ifdef DEBUG_GDB_COMMUNICATIONS + IFile debugFile; +#endif } GDBContext; typedef int (*GDBCommandHandler)(GDBContext *ctx); diff --git a/sysmodules/rosalina/include/ifile.h b/sysmodules/rosalina/include/ifile.h index a164660f..a6721e9d 100644 --- a/sysmodules/rosalina/include/ifile.h +++ b/sysmodules/rosalina/include/ifile.h @@ -44,3 +44,4 @@ Result IFile_GetSize(IFile *file, u64 *size); Result IFile_SetSize(IFile *file, u64 size); Result IFile_Read(IFile *file, u64 *total, void *buffer, u32 len); Result IFile_Write(IFile *file, u64 *total, const void *buffer, u32 len, u32 flags); +Result IFile_Flush(IFile *file); diff --git a/sysmodules/rosalina/source/gdb.c b/sysmodules/rosalina/source/gdb.c index 7c10e113..6c0c3d93 100644 --- a/sysmodules/rosalina/source/gdb.c +++ b/sysmodules/rosalina/source/gdb.c @@ -28,6 +28,10 @@ void GDB_InitializeContext(GDBContext *ctx) ctx->eventToWaitFor = ctx->processAttachedEvent; ctx->continueFlags = (DebugFlags)(DBG_SIGNAL_FAULT_EXCEPTION_EVENTS | DBG_INHIBIT_USER_CPU_EXCEPTION_HANDLERS); +#ifdef DEBUG_GDB_COMMUNICATIONS + IFile_Open(&ctx->debugFile, ARCHIVE_SDMC, fsMakePath(PATH_EMPTY, ""), fsMakePath(PATH_ASCII, "/luma/gdb_debug.txt"), FS_OPEN_READ | FS_OPEN_WRITE | FS_OPEN_CREATE); +#endif + RecursiveLock_Unlock(&ctx->lock); } @@ -40,6 +44,10 @@ void GDB_FinalizeContext(GDBContext *ctx) svcCloseHandle(ctx->processAttachedEvent); svcCloseHandle(ctx->continuedEvent); +#ifdef DEBUG_GDB_COMMUNICATIONS + IFile_Close(&ctx->debugFile); +#endif + RecursiveLock_Unlock(&ctx->lock); } diff --git a/sysmodules/rosalina/source/gdb/net.c b/sysmodules/rosalina/source/gdb/net.c index 85ff4669..94cd5a41 100644 --- a/sysmodules/rosalina/source/gdb/net.c +++ b/sysmodules/rosalina/source/gdb/net.c @@ -171,6 +171,17 @@ const char *GDB_ParseHexIntegerList64(u64 *dst, const char *src, u32 nb, char la return GDB_ParseIntegerList64(dst, src, nb, ',', lastSep, 16, false); } +#ifdef DEBUG_GDB_COMMUNICATIONS +void GDB_LogPacket(GDBContext *ctx, void* buffer, u32 pLen, bool incoming) +{ + u64 written; + IFile_Write(&ctx->debugFile, &written, incoming ? "<-\t" : "->\t", 3, 0); + IFile_Write(&ctx->debugFile, &written, buffer, pLen, 0); + IFile_Write(&ctx->debugFile, &written, "\n", 1, 0); + IFile_Flush(&ctx->debugFile); +} +#endif + int GDB_ReceivePacket(GDBContext *ctx) { char backupbuf[GDB_BUF_LEN + 4]; @@ -189,6 +200,9 @@ int GDB_ReceivePacket(GDBContext *ctx) r = socRecv(ctx->super.sockfd, ctx->buffer, 1, 0); if(r != 1) return -1; +#ifdef DEBUG_GDB_COMMUNICATIONS + GDB_LogPacket(ctx, ctx->buffer, r, true); +#endif ctx->buffer[0] = 0; @@ -197,9 +211,19 @@ int GDB_ReceivePacket(GDBContext *ctx) if(r == -1) goto packet_error; } - else if(ctx->buffer[0] == '-') + +#ifdef DEBUG_GDB_COMMUNICATIONS + GDB_LogPacket(ctx, ctx->buffer, r, true); +#endif + + if(ctx->buffer[0] == '-') { socSend(ctx->super.sockfd, backupbuf, ctx->latestSentPacketSize, 0); + +#ifdef DEBUG_GDB_COMMUNICATIONS + GDB_LogPacket(ctx, backupbuf, ctx->latestSentPacketSize, false); +#endif + return 0; } int maxlen = r > (int)sizeof(ctx->buffer) ? (int)sizeof(ctx->buffer) : r; @@ -239,6 +263,9 @@ int GDB_ReceivePacket(GDBContext *ctx) int r2 = socSend(ctx->super.sockfd, "+", 1, 0); if(r2 != 1) return -1; +#ifdef DEBUG_GDB_COMMUNICATIONS + GDB_LogPacket(ctx, "+", 1, false); +#endif } if(ctx->noAckSent) @@ -255,8 +282,12 @@ packet_error: r = socSend(ctx->super.sockfd, "-", 1, 0); if(r != 1) return -1; - else + else { +#ifdef DEBUG_GDB_COMMUNICATIONS + GDB_LogPacket(ctx, "-", 1, false); +#endif return 0; + } } else return -1; @@ -265,6 +296,9 @@ packet_error: static int GDB_DoSendPacket(GDBContext *ctx, u32 len) { int r = socSend(ctx->super.sockfd, ctx->buffer, len, 0); +#ifdef DEBUG_GDB_COMMUNICATIONS + GDB_LogPacket(ctx, ctx->buffer, len, false); +#endif if(r > 0) ctx->latestSentPacketSize = r; diff --git a/sysmodules/rosalina/source/ifile.c b/sysmodules/rosalina/source/ifile.c index 94951d1e..7d3949cc 100644 --- a/sysmodules/rosalina/source/ifile.c +++ b/sysmodules/rosalina/source/ifile.c @@ -149,3 +149,8 @@ Result IFile_Write(IFile *file, u64 *total, const void *buffer, u32 len, u32 fla *total = cur; return res; } + +Result IFile_Flush(IFile *file) +{ + return FSFILE_Flush(file->handle); +} \ No newline at end of file