Skip to content

Commit

Permalink
rosalina: Add debug flag to dump gdb communications
Browse files Browse the repository at this point in the history
  • Loading branch information
PabloMK7 authored and TuxSH committed Sep 27, 2024
1 parent 902f306 commit e0e86c4
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 2 deletions.
7 changes: 7 additions & 0 deletions sysmodules/rosalina/include/gdb.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions sysmodules/rosalina/include/ifile.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
8 changes: 8 additions & 0 deletions sysmodules/rosalina/source/gdb.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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);
}

Expand Down
38 changes: 36 additions & 2 deletions sysmodules/rosalina/source/gdb/net.c
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -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;

Expand All @@ -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;
Expand Down Expand Up @@ -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)
Expand All @@ -255,8 +282,12 @@ int GDB_ReceivePacket(GDBContext *ctx)
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;
Expand All @@ -265,6 +296,9 @@ int GDB_ReceivePacket(GDBContext *ctx)
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;
Expand Down
5 changes: 5 additions & 0 deletions sysmodules/rosalina/source/ifile.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

0 comments on commit e0e86c4

Please sign in to comment.