Skip to content

Commit

Permalink
0000xf0
Browse files Browse the repository at this point in the history
Signed-off-by: Simo <[email protected]>
  • Loading branch information
Simo3ds authored Nov 18, 2023
1 parent 590fc01 commit f24efa7
Showing 1 changed file with 175 additions and 73 deletions.
248 changes: 175 additions & 73 deletions sysmodules/rosalina/source/menus/debugger_menu.c
Original file line number Diff line number Diff line change
@@ -1,51 +1,121 @@
/*
* This file is part of Luma3DS
* Copyright (C) 2016-2020 Aurora Wright, TuxSH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that 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/>.
*
* Additional Terms 7.b and 7.c of GPLv3 apply to this file:
* * Requiring preservation of specified reasonable legal notices or
* author attributions in that material or in the Appropriate Legal
* Notices displayed by works containing it.
* * Prohibiting misrepresentation of the origin of that material,
* or requiring that modified versions of such material be marked in
* reasonable ways as different from the original version.
*/

#include <stdio.h>
#include "menus/debugger_menu.h"
#include "debugger.h"
* This file is part of Luma3DS
* Copyright (C) 2016-2020 Aurora Wright, TuxSH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that 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/>.
*
* Additional Terms 7.b and 7.c of GPLv3 apply to this file:
* * Requiring preservation of specified reasonable legal notices or
* author attributions in that material or in the Appropriate Legal
* Notices displayed by works containing it.
* * Prohibiting misrepresentation of the origin of that material,
* or requiring that modified versions of such material be marked in
* reasonable ways as different from the original version.
*/

#include "menus/debugger.h"
#include "memory.h"
#include "draw.h"
#include "menu.h"
#include "menus.h"
#include "minisoc.h"
#include "fmt.h"
#include "pmdbgext.h"
#include "gdb/server.h"
#include "gdb/debug.h"
#include "gdb/monitor.h"
#include "gdb/net.h"
#include "pmdbgext.h"

Menu debuggerMenu = {
"Menu del Debugger",
"Debugger options menu",
{
{"Abilita il debugger", METHOD, .method = &DebuggerMenu_EnableDebugger},
{"Disabilita il debugger", METHOD, .method = &DebuggerMenu_DisableDebugger},
{"Forza il debug all'avvio della prossima applicazione", METHOD, .method = &DebuggerMenu_DebugNextApplicationByForce},
{ "Enable debugger", METHOD, .method = &DebuggerMenu_EnableDebugger },
{ "Disable debugger", METHOD, .method = &DebuggerMenu_DisableDebugger },
{ "Force-debug next application at launch", METHOD, .method = &DebuggerMenu_DebugNextApplicationByForce },
{},
}};
}
};

static MyThread debuggerSocketThread;
static MyThread debuggerDebugThread;
static u8 CTR_ALIGN(8) debuggerSocketThreadStack[0x5000];
static u8 CTR_ALIGN(8) debuggerDebugThreadStack[0x3000];

GDBServer gdbServer = { 0 };

GDBContext *nextApplicationGdbCtx = NULL;

void debuggerSocketThreadMain(void);
MyThread *debuggerCreateSocketThread(void)
{
MyThread_Create(&debuggerSocketThread, debuggerSocketThreadMain, debuggerSocketThreadStack, 0x5000, 0x20, CORE_SYSTEM);
return &debuggerSocketThread;
}

void debuggerDebugThreadMain(void);
MyThread *debuggerCreateDebugThread(void)
{
MyThread_Create(&debuggerDebugThread, debuggerDebugThreadMain, debuggerDebugThreadStack, 0x3000, 0x20, CORE_SYSTEM);
return &debuggerDebugThread;
}

void debuggerFetchAndSetNextApplicationDebugHandleTask(void *argdata)
{
(void)argdata;
if(!nextApplicationGdbCtx)
return;
Handle debug = 0;
PMDBG_RunQueuedProcess(&debug);
GDB_LockAllContexts(&gdbServer);
nextApplicationGdbCtx->debug = debug;
if (debug == 0)
nextApplicationGdbCtx->flags = 0;
else
nextApplicationGdbCtx->flags |= GDB_FLAG_ATTACHED_AT_START;
nextApplicationGdbCtx = NULL;
GDB_UnlockAllContexts(&gdbServer);
}

Result debuggerDisable(s64 timeout)
{
Result res = 0;
bool initialized = gdbServer.referenceCount != 0;
if(initialized)
{
svcSignalEvent(gdbServer.super.shall_terminate_event);
server_kill_connections(&gdbServer.super);

res = MyThread_Join(&debuggerDebugThread, timeout);
if(res == 0)
res = MyThread_Join(&debuggerSocketThread, timeout);

Handle dummy = 0;
PMDBG_RunQueuedProcess(&dummy);
svcCloseHandle(dummy);
PMDBG_DebugNextApplicationByForce(false);
nextApplicationGdbCtx = NULL;
}

return res;
}

void DebuggerMenu_EnableDebugger(void)
{
bool done = false, alreadyEnabled = gdbServer.super.running;
Result res = 0;
char buf[65];
bool isSocURegistered;

res = srvIsServiceRegistered(&isSocURegistered, "soc:U");
isSocURegistered = R_SUCCEEDED(res) && isSocURegistered;

Expand All @@ -57,32 +127,43 @@ void DebuggerMenu_EnableDebugger(void)
do
{
Draw_Lock();
Draw_DrawString(10, 10, COLOR_TITLE, "Menu del Debugger");
Draw_DrawString(10, 10, COLOR_TITLE, "Debugger options menu");

if (alreadyEnabled)
Draw_DrawString(10, 30, COLOR_WHITE, "Gia' attivato!");
else if (!isSocURegistered)
Draw_DrawString(10, 30, COLOR_WHITE, "Impossibile avviare il debugger prima che il sistema\nha finito di caricarsi.");
if(alreadyEnabled)
Draw_DrawString(10, 30, COLOR_WHITE, "Already enabled!");
else if(!isSocURegistered)
Draw_DrawString(10, 30, COLOR_WHITE, "Can't start the debugger before the system has fi-\nnished loading.");
else
{
Draw_DrawString(10, 30, COLOR_WHITE, "Inizializzando il debugger...");
Draw_DrawString(10, 30, COLOR_WHITE, "Starting debugger...");

if (!done)
if(!done)
{
res = debuggerEnable(5 * 1000 * 1000 * 1000LL);
if (res != 0)
sprintf(buf, "Inizializzando il debugger... Fallito (0x%08lx).", (u32)res);
res = GDB_InitializeServer(&gdbServer);
Handle handles[3] = { gdbServer.super.started_event, gdbServer.super.shall_terminate_event, preTerminationEvent };
s32 idx;
if(R_SUCCEEDED(res))
{
debuggerCreateSocketThread();
debuggerCreateDebugThread();
res = svcWaitSynchronizationN(&idx, handles, 3, false, 5 * 1000 * 1000 * 1000LL);
if(res == 0) res = gdbServer.super.init_result;
}

if(res != 0)
sprintf(buf, "Starting debugger... failed (0x%08lx).", (u32)res);
done = true;
}
if (res == 0)
Draw_DrawString(10, 30, COLOR_WHITE, "Inizializzando il debugger... OK.");
if(res == 0)
Draw_DrawString(10, 30, COLOR_WHITE, "Starting debugger... OK.");
else
Draw_DrawString(10, 30, COLOR_WHITE, buf);
}

Draw_FlushFramebuffer();
Draw_Unlock();
} while (!(waitInput() & KEY_B) && !menuShouldExit);
}
while(!(waitInput() & KEY_B) && !menuShouldExit);
}

void DebuggerMenu_DisableDebugger(void)
Expand All @@ -92,17 +173,18 @@ void DebuggerMenu_DisableDebugger(void)
Result res = initialized ? debuggerDisable(2 * 1000 * 1000 * 1000LL) : 0;
char buf[65];

if (res != 0)
sprintf( "Debugger qualcosa");
if(res != 0)
sprintf(buf, "Failed to disable debugger (0x%08lx).", (u32)res);

do
{
Draw_Lock();
Draw_DrawString(10, 10, COLOR_TITLE, "Menu del Debugger");
Draw_DrawString(10, 30, COLOR_WHITE, initialized ? (res == 0 ? "Debugger disabilitato con successo." : buf) : "Debugger non abilitato.");
Draw_DrawString(10, 10, COLOR_TITLE, "Debugger options menu");
Draw_DrawString(10, 30, COLOR_WHITE, initialized ? (res == 0 ? "Debugger disabled successfully." : buf) : "Debugger not enabled.");
Draw_FlushFramebuffer();
Draw_Unlock();
} while (!(waitInput() & KEY_B) && !menuShouldExit);
}
while(!(waitInput() & KEY_B) && !menuShouldExit);
}

void DebuggerMenu_DebugNextApplicationByForce(void)
Expand All @@ -111,39 +193,59 @@ void DebuggerMenu_DebugNextApplicationByForce(void)
Result res = 0;
char buf[256];

if (initialized)
if(initialized)
{
GDB_LockAllContexts(&gdbServer);
res = debugNextApplicationByForce();
GDB_UnlockAllContexts(&gdbServer);
switch (res)

if (nextApplicationGdbCtx != NULL)
strcpy(buf, "Operation already performed.");
else
{
case 0:
strcpy(buf, "Operazione gia' performata.");
break;
case 1:
sprintf(buf, "Operazione eseguita con successo.\nUsa la porta %d per connettere la prossima applicazione\nlanciata.", nextApplicationGdbCtx->localPort);
break;
case 2:
strcpy(buf, "Fallimento nell'allocazione di uno slot.\nPerfavore prima deselezziona un processo nella lista processi.");
break;
default:
if (!R_SUCCEEDED(res))
nextApplicationGdbCtx = GDB_SelectAvailableContext(&gdbServer, GDB_PORT_BASE + 3, GDB_PORT_BASE + 4);
if (nextApplicationGdbCtx != NULL)
{
sprintf(buf, "Operazione fallita (0x%08lx).", (u32)res);
nextApplicationGdbCtx->debug = 0;
nextApplicationGdbCtx->pid = 0xFFFFFFFF;
res = PMDBG_DebugNextApplicationByForce(true);
if(R_SUCCEEDED(res))
sprintf(buf, "Operation succeeded.\nUse port %d to connect to the next launched\napplication.", nextApplicationGdbCtx->localPort);
else
{
nextApplicationGdbCtx->flags = 0;
nextApplicationGdbCtx->localPort = 0;
nextApplicationGdbCtx = NULL;
sprintf(buf, "Operation failed (0x%08lx).", (u32)res);
}
}
break;
else
strcpy(buf, "Failed to allocate a slot.\nPlease unselect a process in the process list first");
}
GDB_UnlockAllContexts(&gdbServer);
}
else
strcpy(buf, "Debugger non abilitato.");
strcpy(buf, "Debugger not enabled.");

do
{
Draw_Lock();
Draw_DrawString(10, 10, COLOR_TITLE, "Menu del Debugger");
Draw_DrawString(10, 10, COLOR_TITLE, "Debugger options menu");
Draw_DrawString(10, 30, COLOR_WHITE, buf);
Draw_FlushFramebuffer();
Draw_Unlock();
} while (!(waitInput() & KEY_B) && !menuShouldExit);
}
while(!(waitInput() & KEY_B) && !menuShouldExit);
}

void debuggerSocketThreadMain(void)
{
GDB_IncrementServerReferenceCount(&gdbServer);
GDB_RunServer(&gdbServer);
GDB_DecrementServerReferenceCount(&gdbServer);
}

void debuggerDebugThreadMain(void)
{
GDB_IncrementServerReferenceCount(&gdbServer);
GDB_RunMonitor(&gdbServer);
GDB_DecrementServerReferenceCount(&gdbServer);
}

0 comments on commit f24efa7

Please sign in to comment.