Add support to force-debug applications before they start running code

This commit is contained in:
TuxSH
2019-03-31 20:01:16 +02:00
parent 219f38169f
commit fb800bd4c9
8 changed files with 138 additions and 21 deletions

View File

@@ -29,6 +29,7 @@
#include "draw.h"
#include "minisoc.h"
#include "fmt.h"
#include "pmdbgext.h"
#include "gdb/server.h"
#include "gdb/debug.h"
#include "gdb/monitor.h"
@@ -36,10 +37,11 @@
Menu debuggerMenu = {
"Debugger options menu",
.nbItems = 2,
.nbItems = 3,
{
{ "Enable debugger", METHOD, .method = &DebuggerMenu_EnableDebugger },
{ "Disable debugger", METHOD, .method = &DebuggerMenu_DisableDebugger }
{ "Enable debugger", METHOD, .method = &DebuggerMenu_EnableDebugger },
{ "Disable debugger", METHOD, .method = &DebuggerMenu_DisableDebugger },
{ "Force-debug next application at launch", METHOD, .method = &DebuggerMenu_DebugNextApplicationByForce },
}
};
@@ -50,6 +52,8 @@ static u8 ALIGN(8) debuggerDebugThreadStack[0x2000];
GDBServer gdbServer = { 0 };
static GDBContext *nextApplicationGdbCtx = NULL;
void debuggerSocketThreadMain(void);
MyThread *debuggerCreateSocketThread(void)
{
@@ -64,6 +68,18 @@ MyThread *debuggerCreateDebugThread(void)
return &debuggerDebugThread;
}
void debuggerSetNextApplicationDebugHandle(Handle debug)
{
GDB_LockAllContexts(&gdbServer);
nextApplicationGdbCtx->debug = debug;
if (debug == 0)
nextApplicationGdbCtx->flags = (GDBFlags)0;
else
nextApplicationGdbCtx->flags |= GDB_FLAG_ATTACHED_AT_START;
nextApplicationGdbCtx = NULL;
GDB_UnlockAllContexts(&gdbServer);
}
void DebuggerMenu_EnableDebugger(void)
{
bool done = false, alreadyEnabled = gdbServer.super.running;
@@ -148,6 +164,47 @@ void DebuggerMenu_DisableDebugger(void)
while(!(waitInput() & BUTTON_B) && !terminationRequest);
}
void DebuggerMenu_DebugNextApplicationByForce(void)
{
bool initialized = gdbServer.referenceCount != 0;
Result res = 0;
char buf[256];
if(initialized)
{
res = PMDBG_DebugNextApplicationByForce();
if(R_SUCCEEDED(res))
{
GDB_LockAllContexts(&gdbServer);
if (nextApplicationGdbCtx == NULL)
nextApplicationGdbCtx = GDB_SelectAvailableContext(&gdbServer, GDB_PORT_BASE + 3, GDB_PORT_BASE + 4);
if (nextApplicationGdbCtx != NULL)
{
nextApplicationGdbCtx->debug = 0;
nextApplicationGdbCtx->pid = 0xFFFFFFFF;
sprintf(buf, "Operation succeeded.\nUse port %d to connect to the next launched\napplication.", nextApplicationGdbCtx->localPort);
}
else
strcpy(buf, "Failed to allocate a slot.\nPlease unselect a process in the process list first");
GDB_UnlockAllContexts(&gdbServer);
}
else
sprintf(buf, "Operation failed (0x%08lx).", (u32)res);
}
else
strcpy(buf, "Debugger not enabled.");
do
{
Draw_Lock();
Draw_DrawString(10, 10, COLOR_TITLE, "Debugger options menu");
Draw_DrawString(10, 30, COLOR_WHITE, buf);
Draw_FlushFramebuffer();
Draw_Unlock();
}
while(!(waitInput() & BUTTON_B) && !terminationRequest);
}
void debuggerSocketThreadMain(void)
{
GDB_IncrementServerReferenceCount(&gdbServer);

View File

@@ -77,7 +77,7 @@ static inline int ProcessListMenu_FormatInfoLine(char *out, const ProcessInfo *i
checkbox = "(A) ";
sprintf(commentBuf, "Remote: %hhu.%hhu.%hhu.%hhu", addr[0], addr[1], addr[2], addr[3]);
}
else
else if (gdbServer.ctxs[id].localPort >= GDB_PORT_BASE && gdbServer.ctxs[id].localPort < GDB_PORT_BASE + MAX_DEBUG)
{
checkbox = "(W) ";
sprintf(commentBuf, "Port: %hu", gdbServer.ctxs[id].localPort);
@@ -606,7 +606,7 @@ static inline void ProcessListMenu_HandleSelected(const ProcessInfo *info)
while(ctx->super.should_close)
svcSleepThread(12 * 1000 * 1000LL);
}
else
else if (gdbServer.ctxs[id].localPort >= GDB_PORT_BASE && gdbServer.ctxs[id].localPort < GDB_PORT_BASE + MAX_DEBUG)
{
RecursiveLock_Lock(&ctx->lock);
ctx->flags &= ~GDB_FLAG_SELECTED;