2017-06-05 02:02:04 +02:00
|
|
|
/*
|
2019-05-03 21:06:10 +02:00
|
|
|
* This file is part of Luma3DS.
|
2019-02-25 02:04:32 +01:00
|
|
|
* Copyright (C) 2016-2019 Aurora Wright, TuxSH
|
2017-06-05 02:02:04 +02:00
|
|
|
*
|
2019-05-03 21:06:10 +02:00
|
|
|
* SPDX-License-Identifier: (MIT OR GPL-2.0-or-later)
|
2017-06-05 02:02:04 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "gdb.h"
|
|
|
|
#include "gdb/net.h"
|
2019-04-12 23:40:51 +02:00
|
|
|
#include "gdb/server.h"
|
2017-06-05 02:02:04 +02:00
|
|
|
|
2019-03-31 22:58:36 +02:00
|
|
|
#include "gdb/debug.h"
|
|
|
|
|
|
|
|
#include "gdb/watchpoints.h"
|
|
|
|
#include "gdb/breakpoints.h"
|
|
|
|
#include "gdb/stop_point.h"
|
|
|
|
|
2017-06-05 02:02:04 +02:00
|
|
|
void GDB_InitializeContext(GDBContext *ctx)
|
|
|
|
{
|
2019-03-31 00:24:45 +01:00
|
|
|
memset(ctx, 0, sizeof(GDBContext));
|
2017-06-14 10:04:09 +02:00
|
|
|
RecursiveLock_Init(&ctx->lock);
|
2017-06-05 02:02:04 +02:00
|
|
|
|
2017-06-14 10:04:09 +02:00
|
|
|
RecursiveLock_Lock(&ctx->lock);
|
2017-06-05 02:02:04 +02:00
|
|
|
|
2017-06-14 10:04:09 +02:00
|
|
|
svcCreateEvent(&ctx->continuedEvent, RESET_ONESHOT);
|
2019-03-31 22:58:36 +02:00
|
|
|
svcCreateEvent(&ctx->processAttachedEvent, RESET_STICKY);
|
2017-06-05 02:02:04 +02:00
|
|
|
|
2019-03-31 22:58:36 +02:00
|
|
|
ctx->eventToWaitFor = ctx->processAttachedEvent;
|
2017-06-14 10:04:09 +02:00
|
|
|
ctx->continueFlags = (DebugFlags)(DBG_SIGNAL_FAULT_EXCEPTION_EVENTS | DBG_INHIBIT_USER_CPU_EXCEPTION_HANDLERS);
|
2017-06-05 02:02:04 +02:00
|
|
|
|
2017-06-14 10:04:09 +02:00
|
|
|
RecursiveLock_Unlock(&ctx->lock);
|
2017-06-05 02:02:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void GDB_FinalizeContext(GDBContext *ctx)
|
|
|
|
{
|
2017-06-14 10:04:09 +02:00
|
|
|
RecursiveLock_Lock(&ctx->lock);
|
2017-06-05 02:02:04 +02:00
|
|
|
|
2019-03-31 22:58:36 +02:00
|
|
|
svcClearEvent(ctx->processAttachedEvent);
|
2017-06-05 02:02:04 +02:00
|
|
|
|
2019-03-31 22:58:36 +02:00
|
|
|
svcCloseHandle(ctx->processAttachedEvent);
|
2017-06-14 10:04:09 +02:00
|
|
|
svcCloseHandle(ctx->continuedEvent);
|
2017-06-05 02:02:04 +02:00
|
|
|
|
2017-06-14 10:04:09 +02:00
|
|
|
RecursiveLock_Unlock(&ctx->lock);
|
2017-06-05 02:02:04 +02:00
|
|
|
}
|
|
|
|
|
2019-03-31 22:58:36 +02:00
|
|
|
Result GDB_AttachToProcess(GDBContext *ctx)
|
|
|
|
{
|
|
|
|
Result r;
|
|
|
|
|
|
|
|
// Two cases: attached during execution, or started attached
|
|
|
|
// The second case will have, after RunQueuedProcess: attach process, debugger break, attach thread (with creator = 0)
|
|
|
|
|
|
|
|
if (!(ctx->flags & GDB_FLAG_ATTACHED_AT_START))
|
2019-04-18 00:40:20 +02:00
|
|
|
r = svcDebugActiveProcess(&ctx->debug, ctx->pid);
|
2019-03-31 22:58:36 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
r = 0;
|
|
|
|
}
|
|
|
|
if(R_SUCCEEDED(r))
|
|
|
|
{
|
|
|
|
// Note: ctx->pid will be (re)set while processing 'attach process'
|
|
|
|
DebugEventInfo *info = &ctx->latestDebugEvent;
|
|
|
|
ctx->processExited = ctx->processEnded = false;
|
|
|
|
if (!(ctx->flags & GDB_FLAG_ATTACHED_AT_START))
|
|
|
|
{
|
|
|
|
while(R_SUCCEEDED(svcGetProcessDebugEvent(info, ctx->debug)) &&
|
|
|
|
info->type != DBGEVENT_EXCEPTION &&
|
|
|
|
info->exception.type != EXCEVENT_ATTACH_BREAK)
|
|
|
|
{
|
|
|
|
GDB_PreprocessDebugEvent(ctx, info);
|
|
|
|
svcContinueDebugEvent(ctx->debug, ctx->continueFlags);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Attach process, debugger break
|
|
|
|
for(u32 i = 0; i < 2; i++)
|
|
|
|
{
|
|
|
|
if (R_FAILED(r = svcGetProcessDebugEvent(info, ctx->debug)))
|
|
|
|
return r;
|
|
|
|
GDB_PreprocessDebugEvent(ctx, info);
|
|
|
|
if (R_FAILED(r = svcContinueDebugEvent(ctx->debug, ctx->continueFlags)))
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(R_FAILED(r = svcWaitSynchronization(ctx->debug, -1LL)))
|
|
|
|
return r;
|
|
|
|
if (R_FAILED(r = svcGetProcessDebugEvent(info, ctx->debug)))
|
|
|
|
return r;
|
|
|
|
// Attach thread
|
|
|
|
GDB_PreprocessDebugEvent(ctx, info);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return r;
|
|
|
|
|
2019-04-13 19:18:47 +02:00
|
|
|
r = svcSignalEvent(ctx->processAttachedEvent);
|
|
|
|
if (R_SUCCEEDED(r))
|
|
|
|
ctx->state = GDB_STATE_ATTACHED;
|
|
|
|
return r;
|
2019-03-31 22:58:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void GDB_DetachFromProcess(GDBContext *ctx)
|
|
|
|
{
|
|
|
|
DebugEventInfo dummy;
|
|
|
|
for(u32 i = 0; i < ctx->nbBreakpoints; i++)
|
|
|
|
{
|
|
|
|
if(!ctx->breakpoints[i].persistent)
|
|
|
|
GDB_DisableBreakpointById(ctx, i);
|
|
|
|
}
|
|
|
|
memset(&ctx->breakpoints, 0, sizeof(ctx->breakpoints));
|
|
|
|
ctx->nbBreakpoints = 0;
|
|
|
|
|
|
|
|
for(u32 i = 0; i < ctx->nbWatchpoints; i++)
|
|
|
|
{
|
|
|
|
GDB_RemoveWatchpoint(ctx, ctx->watchpoints[i], WATCHPOINT_DISABLED);
|
|
|
|
ctx->watchpoints[i] = 0;
|
|
|
|
}
|
|
|
|
ctx->nbWatchpoints = 0;
|
|
|
|
|
|
|
|
svcKernelSetState(0x10002, ctx->pid, false);
|
|
|
|
memset(ctx->svcMask, 0, 32);
|
|
|
|
|
|
|
|
memset(ctx->threadListData, 0, sizeof(ctx->threadListData));
|
|
|
|
ctx->threadListDataPos = 0;
|
|
|
|
|
|
|
|
//svcSignalEvent(server->statusUpdated);
|
|
|
|
|
|
|
|
/*
|
|
|
|
There's a possibility of a race condition with a possible user exception handler, but you shouldn't
|
|
|
|
use 'kill' on APPLICATION titles in the first place (reboot hanging because the debugger is still running, etc).
|
|
|
|
*/
|
|
|
|
|
|
|
|
ctx->continueFlags = (DebugFlags)0;
|
|
|
|
|
|
|
|
while(R_SUCCEEDED(svcGetProcessDebugEvent(&dummy, ctx->debug)));
|
|
|
|
while(R_SUCCEEDED(svcContinueDebugEvent(ctx->debug, ctx->continueFlags)));
|
|
|
|
if(ctx->flags & GDB_FLAG_TERMINATE_PROCESS)
|
|
|
|
{
|
|
|
|
svcTerminateDebugProcess(ctx->debug);
|
|
|
|
ctx->processEnded = true;
|
|
|
|
ctx->processExited = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
while(R_SUCCEEDED(svcGetProcessDebugEvent(&dummy, ctx->debug)));
|
|
|
|
while(R_SUCCEEDED(svcContinueDebugEvent(ctx->debug, ctx->continueFlags)));
|
|
|
|
|
|
|
|
svcCloseHandle(ctx->debug);
|
|
|
|
ctx->debug = 0;
|
2019-04-13 19:18:47 +02:00
|
|
|
memset(&ctx->launchedProgramInfo, 0, sizeof(FS_ProgramInfo));
|
|
|
|
ctx->launchedProgramLaunchFlags = 0;
|
2019-03-31 22:58:36 +02:00
|
|
|
|
|
|
|
ctx->eventToWaitFor = ctx->processAttachedEvent;
|
|
|
|
ctx->continueFlags = (DebugFlags)(DBG_SIGNAL_FAULT_EXCEPTION_EVENTS | DBG_INHIBIT_USER_CPU_EXCEPTION_HANDLERS);
|
|
|
|
ctx->pid = 0;
|
2019-04-17 23:12:29 +02:00
|
|
|
ctx->currentThreadId = 0;
|
|
|
|
ctx->selectedThreadId = 0;
|
|
|
|
ctx->selectedThreadIdForContinuing = 0;
|
2019-03-31 22:58:36 +02:00
|
|
|
ctx->nbThreads = 0;
|
|
|
|
ctx->totalNbCreatedThreads = 0;
|
|
|
|
memset(ctx->threadInfos, 0, sizeof(ctx->threadInfos));
|
2019-04-13 19:18:47 +02:00
|
|
|
|
2019-04-14 21:48:15 +02:00
|
|
|
ctx->currentHioRequestTargetAddr = 0;
|
|
|
|
memset(&ctx->currentHioRequest, 0, sizeof(PackedGdbHioRequest));
|
2019-04-13 19:18:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Result GDB_CreateProcess(GDBContext *ctx, const FS_ProgramInfo *progInfo, u32 launchFlags)
|
|
|
|
{
|
|
|
|
Handle debug = 0;
|
|
|
|
ctx->debug = 0;
|
|
|
|
Result r = PMDBG_LaunchTitleDebug(&debug, progInfo, launchFlags);
|
|
|
|
if(R_FAILED(r))
|
|
|
|
return r;
|
|
|
|
|
|
|
|
ctx->flags |= GDB_FLAG_CREATED | GDB_FLAG_ATTACHED_AT_START;
|
|
|
|
ctx->debug = debug;
|
|
|
|
ctx->launchedProgramInfo = *progInfo;
|
|
|
|
ctx->launchedProgramLaunchFlags = launchFlags;
|
|
|
|
r = GDB_AttachToProcess(ctx);
|
|
|
|
return r;
|
2019-03-31 22:58:36 +02:00
|
|
|
}
|
|
|
|
|
2017-06-05 02:02:04 +02:00
|
|
|
GDB_DECLARE_HANDLER(Unsupported)
|
|
|
|
{
|
2017-06-14 10:04:09 +02:00
|
|
|
return GDB_ReplyEmpty(ctx);
|
2017-06-05 02:02:04 +02:00
|
|
|
}
|
2019-04-12 23:40:51 +02:00
|
|
|
|
|
|
|
GDB_DECLARE_HANDLER(EnableExtendedMode)
|
|
|
|
{
|
2019-04-18 22:28:23 +02:00
|
|
|
if (ctx->localPort >= GDB_PORT_BASE && ctx->localPort < GDB_PORT_BASE + MAX_DEBUG)
|
|
|
|
{
|
|
|
|
ctx->flags |= GDB_FLAG_EXTENDED_REMOTE;
|
|
|
|
return GDB_ReplyOk(ctx);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return GDB_ReplyEmpty(ctx);
|
2019-04-12 23:40:51 +02:00
|
|
|
}
|