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/server.h"
|
|
|
|
#include "gdb/net.h"
|
|
|
|
#include "gdb/query.h"
|
|
|
|
#include "gdb/verbose.h"
|
|
|
|
#include "gdb/thread.h"
|
|
|
|
#include "gdb/debug.h"
|
|
|
|
#include "gdb/regs.h"
|
|
|
|
#include "gdb/mem.h"
|
2019-04-14 21:48:15 +02:00
|
|
|
#include "gdb/hio.h"
|
2017-06-05 02:02:04 +02:00
|
|
|
#include "gdb/watchpoints.h"
|
|
|
|
#include "gdb/breakpoints.h"
|
|
|
|
#include "gdb/stop_point.h"
|
2019-04-18 22:58:53 +02:00
|
|
|
#include "task_runner.h"
|
2017-06-05 02:02:04 +02:00
|
|
|
|
|
|
|
Result GDB_InitializeServer(GDBServer *server)
|
|
|
|
{
|
|
|
|
Result ret = server_init(&server->super);
|
|
|
|
if(ret != 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
server->super.host = 0;
|
|
|
|
|
|
|
|
server->super.accept_cb = (sock_accept_cb)GDB_AcceptClient;
|
|
|
|
server->super.data_cb = (sock_data_cb) GDB_DoPacket;
|
|
|
|
server->super.close_cb = (sock_close_cb) GDB_CloseClient;
|
|
|
|
|
|
|
|
server->super.alloc = (sock_alloc_func) GDB_GetClient;
|
|
|
|
server->super.free = (sock_free_func) GDB_ReleaseClient;
|
|
|
|
|
|
|
|
server->super.clients_per_server = 1;
|
|
|
|
|
|
|
|
server->referenceCount = 0;
|
|
|
|
svcCreateEvent(&server->statusUpdated, RESET_ONESHOT);
|
2019-04-17 23:12:29 +02:00
|
|
|
svcCreateEvent(&server->statusUpdateReceived, RESET_STICKY);
|
2017-06-05 02:02:04 +02:00
|
|
|
|
|
|
|
for(u32 i = 0; i < sizeof(server->ctxs) / sizeof(GDBContext); i++)
|
|
|
|
GDB_InitializeContext(server->ctxs + i);
|
|
|
|
|
|
|
|
GDB_ResetWatchpoints();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GDB_FinalizeServer(GDBServer *server)
|
|
|
|
{
|
|
|
|
server_finalize(&server->super);
|
|
|
|
|
2019-06-16 22:06:05 +02:00
|
|
|
// Kill the "next application" context if needed
|
|
|
|
for (u32 i = 0; i < MAX_DEBUG; i++) {
|
|
|
|
if (server->ctxs[i].debug != 0)
|
|
|
|
GDB_CloseClient(&server->ctxs[i]);
|
|
|
|
}
|
2017-06-05 02:02:04 +02:00
|
|
|
svcCloseHandle(server->statusUpdated);
|
2019-04-17 23:12:29 +02:00
|
|
|
svcCloseHandle(server->statusUpdateReceived);
|
2017-06-05 02:02:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void GDB_IncrementServerReferenceCount(GDBServer *server)
|
|
|
|
{
|
|
|
|
AtomicPostIncrement(&server->referenceCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GDB_DecrementServerReferenceCount(GDBServer *server)
|
|
|
|
{
|
|
|
|
if(AtomicDecrement(&server->referenceCount) == 0)
|
|
|
|
GDB_FinalizeServer(server);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GDB_RunServer(GDBServer *server)
|
|
|
|
{
|
|
|
|
server_bind(&server->super, GDB_PORT_BASE);
|
|
|
|
server_bind(&server->super, GDB_PORT_BASE + 1);
|
|
|
|
server_bind(&server->super, GDB_PORT_BASE + 2);
|
2019-03-31 20:01:16 +02:00
|
|
|
|
|
|
|
server_bind(&server->super, GDB_PORT_BASE + 3); // next application
|
|
|
|
|
2017-06-05 02:02:04 +02:00
|
|
|
server_run(&server->super);
|
|
|
|
}
|
|
|
|
|
2019-03-31 01:12:22 +01:00
|
|
|
void GDB_LockAllContexts(GDBServer *server)
|
|
|
|
{
|
|
|
|
for (u32 i = 0; i < MAX_DEBUG; i++)
|
|
|
|
RecursiveLock_Lock(&server->ctxs[i].lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GDB_UnlockAllContexts(GDBServer *server)
|
|
|
|
{
|
|
|
|
for (u32 i = MAX_DEBUG; i > 0; i--)
|
|
|
|
RecursiveLock_Unlock(&server->ctxs[i - 1].lock);
|
|
|
|
}
|
|
|
|
|
2019-03-31 00:24:45 +01:00
|
|
|
GDBContext *GDB_SelectAvailableContext(GDBServer *server, u16 minPort, u16 maxPort)
|
|
|
|
{
|
|
|
|
GDBContext *ctx;
|
|
|
|
u16 port;
|
|
|
|
|
2019-03-31 01:12:22 +01:00
|
|
|
GDB_LockAllContexts(server);
|
|
|
|
|
2019-03-31 00:24:45 +01:00
|
|
|
// Get a context
|
|
|
|
u32 id;
|
2019-04-10 23:38:10 +02:00
|
|
|
for(id = 0; id < MAX_DEBUG && (server->ctxs[id].flags & GDB_FLAG_ALLOCATED_MASK); id++);
|
2019-03-31 00:24:45 +01:00
|
|
|
if(id < MAX_DEBUG)
|
|
|
|
ctx = &server->ctxs[id];
|
|
|
|
else
|
2019-03-31 01:12:22 +01:00
|
|
|
{
|
|
|
|
GDB_UnlockAllContexts(server);
|
2019-03-31 00:24:45 +01:00
|
|
|
return NULL;
|
2019-03-31 01:12:22 +01:00
|
|
|
}
|
2019-03-31 00:24:45 +01:00
|
|
|
|
|
|
|
// Get a port
|
|
|
|
for (port = minPort; port < maxPort; port++)
|
|
|
|
{
|
|
|
|
bool portUsed = false;
|
|
|
|
for(id = 0; id < MAX_DEBUG; id++)
|
|
|
|
{
|
2019-04-10 23:38:10 +02:00
|
|
|
if((server->ctxs[id].flags & GDB_FLAG_ALLOCATED_MASK) && server->ctxs[id].localPort == port)
|
2019-03-31 00:24:45 +01:00
|
|
|
portUsed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!portUsed)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (port >= maxPort)
|
|
|
|
{
|
|
|
|
ctx->flags = ~GDB_FLAG_SELECTED;
|
2019-03-31 01:12:22 +01:00
|
|
|
ctx = NULL;
|
2019-03-31 00:24:45 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ctx->flags |= GDB_FLAG_SELECTED;
|
|
|
|
ctx->localPort = port;
|
|
|
|
}
|
2019-03-31 01:12:22 +01:00
|
|
|
|
2019-06-16 22:06:05 +02:00
|
|
|
ctx->parent = server;
|
2019-03-31 01:12:22 +01:00
|
|
|
GDB_UnlockAllContexts(server);
|
|
|
|
return ctx;
|
2019-03-31 00:24:45 +01:00
|
|
|
}
|
|
|
|
|
2019-04-10 23:17:49 +02:00
|
|
|
GDBContext *GDB_FindAllocatedContextByPid(GDBServer *server, u32 pid)
|
|
|
|
{
|
|
|
|
GDB_LockAllContexts(server);
|
|
|
|
GDBContext *ctx = NULL;
|
|
|
|
for(u32 i = 0; i < MAX_DEBUG; i++)
|
|
|
|
{
|
2019-04-13 19:18:47 +02:00
|
|
|
if(
|
|
|
|
((server->ctxs[i].flags & GDB_FLAG_SELECTED) ||
|
|
|
|
(server->ctxs[i].state >= GDB_STATE_ATTACHED && server->ctxs[i].state < GDB_STATE_DETACHING))
|
|
|
|
&& server->ctxs[i].pid == pid
|
|
|
|
)
|
2019-04-10 23:17:49 +02:00
|
|
|
ctx = &server->ctxs[i];
|
|
|
|
}
|
|
|
|
GDB_UnlockAllContexts(server);
|
|
|
|
return ctx;
|
|
|
|
}
|
|
|
|
|
2017-06-05 02:02:04 +02:00
|
|
|
int GDB_AcceptClient(GDBContext *ctx)
|
|
|
|
{
|
2019-04-12 23:40:51 +02:00
|
|
|
Result r = 0;
|
2019-03-31 20:01:16 +02:00
|
|
|
|
2019-03-31 22:58:36 +02:00
|
|
|
RecursiveLock_Lock(&ctx->lock);
|
2019-04-13 19:18:47 +02:00
|
|
|
ctx->state = GDB_STATE_CONNECTED;
|
|
|
|
ctx->latestSentPacketSize = 0;
|
|
|
|
|
2019-04-12 23:40:51 +02:00
|
|
|
if (ctx->flags & GDB_FLAG_SELECTED)
|
|
|
|
r = GDB_AttachToProcess(ctx);
|
2019-04-13 19:18:47 +02:00
|
|
|
|
2017-06-05 02:02:04 +02:00
|
|
|
RecursiveLock_Unlock(&ctx->lock);
|
|
|
|
|
2019-03-31 22:58:36 +02:00
|
|
|
return R_SUCCEEDED(r) ? 0 : -1;
|
2017-06-05 02:02:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int GDB_CloseClient(GDBContext *ctx)
|
|
|
|
{
|
|
|
|
RecursiveLock_Lock(&ctx->lock);
|
2019-04-21 18:00:29 +02:00
|
|
|
svcClearEvent(ctx->processAttachedEvent);
|
|
|
|
ctx->eventToWaitFor = ctx->processAttachedEvent;
|
2019-04-17 23:12:29 +02:00
|
|
|
svcClearEvent(ctx->parent->statusUpdateReceived);
|
2019-03-31 22:58:36 +02:00
|
|
|
svcSignalEvent(ctx->parent->statusUpdated); // note: monitor will be waiting for lock
|
2019-04-17 23:12:29 +02:00
|
|
|
RecursiveLock_Unlock(&ctx->lock);
|
|
|
|
|
2019-06-16 22:06:05 +02:00
|
|
|
if(ctx->parent->referenceCount >= 2)
|
|
|
|
svcWaitSynchronization(ctx->parent->statusUpdateReceived, -1LL);
|
2019-04-17 23:12:29 +02:00
|
|
|
|
|
|
|
RecursiveLock_Lock(&ctx->lock);
|
2019-06-16 22:06:05 +02:00
|
|
|
if (ctx->state >= GDB_STATE_ATTACHED || ctx->debug != 0)
|
2019-04-21 18:00:29 +02:00
|
|
|
GDB_DetachFromProcess(ctx);
|
|
|
|
|
|
|
|
ctx->localPort = 0;
|
|
|
|
ctx->enableExternalMemoryAccess = false;
|
|
|
|
ctx->flags = 0;
|
2019-04-13 19:18:47 +02:00
|
|
|
ctx->state = GDB_STATE_DISCONNECTED;
|
2019-04-21 18:00:29 +02:00
|
|
|
|
|
|
|
ctx->catchThreadEvents = false;
|
|
|
|
|
|
|
|
memset(&ctx->latestDebugEvent, 0, sizeof(DebugEventInfo));
|
|
|
|
memset(ctx->memoryOsInfoXmlData, 0, sizeof(ctx->memoryOsInfoXmlData));
|
|
|
|
memset(ctx->processesOsInfoXmlData, 0, sizeof(ctx->processesOsInfoXmlData));
|
|
|
|
|
2019-04-27 16:50:47 +02:00
|
|
|
for (u32 i = 0; i < MAX_TIO_OPEN_FILE; i++)
|
|
|
|
IFile_Close(&ctx->openTioFileInfos[i].f);
|
|
|
|
memset(ctx->openTioFileInfos, 0, sizeof(ctx->openTioFileInfos));
|
|
|
|
ctx->numOpenTioFiles = 0;
|
|
|
|
|
2017-06-05 02:02:04 +02:00
|
|
|
RecursiveLock_Unlock(&ctx->lock);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
GDBContext *GDB_GetClient(GDBServer *server, u16 port)
|
|
|
|
{
|
2019-03-31 01:12:22 +01:00
|
|
|
GDB_LockAllContexts(server);
|
2019-03-31 00:24:45 +01:00
|
|
|
GDBContext *ctx = NULL;
|
2019-03-31 20:01:16 +02:00
|
|
|
for (u32 i = 0; i < MAX_DEBUG; i++)
|
2019-03-31 00:24:45 +01:00
|
|
|
{
|
2019-04-12 23:40:51 +02:00
|
|
|
if (server->ctxs[i].localPort == port)
|
2019-03-31 00:24:45 +01:00
|
|
|
{
|
2019-03-31 20:01:16 +02:00
|
|
|
ctx = &server->ctxs[i];
|
|
|
|
break;
|
2019-03-31 00:24:45 +01:00
|
|
|
}
|
|
|
|
}
|
2017-06-05 02:02:04 +02:00
|
|
|
|
2019-03-31 00:24:45 +01:00
|
|
|
if (ctx != NULL)
|
2017-06-05 02:02:04 +02:00
|
|
|
{
|
2019-04-12 23:40:51 +02:00
|
|
|
// Context already tied to a port/selected
|
|
|
|
if (ctx->flags & GDB_FLAG_USED)
|
|
|
|
{
|
|
|
|
GDB_UnlockAllContexts(server);
|
|
|
|
return NULL;
|
|
|
|
}
|
2019-04-18 22:58:53 +02:00
|
|
|
|
2019-04-12 23:40:51 +02:00
|
|
|
ctx->flags |= GDB_FLAG_USED;
|
|
|
|
ctx->state = GDB_STATE_CONNECTED;
|
|
|
|
ctx->parent = server;
|
|
|
|
}
|
2019-04-18 22:28:23 +02:00
|
|
|
else if (port >= GDB_PORT_BASE && port < GDB_PORT_BASE + MAX_DEBUG)
|
2019-04-12 23:40:51 +02:00
|
|
|
{
|
|
|
|
// Grab a free context
|
|
|
|
u32 id;
|
|
|
|
for(id = 0; id < MAX_DEBUG && (server->ctxs[id].flags & GDB_FLAG_ALLOCATED_MASK); id++);
|
|
|
|
if(id < MAX_DEBUG)
|
|
|
|
ctx = &server->ctxs[id];
|
|
|
|
else
|
|
|
|
{
|
|
|
|
GDB_UnlockAllContexts(server);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx->localPort = port;
|
2017-06-05 02:02:04 +02:00
|
|
|
ctx->flags |= GDB_FLAG_USED;
|
|
|
|
ctx->state = GDB_STATE_CONNECTED;
|
2019-03-31 22:58:36 +02:00
|
|
|
ctx->parent = server;
|
2017-06-05 02:02:04 +02:00
|
|
|
}
|
|
|
|
|
2019-03-31 01:12:22 +01:00
|
|
|
GDB_UnlockAllContexts(server);
|
2019-05-01 19:37:46 +02:00
|
|
|
if (port == GDB_PORT_BASE + MAX_DEBUG)
|
|
|
|
{
|
|
|
|
// this is not sufficient/foolproof and is buggy: TaskRunner_WaitReady(); // Finish grabbing new process debug, if anything...
|
|
|
|
bool ok = false;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
svcSleepThread(5 * 1000 * 1000LL);
|
|
|
|
RecursiveLock_Lock(&ctx->lock);
|
|
|
|
ok = ctx->debug != 0;
|
|
|
|
RecursiveLock_Unlock(&ctx->lock);
|
|
|
|
}
|
|
|
|
while (!ok);
|
|
|
|
}
|
|
|
|
|
2019-03-31 00:24:45 +01:00
|
|
|
return ctx;
|
2017-06-05 02:02:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void GDB_ReleaseClient(GDBServer *server, GDBContext *ctx)
|
|
|
|
{
|
2019-03-31 22:58:36 +02:00
|
|
|
(void)server;
|
2019-04-21 18:00:29 +02:00
|
|
|
(void)ctx;
|
2017-06-05 02:02:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static const struct
|
|
|
|
{
|
|
|
|
char command;
|
|
|
|
GDBCommandHandler handler;
|
|
|
|
} gdbCommandHandlers[] =
|
|
|
|
{
|
|
|
|
{ '?', GDB_HANDLER(GetStopReason) },
|
2019-04-12 23:40:51 +02:00
|
|
|
{ '!', GDB_HANDLER(EnableExtendedMode) },
|
2017-06-05 02:02:04 +02:00
|
|
|
{ 'c', GDB_HANDLER(Continue) },
|
|
|
|
{ 'C', GDB_HANDLER(Continue) },
|
|
|
|
{ 'D', GDB_HANDLER(Detach) },
|
2019-04-14 21:48:15 +02:00
|
|
|
{ 'F', GDB_HANDLER(HioReply) },
|
2017-06-05 02:02:04 +02:00
|
|
|
{ 'g', GDB_HANDLER(ReadRegisters) },
|
|
|
|
{ 'G', GDB_HANDLER(WriteRegisters) },
|
|
|
|
{ 'H', GDB_HANDLER(SetThreadId) },
|
|
|
|
{ 'k', GDB_HANDLER(Kill) },
|
|
|
|
{ 'm', GDB_HANDLER(ReadMemory) },
|
|
|
|
{ 'M', GDB_HANDLER(WriteMemory) },
|
|
|
|
{ 'p', GDB_HANDLER(ReadRegister) },
|
|
|
|
{ 'P', GDB_HANDLER(WriteRegister) },
|
|
|
|
{ 'q', GDB_HANDLER(ReadQuery) },
|
|
|
|
{ 'Q', GDB_HANDLER(WriteQuery) },
|
2019-04-13 19:18:47 +02:00
|
|
|
{ 'R', GDB_HANDLER(Restart) },
|
2017-06-05 02:02:04 +02:00
|
|
|
{ 'T', GDB_HANDLER(IsThreadAlive) },
|
|
|
|
{ 'v', GDB_HANDLER(VerboseCommand) },
|
|
|
|
{ 'X', GDB_HANDLER(WriteMemoryRaw) },
|
|
|
|
{ 'z', GDB_HANDLER(ToggleStopPoint) },
|
|
|
|
{ 'Z', GDB_HANDLER(ToggleStopPoint) },
|
|
|
|
};
|
|
|
|
|
|
|
|
static inline GDBCommandHandler GDB_GetCommandHandler(char command)
|
|
|
|
{
|
|
|
|
static const u32 nbHandlers = sizeof(gdbCommandHandlers) / sizeof(gdbCommandHandlers[0]);
|
|
|
|
|
|
|
|
u32 i;
|
|
|
|
for(i = 0; i < nbHandlers && gdbCommandHandlers[i].command != command; i++);
|
|
|
|
|
|
|
|
return i < nbHandlers ? gdbCommandHandlers[i].handler : GDB_HANDLER(Unsupported);
|
|
|
|
}
|
|
|
|
|
|
|
|
int GDB_DoPacket(GDBContext *ctx)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
RecursiveLock_Lock(&ctx->lock);
|
2019-04-10 23:38:10 +02:00
|
|
|
u32 oldFlags = ctx->flags;
|
2017-06-05 02:02:04 +02:00
|
|
|
|
|
|
|
if(ctx->state == GDB_STATE_DISCONNECTED)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
int r = GDB_ReceivePacket(ctx);
|
|
|
|
if(r == 0)
|
|
|
|
ret = 0;
|
|
|
|
else if(r == -1)
|
|
|
|
ret = -1;
|
|
|
|
else if(ctx->buffer[0] == '\x03')
|
|
|
|
{
|
|
|
|
GDB_HandleBreak(ctx);
|
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
else if(ctx->buffer[0] == '$')
|
|
|
|
{
|
|
|
|
GDBCommandHandler handler = GDB_GetCommandHandler(ctx->buffer[1]);
|
|
|
|
ctx->commandData = ctx->buffer + 2;
|
|
|
|
ret = handler(ctx);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ret = 0;
|
|
|
|
|
2019-03-31 22:58:36 +02:00
|
|
|
if(ctx->state == GDB_STATE_DETACHING)
|
2019-04-21 18:00:29 +02:00
|
|
|
{
|
|
|
|
if(ctx->flags & GDB_FLAG_EXTENDED_REMOTE)
|
|
|
|
{
|
|
|
|
ctx->state = GDB_STATE_CONNECTED;
|
|
|
|
RecursiveLock_Unlock(&ctx->lock);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
RecursiveLock_Unlock(&ctx->lock);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2017-06-05 02:02:04 +02:00
|
|
|
|
|
|
|
if((oldFlags & GDB_FLAG_PROCESS_CONTINUING) && !(ctx->flags & GDB_FLAG_PROCESS_CONTINUING))
|
|
|
|
{
|
|
|
|
if(R_FAILED(svcBreakDebugProcess(ctx->debug)))
|
|
|
|
ctx->flags |= GDB_FLAG_PROCESS_CONTINUING;
|
|
|
|
}
|
|
|
|
else if(!(oldFlags & GDB_FLAG_PROCESS_CONTINUING) && (ctx->flags & GDB_FLAG_PROCESS_CONTINUING))
|
|
|
|
svcSignalEvent(ctx->continuedEvent);
|
|
|
|
|
2019-04-21 18:00:29 +02:00
|
|
|
RecursiveLock_Unlock(&ctx->lock);
|
2017-06-05 02:02:04 +02:00
|
|
|
return ret;
|
|
|
|
}
|