thermosphere: wip gdb

This commit is contained in:
TuxSH
2020-01-22 01:50:03 +00:00
parent f23fb45956
commit 9ebf3c9580
8 changed files with 134 additions and 500 deletions

View File

@@ -17,266 +17,98 @@
#include "gdb/watchpoints.h"
#include "gdb/breakpoints.h"
#include "gdb/stop_point.h"
#include "task_runner.h"
Result GDB_InitializeServer(GDBServer *server)
void 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);
svcCreateEvent(&server->statusUpdateReceived, RESET_STICKY);
for(u32 i = 0; i < sizeof(server->ctxs) / sizeof(GDBContext); i++)
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);
// Kill the "next application" context if needed
for (u32 i = 0; i < MAX_DEBUG; i++) {
if (server->ctxs[i].debug != 0)
for (u32 i = 0; i < MAX_CTX; i++) {
if (server->ctxs[i].state != GDB_STATE_DISCONNECTED) {
GDB_CloseClient(&server->ctxs[i]);
}
}
svcCloseHandle(server->statusUpdated);
svcCloseHandle(server->statusUpdateReceived);
}
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);
server_bind(&server->super, GDB_PORT_BASE + 3); // next application
server_run(&server->super);
// TODO transport iface
(void)server;
}
void GDB_LockAllContexts(GDBServer *server)
{
for (u32 i = 0; i < MAX_DEBUG; i++)
RecursiveLock_Lock(&server->ctxs[i].lock);
for (u32 i = 0; i < MAX_CTX; i++) {
recursiveSpinlockLock(&server->ctxs[i].lock);
}
}
void GDB_UnlockAllContexts(GDBServer *server)
{
for (u32 i = MAX_DEBUG; i > 0; i--)
RecursiveLock_Unlock(&server->ctxs[i - 1].lock);
for (u32 i = MAX_CTX; i > 0; i--) {
recursiveSpinlockUnlock(&server->ctxs[i - 1].lock);
}
}
GDBContext *GDB_SelectAvailableContext(GDBServer *server, u16 minPort, u16 maxPort)
GDBContext *GDB_SelectAvailableContext(GDBServer *server)
{
GDBContext *ctx;
u16 port;
GDB_LockAllContexts(server);
// Get a 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;
}
size_t id;
for (id = 0; id < MAX_CTX && (server->ctxs[id].flags & GDB_FLAG_ALLOCATED_MASK); id++);
ctx = id < MAX_CTX ? &server->ctxs[id] : NULL;
// Get a port
for (port = minPort; port < maxPort; port++)
{
bool portUsed = false;
for(id = 0; id < MAX_DEBUG; id++)
{
if((server->ctxs[id].flags & GDB_FLAG_ALLOCATED_MASK) && server->ctxs[id].localPort == port)
portUsed = true;
}
if (!portUsed)
break;
}
if (port >= maxPort)
{
ctx->flags = ~GDB_FLAG_SELECTED;
ctx = NULL;
}
else
{
ctx->flags |= GDB_FLAG_SELECTED;
ctx->localPort = port;
ctx->parent = server;
}
GDB_UnlockAllContexts(server);
return ctx;
}
GDBContext *GDB_FindAllocatedContextByPid(GDBServer *server, u32 pid)
{
GDB_LockAllContexts(server);
GDBContext *ctx = NULL;
for(u32 i = 0; i < MAX_DEBUG; i++)
{
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
)
ctx = &server->ctxs[i];
}
GDB_UnlockAllContexts(server);
return ctx;
}
int GDB_AcceptClient(GDBContext *ctx)
{
Result r = 0;
RecursiveLock_Lock(&ctx->lock);
recursiveSpinlockLock(&ctx->lock);
ctx->state = GDB_STATE_CONNECTED;
ctx->latestSentPacketSize = 0;
if (ctx->flags & GDB_FLAG_SELECTED)
/*if (ctx->flags & GDB_FLAG_SELECTED)
r = GDB_AttachToProcess(ctx);
*/
recursiveSpinlockUnlock(&ctx->lock);
RecursiveLock_Unlock(&ctx->lock);
return R_SUCCEEDED(r) ? 0 : -1;
return 0;
}
int GDB_CloseClient(GDBContext *ctx)
{
RecursiveLock_Lock(&ctx->lock);
svcClearEvent(ctx->processAttachedEvent);
ctx->eventToWaitFor = ctx->processAttachedEvent;
svcClearEvent(ctx->parent->statusUpdateReceived);
svcSignalEvent(ctx->parent->statusUpdated); // note: monitor will be waiting for lock
RecursiveLock_Unlock(&ctx->lock);
// currently unused
recursiveSpinlockLock(&ctx->lock);
if(ctx->parent->referenceCount >= 2)
svcWaitSynchronization(ctx->parent->statusUpdateReceived, -1LL);
RecursiveLock_Lock(&ctx->lock);
if (ctx->state >= GDB_STATE_ATTACHED || ctx->debug != 0)
if (ctx->state >= GDB_STATE_ATTACHED) {
GDB_DetachFromProcess(ctx);
}
ctx->localPort = 0;
ctx->enableExternalMemoryAccess = false;
ctx->flags = 0;
ctx->state = GDB_STATE_DISCONNECTED;
ctx->catchThreadEvents = false;
memset(&ctx->latestDebugEvent, 0, sizeof(DebugEventInfo));
memset(ctx->memoryOsInfoXmlData, 0, sizeof(ctx->memoryOsInfoXmlData));
memset(ctx->processesOsInfoXmlData, 0, sizeof(ctx->processesOsInfoXmlData));
// memset(&ctx->latestDebugEvent, 0, sizeof(DebugEventInfo)); TODO
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;
RecursiveLock_Unlock(&ctx->lock);
recursiveSpinlockUnlock(&ctx->lock);
return 0;
}
GDBContext *GDB_GetClient(GDBServer *server, u16 port)
{
GDB_LockAllContexts(server);
GDBContext *ctx = NULL;
for (u32 i = 0; i < MAX_DEBUG; i++)
{
if (server->ctxs[i].localPort == port)
{
ctx = &server->ctxs[i];
break;
}
}
if (ctx != NULL)
{
// Context already tied to a port/selected
if (ctx->flags & GDB_FLAG_USED)
{
GDB_UnlockAllContexts(server);
return NULL;
}
ctx->flags |= GDB_FLAG_USED;
ctx->state = GDB_STATE_CONNECTED;
ctx->parent = server;
}
else if (port >= GDB_PORT_BASE && port < GDB_PORT_BASE + MAX_DEBUG)
{
// 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;
ctx->flags |= GDB_FLAG_USED;
ctx->state = GDB_STATE_CONNECTED;
ctx->parent = server;
}
GDB_UnlockAllContexts(server);
if (port == GDB_PORT_BASE + MAX_DEBUG && ctx != NULL)
{
// 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);
}
return ctx;
}
void GDB_ReleaseClient(GDBServer *server, GDBContext *ctx)
{
// same thing
(void)server;
(void)ctx;
}
@@ -315,8 +147,8 @@ 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++);
size_t i;
for (i = 0; i < nbHandlers && gdbCommandHandlers[i].command != command; i++);
return i < nbHandlers ? gdbCommandHandlers[i].handler : GDB_HANDLER(Unsupported);
}
@@ -325,54 +157,47 @@ int GDB_DoPacket(GDBContext *ctx)
{
int ret;
RecursiveLock_Lock(&ctx->lock);
recursiveSpinlockLock(&ctx->lock);
u32 oldFlags = ctx->flags;
if(ctx->state == GDB_STATE_DISCONNECTED)
if(ctx->state == GDB_STATE_DISCONNECTED) {
return -1;
}
int r = GDB_ReceivePacket(ctx);
if(r == 0)
if (r == 0) {
ret = 0;
else if(r == -1)
} else if (r == -1) {
ret = -1;
else if(ctx->buffer[0] == '\x03')
{
} else if (ctx->buffer[0] == '\x03') {
GDB_HandleBreak(ctx);
ret = 0;
}
else if(ctx->buffer[0] == '$')
{
} else if (ctx->buffer[0] == '$') {
GDBCommandHandler handler = GDB_GetCommandHandler(ctx->buffer[1]);
ctx->commandData = ctx->buffer + 2;
ret = handler(ctx);
}
else
} else {
ret = 0;
}
if(ctx->state == GDB_STATE_DETACHING)
{
if(ctx->flags & GDB_FLAG_EXTENDED_REMOTE)
{
if (ctx->state == GDB_STATE_DETACHING) {
if (ctx->flags & GDB_FLAG_EXTENDED_REMOTE) {
ctx->state = GDB_STATE_CONNECTED;
RecursiveLock_Unlock(&ctx->lock);
recursiveSpinlockUnlock(&ctx->lock);
return ret;
}
else
{
RecursiveLock_Unlock(&ctx->lock);
} else {
recursiveSpinlockUnlock(&ctx->lock);
return -1;
}
}
if((oldFlags & GDB_FLAG_PROCESS_CONTINUING) && !(ctx->flags & GDB_FLAG_PROCESS_CONTINUING))
{
if(R_FAILED(svcBreakDebugProcess(ctx->debug)))
ctx->flags |= GDB_FLAG_PROCESS_CONTINUING;
if ((oldFlags & GDB_FLAG_CONTINUING) && !(ctx->flags & GDB_FLAG_CONTINUING)) {
// TODO
}
else if (!(oldFlags & GDB_FLAG_CONTINUING) && (ctx->flags & GDB_FLAG_CONTINUING)) {
// TODO
}
else if(!(oldFlags & GDB_FLAG_PROCESS_CONTINUING) && (ctx->flags & GDB_FLAG_PROCESS_CONTINUING))
svcSignalEvent(ctx->continuedEvent);
RecursiveLock_Unlock(&ctx->lock);
recursiveSpinlockUnlock(&ctx->lock);
return ret;
}