gdb: refactor port/ctx alloc

This commit is contained in:
TuxSH
2019-03-31 00:24:45 +01:00
parent 22ec031495
commit cd18b85632
5 changed files with 70 additions and 18 deletions

View File

@@ -90,6 +90,51 @@ void GDB_RunServer(GDBServer *server)
server_run(&server->super);
}
GDBContext *GDB_SelectAvailableContext(GDBServer *server, u16 minPort, u16 maxPort)
{
GDBContext *ctx;
u16 port;
// Get a context
u32 id;
for(id = 0; id < MAX_DEBUG && (server->ctxs[id].flags & GDB_FLAG_SELECTED); id++);
if(id < MAX_DEBUG)
ctx = &server->ctxs[id];
else
return 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_SELECTED) && server->ctxs[id].localPort == port)
portUsed = true;
}
if (!portUsed)
break;
}
if (port >= maxPort)
{
RecursiveLock_Lock(&ctx->lock);
ctx->flags = ~GDB_FLAG_SELECTED;
RecursiveLock_Unlock(&ctx->lock);
return NULL;
}
else
{
RecursiveLock_Lock(&ctx->lock);
ctx->flags |= GDB_FLAG_SELECTED;
ctx->localPort = port;
RecursiveLock_Unlock(&ctx->lock);
return ctx;
}
}
int GDB_AcceptClient(GDBContext *ctx)
{
RecursiveLock_Lock(&ctx->lock);
@@ -148,27 +193,36 @@ int GDB_CloseClient(GDBContext *ctx)
svcClearEvent(ctx->clientAcceptedEvent);
ctx->eventToWaitFor = ctx->clientAcceptedEvent;
ctx->localPort = 0;
RecursiveLock_Unlock(&ctx->lock);
return 0;
}
GDBContext *GDB_GetClient(GDBServer *server, u16 port)
{
if(port < GDB_PORT_BASE || port >= GDB_PORT_BASE + sizeof(server->ctxs) / sizeof(GDBContext))
return NULL;
GDBContext *ctx = NULL;
if (port >= GDB_PORT_BASE && port < GDB_PORT_BASE + MAX_DEBUG)
{
for (u32 i = 0; i < MAX_DEBUG; i++)
{
if ((server->ctxs[i].flags & GDB_FLAG_SELECTED) && server->ctxs[i].localPort == port)
{
ctx = &server->ctxs[i];
break;
}
}
}
GDBContext *ctx = &server->ctxs[port - GDB_PORT_BASE];
if(!(ctx->flags & GDB_FLAG_USED) && (ctx->flags & GDB_FLAG_SELECTED))
if (ctx != NULL)
{
RecursiveLock_Lock(&ctx->lock);
ctx->flags |= GDB_FLAG_USED;
ctx->state = GDB_STATE_CONNECTED;
RecursiveLock_Unlock(&ctx->lock);
return ctx;
}
return NULL;
return ctx;
}
void GDB_ReleaseClient(GDBServer *server, GDBContext *ctx)