Fix "disable debugger" finally

This commit is contained in:
TuxSH 2019-06-16 22:06:05 +02:00
parent d989c45c3d
commit e989309771
4 changed files with 24 additions and 23 deletions

View File

@ -194,4 +194,4 @@ GDB_DECLARE_HANDLER(EnableExtendedMode)
} }
else else
return GDB_ReplyEmpty(ctx); return GDB_ReplyEmpty(ctx);
} }

View File

@ -52,6 +52,11 @@ void GDB_FinalizeServer(GDBServer *server)
{ {
server_finalize(&server->super); 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)
GDB_CloseClient(&server->ctxs[i]);
}
svcCloseHandle(server->statusUpdated); svcCloseHandle(server->statusUpdated);
svcCloseHandle(server->statusUpdateReceived); svcCloseHandle(server->statusUpdateReceived);
} }
@ -133,6 +138,7 @@ GDBContext *GDB_SelectAvailableContext(GDBServer *server, u16 minPort, u16 maxPo
ctx->localPort = port; ctx->localPort = port;
} }
ctx->parent = server;
GDB_UnlockAllContexts(server); GDB_UnlockAllContexts(server);
return ctx; return ctx;
} }
@ -179,10 +185,11 @@ int GDB_CloseClient(GDBContext *ctx)
svcSignalEvent(ctx->parent->statusUpdated); // note: monitor will be waiting for lock svcSignalEvent(ctx->parent->statusUpdated); // note: monitor will be waiting for lock
RecursiveLock_Unlock(&ctx->lock); RecursiveLock_Unlock(&ctx->lock);
svcWaitSynchronization(ctx->parent->statusUpdateReceived, -1LL); if(ctx->parent->referenceCount >= 2)
svcWaitSynchronization(ctx->parent->statusUpdateReceived, -1LL);
RecursiveLock_Lock(&ctx->lock); RecursiveLock_Lock(&ctx->lock);
if (ctx->state >= GDB_STATE_ATTACHED) if (ctx->state >= GDB_STATE_ATTACHED || ctx->debug != 0)
GDB_DetachFromProcess(ctx); GDB_DetachFromProcess(ctx);
ctx->localPort = 0; ctx->localPort = 0;

View File

@ -147,13 +147,9 @@ void DebuggerMenu_DisableDebugger(void)
if(initialized) if(initialized)
{ {
GDB_LockAllContexts(&gdbServer);
svcSignalEvent(gdbServer.super.shall_terminate_event); svcSignalEvent(gdbServer.super.shall_terminate_event);
//server_kill_connections(&gdbServer.super); server_kill_connections(&gdbServer.super);
server_set_should_close_all(&gdbServer.super); //server_set_should_close_all(&gdbServer.super);
GDB_UnlockAllContexts(&gdbServer);
res = MyThread_Join(&debuggerDebugThread, 2 * 1000 * 1000 * 1000LL); res = MyThread_Join(&debuggerDebugThread, 2 * 1000 * 1000 * 1000LL);
if(res == 0) if(res == 0)

View File

@ -77,6 +77,7 @@ static void server_close_ctx(struct sock_server *serv, struct sock_ctx *ctx)
serv->poll_fds[ctx->i].revents = 0; serv->poll_fds[ctx->i].revents = 0;
ctx->type = SOCK_NONE; ctx->type = SOCK_NONE;
serv->ctx_ptrs[ctx->i] = NULL;
} }
Result server_init(struct sock_server *serv) Result server_init(struct sock_server *serv)
@ -144,34 +145,33 @@ void server_bind(struct sock_server *serv, u16 port)
} }
} }
static bool server_should_exit(struct sock_server *serv)
{
return svcWaitSynchronization(serv->shall_terminate_event, 0) == 0 || svcWaitSynchronization(terminationRequestEvent, 0) == 0;
}
void server_run(struct sock_server *serv) void server_run(struct sock_server *serv)
{ {
struct pollfd *fds = serv->poll_fds; struct pollfd *fds = serv->poll_fds;
Handle handles[2] = { terminationRequestEvent, serv->shall_terminate_event };
s32 idx = -1;
serv->running = true; serv->running = true;
svcSignalEvent(serv->started_event); svcSignalEvent(serv->started_event);
while(serv->running && !terminationRequest) while(serv->running && !terminationRequest)
{ {
idx = -1; if(server_should_exit(serv))
if(svcWaitSynchronizationN(&idx, handles, 2, false, 0LL) == 0)
goto abort_connections; goto abort_connections;
if(serv->nfds == 0) if(serv->nfds == 0)
{ {
if(svcWaitSynchronizationN(&idx, handles, 2, false, 12 * 1000 * 1000LL) == 0) svcSleepThread(12 * 1000 * 1000LL);
goto abort_connections; continue;
else
continue;
} }
for(nfds_t i = 0; i < serv->nfds; i++) for(nfds_t i = 0; i < serv->nfds; i++)
fds[i].revents = 0; fds[i].revents = 0;
int pollres = socPoll(fds, serv->nfds, 50); int pollres = socPoll(fds, serv->nfds, 50);
idx = -1; if(server_should_exit(serv))
if(svcWaitSynchronizationN(&idx, handles, 2, false, 0LL) == 0)
goto abort_connections; goto abort_connections;
for(nfds_t i = 0; pollres > 0 && i < serv->nfds; i++) for(nfds_t i = 0; pollres > 0 && i < serv->nfds; i++)
@ -189,7 +189,7 @@ void server_run(struct sock_server *serv)
socklen_t len = sizeof(struct sockaddr_in); socklen_t len = sizeof(struct sockaddr_in);
int client_sockfd = socAccept(fds[i].fd, (struct sockaddr *)&saddr, &len); int client_sockfd = socAccept(fds[i].fd, (struct sockaddr *)&saddr, &len);
if(svcWaitSynchronizationN(&idx, handles, 2, false, 0LL) == 0) if(server_should_exit(serv))
goto abort_connections; goto abort_connections;
if(client_sockfd < 0 || curr_ctx->n == serv->clients_per_server || serv->nfds == MAX_CTXS) if(client_sockfd < 0 || curr_ctx->n == serv->clients_per_server || serv->nfds == MAX_CTXS)
@ -234,8 +234,7 @@ void server_run(struct sock_server *serv)
} }
} }
idx = -1; if(server_should_exit(serv))
if(svcWaitSynchronizationN(&idx, handles, 2, false, 0LL) == 0)
goto abort_connections; goto abort_connections;
if(serv->compact_needed) if(serv->compact_needed)
@ -255,7 +254,6 @@ void server_run(struct sock_server *serv)
abort_connections: abort_connections:
server_kill_connections(serv); server_kill_connections(serv);
serv->running = false; serv->running = false;
svcClearEvent(serv->started_event); svcClearEvent(serv->started_event);
} }