2017-06-05 02:02:04 +02:00
|
|
|
/*
|
|
|
|
* This file is part of Luma3DS
|
2020-04-25 14:26:21 +02:00
|
|
|
* Copyright (C) 2016-2020 Aurora Wright, TuxSH
|
2017-06-05 02:02:04 +02:00
|
|
|
*
|
|
|
|
* 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 "minisoc.h"
|
|
|
|
#include "fmt.h"
|
2019-03-31 20:01:16 +02:00
|
|
|
#include "pmdbgext.h"
|
2017-06-05 02:02:04 +02:00
|
|
|
#include "gdb/server.h"
|
|
|
|
#include "gdb/debug.h"
|
|
|
|
#include "gdb/monitor.h"
|
|
|
|
#include "gdb/net.h"
|
2019-04-18 22:58:53 +02:00
|
|
|
#include "pmdbgext.h"
|
2017-06-05 02:02:04 +02:00
|
|
|
|
|
|
|
Menu debuggerMenu = {
|
|
|
|
"Debugger options menu",
|
2019-03-31 20:01:16 +02:00
|
|
|
.nbItems = 3,
|
2017-06-05 02:02:04 +02:00
|
|
|
{
|
2019-03-31 20:01:16 +02:00
|
|
|
{ "Enable debugger", METHOD, .method = &DebuggerMenu_EnableDebugger },
|
|
|
|
{ "Disable debugger", METHOD, .method = &DebuggerMenu_DisableDebugger },
|
|
|
|
{ "Force-debug next application at launch", METHOD, .method = &DebuggerMenu_DebugNextApplicationByForce },
|
2017-06-05 02:02:04 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static MyThread debuggerSocketThread;
|
|
|
|
static MyThread debuggerDebugThread;
|
2019-04-28 02:14:31 +02:00
|
|
|
static u8 ALIGN(8) debuggerSocketThreadStack[0x5000];
|
|
|
|
static u8 ALIGN(8) debuggerDebugThreadStack[0x3000];
|
2017-06-05 02:02:04 +02:00
|
|
|
|
|
|
|
GDBServer gdbServer = { 0 };
|
|
|
|
|
2019-04-18 22:28:23 +02:00
|
|
|
GDBContext *nextApplicationGdbCtx = NULL;
|
2019-03-31 20:01:16 +02:00
|
|
|
|
2017-06-05 02:02:04 +02:00
|
|
|
void debuggerSocketThreadMain(void);
|
|
|
|
MyThread *debuggerCreateSocketThread(void)
|
|
|
|
{
|
2019-04-28 02:14:31 +02:00
|
|
|
MyThread_Create(&debuggerSocketThread, debuggerSocketThreadMain, debuggerSocketThreadStack, 0x5000, 0x20, CORE_SYSTEM);
|
2017-06-05 02:02:04 +02:00
|
|
|
return &debuggerSocketThread;
|
|
|
|
}
|
|
|
|
|
|
|
|
void debuggerDebugThreadMain(void);
|
|
|
|
MyThread *debuggerCreateDebugThread(void)
|
|
|
|
{
|
2019-04-28 02:14:31 +02:00
|
|
|
MyThread_Create(&debuggerDebugThread, debuggerDebugThreadMain, debuggerDebugThreadStack, 0x3000, 0x20, CORE_SYSTEM);
|
2017-06-05 02:02:04 +02:00
|
|
|
return &debuggerDebugThread;
|
|
|
|
}
|
|
|
|
|
2019-04-18 22:58:53 +02:00
|
|
|
void debuggerFetchAndSetNextApplicationDebugHandleTask(void *argdata)
|
2019-03-31 20:01:16 +02:00
|
|
|
{
|
2019-04-18 22:58:53 +02:00
|
|
|
(void)argdata;
|
|
|
|
Handle debug = 0;
|
|
|
|
PMDBG_RunQueuedProcess(&debug);
|
2019-03-31 20:01:16 +02:00
|
|
|
GDB_LockAllContexts(&gdbServer);
|
|
|
|
nextApplicationGdbCtx->debug = debug;
|
|
|
|
if (debug == 0)
|
2019-04-10 23:38:10 +02:00
|
|
|
nextApplicationGdbCtx->flags = 0;
|
2019-03-31 20:01:16 +02:00
|
|
|
else
|
|
|
|
nextApplicationGdbCtx->flags |= GDB_FLAG_ATTACHED_AT_START;
|
|
|
|
nextApplicationGdbCtx = NULL;
|
|
|
|
GDB_UnlockAllContexts(&gdbServer);
|
|
|
|
}
|
|
|
|
|
2017-06-05 02:02:04 +02:00
|
|
|
void DebuggerMenu_EnableDebugger(void)
|
|
|
|
{
|
|
|
|
bool done = false, alreadyEnabled = gdbServer.super.running;
|
|
|
|
Result res = 0;
|
|
|
|
char buf[65];
|
2018-01-15 02:52:50 +01:00
|
|
|
bool isSocURegistered;
|
2017-06-05 02:02:04 +02:00
|
|
|
|
2018-01-15 02:52:50 +01:00
|
|
|
res = srvIsServiceRegistered(&isSocURegistered, "soc:U");
|
|
|
|
isSocURegistered = R_SUCCEEDED(res) && isSocURegistered;
|
2017-06-05 02:02:04 +02:00
|
|
|
|
|
|
|
Draw_Lock();
|
|
|
|
Draw_ClearFramebuffer();
|
|
|
|
Draw_FlushFramebuffer();
|
|
|
|
Draw_Unlock();
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
Draw_Lock();
|
|
|
|
Draw_DrawString(10, 10, COLOR_TITLE, "Debugger options menu");
|
|
|
|
|
|
|
|
if(alreadyEnabled)
|
|
|
|
Draw_DrawString(10, 30, COLOR_WHITE, "Already enabled!");
|
2018-01-15 02:52:50 +01:00
|
|
|
else if(!isSocURegistered)
|
2017-06-05 02:02:04 +02:00
|
|
|
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, "Starting debugger...");
|
|
|
|
|
|
|
|
if(!done)
|
|
|
|
{
|
|
|
|
res = GDB_InitializeServer(&gdbServer);
|
|
|
|
if(R_SUCCEEDED(res))
|
|
|
|
{
|
|
|
|
debuggerCreateSocketThread();
|
|
|
|
debuggerCreateDebugThread();
|
|
|
|
res = svcWaitSynchronization(gdbServer.super.started_event, 10 * 1000 * 1000 * 1000LL);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(res != 0)
|
2018-05-24 00:55:38 +02:00
|
|
|
sprintf(buf, "Starting debugger... failed (0x%08lx).", (u32)res);
|
2017-06-05 02:02:04 +02:00
|
|
|
|
|
|
|
done = true;
|
|
|
|
}
|
|
|
|
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() & BUTTON_B) && !terminationRequest);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DebuggerMenu_DisableDebugger(void)
|
|
|
|
{
|
|
|
|
bool initialized = gdbServer.referenceCount != 0;
|
|
|
|
Result res = 0;
|
|
|
|
char buf[65];
|
|
|
|
|
|
|
|
if(initialized)
|
|
|
|
{
|
|
|
|
svcSignalEvent(gdbServer.super.shall_terminate_event);
|
2019-06-16 22:06:05 +02:00
|
|
|
server_kill_connections(&gdbServer.super);
|
|
|
|
//server_set_should_close_all(&gdbServer.super);
|
2019-06-14 01:32:50 +02:00
|
|
|
|
|
|
|
res = MyThread_Join(&debuggerDebugThread, 2 * 1000 * 1000 * 1000LL);
|
2017-06-05 02:02:04 +02:00
|
|
|
if(res == 0)
|
2019-06-14 01:32:50 +02:00
|
|
|
res = MyThread_Join(&debuggerSocketThread, 2 * 1000 * 1000 * 1000LL);
|
|
|
|
|
|
|
|
Handle dummy = 0;
|
|
|
|
PMDBG_RunQueuedProcess(&dummy);
|
|
|
|
svcCloseHandle(dummy);
|
|
|
|
PMDBG_DebugNextApplicationByForce(false);
|
|
|
|
nextApplicationGdbCtx = NULL;
|
2017-06-05 02:02:04 +02:00
|
|
|
svcKernelSetState(0x10000, 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(res != 0)
|
2018-05-24 00:55:38 +02:00
|
|
|
sprintf(buf, "Failed to disable debugger (0x%08lx).", (u32)res);
|
2017-06-05 02:02:04 +02:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
Draw_Lock();
|
|
|
|
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() & BUTTON_B) && !terminationRequest);
|
|
|
|
}
|
|
|
|
|
2019-03-31 20:01:16 +02:00
|
|
|
void DebuggerMenu_DebugNextApplicationByForce(void)
|
|
|
|
{
|
|
|
|
bool initialized = gdbServer.referenceCount != 0;
|
|
|
|
Result res = 0;
|
|
|
|
char buf[256];
|
|
|
|
|
2019-04-19 00:37:20 +02:00
|
|
|
if(initialized)
|
2019-03-31 20:01:16 +02:00
|
|
|
{
|
2019-04-19 00:37:20 +02:00
|
|
|
GDB_LockAllContexts(&gdbServer);
|
|
|
|
|
2019-04-18 22:28:23 +02:00
|
|
|
if (nextApplicationGdbCtx != NULL)
|
2019-04-19 00:37:20 +02:00
|
|
|
strcpy(buf, "Operation already performed.");
|
|
|
|
else
|
2019-03-31 20:01:16 +02:00
|
|
|
{
|
2019-04-19 00:37:20 +02:00
|
|
|
nextApplicationGdbCtx = GDB_SelectAvailableContext(&gdbServer, GDB_PORT_BASE + 3, GDB_PORT_BASE + 4);
|
|
|
|
if (nextApplicationGdbCtx != NULL)
|
2019-04-18 22:28:23 +02:00
|
|
|
{
|
2019-04-19 00:37:20 +02:00
|
|
|
nextApplicationGdbCtx->debug = 0;
|
|
|
|
nextApplicationGdbCtx->pid = 0xFFFFFFFF;
|
2019-06-14 01:32:50 +02:00
|
|
|
res = PMDBG_DebugNextApplicationByForce(true);
|
2019-04-19 00:37:20 +02:00
|
|
|
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);
|
|
|
|
}
|
2019-04-18 22:28:23 +02:00
|
|
|
}
|
2019-04-19 00:37:20 +02:00
|
|
|
else
|
|
|
|
strcpy(buf, "Failed to allocate a slot.\nPlease unselect a process in the process list first");
|
2019-03-31 20:01:16 +02:00
|
|
|
}
|
2019-04-19 00:37:20 +02:00
|
|
|
GDB_UnlockAllContexts(&gdbServer);
|
2019-03-31 20:01:16 +02:00
|
|
|
}
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2017-06-05 02:02:04 +02:00
|
|
|
void debuggerSocketThreadMain(void)
|
|
|
|
{
|
|
|
|
GDB_IncrementServerReferenceCount(&gdbServer);
|
|
|
|
GDB_RunServer(&gdbServer);
|
|
|
|
GDB_DecrementServerReferenceCount(&gdbServer);
|
|
|
|
}
|
|
|
|
|
|
|
|
void debuggerDebugThreadMain(void)
|
|
|
|
{
|
|
|
|
GDB_IncrementServerReferenceCount(&gdbServer);
|
|
|
|
GDB_RunMonitor(&gdbServer);
|
|
|
|
GDB_DecrementServerReferenceCount(&gdbServer);
|
|
|
|
}
|