diff --git a/sysmodules/rosalina/source/gdb.c b/sysmodules/rosalina/source/gdb.c index d020640..68f6d64 100644 --- a/sysmodules/rosalina/source/gdb.c +++ b/sysmodules/rosalina/source/gdb.c @@ -194,4 +194,4 @@ GDB_DECLARE_HANDLER(EnableExtendedMode) } else return GDB_ReplyEmpty(ctx); -} \ No newline at end of file +} diff --git a/sysmodules/rosalina/source/gdb/server.c b/sysmodules/rosalina/source/gdb/server.c index 2fd9d6c..dbd3d7c 100644 --- a/sysmodules/rosalina/source/gdb/server.c +++ b/sysmodules/rosalina/source/gdb/server.c @@ -52,6 +52,11 @@ 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) + GDB_CloseClient(&server->ctxs[i]); + } svcCloseHandle(server->statusUpdated); svcCloseHandle(server->statusUpdateReceived); } @@ -133,6 +138,7 @@ GDBContext *GDB_SelectAvailableContext(GDBServer *server, u16 minPort, u16 maxPo ctx->localPort = port; } + ctx->parent = server; GDB_UnlockAllContexts(server); return ctx; } @@ -179,10 +185,11 @@ int GDB_CloseClient(GDBContext *ctx) svcSignalEvent(ctx->parent->statusUpdated); // note: monitor will be waiting for lock RecursiveLock_Unlock(&ctx->lock); - svcWaitSynchronization(ctx->parent->statusUpdateReceived, -1LL); + if(ctx->parent->referenceCount >= 2) + svcWaitSynchronization(ctx->parent->statusUpdateReceived, -1LL); RecursiveLock_Lock(&ctx->lock); - if (ctx->state >= GDB_STATE_ATTACHED) + if (ctx->state >= GDB_STATE_ATTACHED || ctx->debug != 0) GDB_DetachFromProcess(ctx); ctx->localPort = 0; diff --git a/sysmodules/rosalina/source/menus/debugger.c b/sysmodules/rosalina/source/menus/debugger.c index 174bf3b..9ffee39 100644 --- a/sysmodules/rosalina/source/menus/debugger.c +++ b/sysmodules/rosalina/source/menus/debugger.c @@ -147,13 +147,9 @@ void DebuggerMenu_DisableDebugger(void) if(initialized) { - GDB_LockAllContexts(&gdbServer); - svcSignalEvent(gdbServer.super.shall_terminate_event); - //server_kill_connections(&gdbServer.super); - server_set_should_close_all(&gdbServer.super); - - GDB_UnlockAllContexts(&gdbServer); + server_kill_connections(&gdbServer.super); + //server_set_should_close_all(&gdbServer.super); res = MyThread_Join(&debuggerDebugThread, 2 * 1000 * 1000 * 1000LL); if(res == 0) diff --git a/sysmodules/rosalina/source/sock_util.c b/sysmodules/rosalina/source/sock_util.c index 020a65d..9f45d8b 100644 --- a/sysmodules/rosalina/source/sock_util.c +++ b/sysmodules/rosalina/source/sock_util.c @@ -77,6 +77,7 @@ static void server_close_ctx(struct sock_server *serv, struct sock_ctx *ctx) serv->poll_fds[ctx->i].revents = 0; ctx->type = SOCK_NONE; + serv->ctx_ptrs[ctx->i] = NULL; } 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) { struct pollfd *fds = serv->poll_fds; - Handle handles[2] = { terminationRequestEvent, serv->shall_terminate_event }; - s32 idx = -1; serv->running = true; svcSignalEvent(serv->started_event); while(serv->running && !terminationRequest) { - idx = -1; - if(svcWaitSynchronizationN(&idx, handles, 2, false, 0LL) == 0) + if(server_should_exit(serv)) goto abort_connections; if(serv->nfds == 0) { - if(svcWaitSynchronizationN(&idx, handles, 2, false, 12 * 1000 * 1000LL) == 0) - goto abort_connections; - else - continue; + svcSleepThread(12 * 1000 * 1000LL); + continue; } for(nfds_t i = 0; i < serv->nfds; i++) fds[i].revents = 0; int pollres = socPoll(fds, serv->nfds, 50); - idx = -1; - if(svcWaitSynchronizationN(&idx, handles, 2, false, 0LL) == 0) + if(server_should_exit(serv)) goto abort_connections; 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); 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; 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(svcWaitSynchronizationN(&idx, handles, 2, false, 0LL) == 0) + if(server_should_exit(serv)) goto abort_connections; if(serv->compact_needed) @@ -255,7 +254,6 @@ void server_run(struct sock_server *serv) abort_connections: server_kill_connections(serv); - serv->running = false; svcClearEvent(serv->started_event); }