diff --git a/sysmodules/pm/source/launch.c b/sysmodules/pm/source/launch.c index ba0a022..50163c1 100644 --- a/sysmodules/pm/source/launch.c +++ b/sysmodules/pm/source/launch.c @@ -516,9 +516,9 @@ Result autolaunchSysmodules(void) } // Custom -Result DebugNextApplicationByForce(void) +Result DebugNextApplicationByForce(bool debug) { - g_debugNextApplication = true; + g_debugNextApplication = debug; return 0; } diff --git a/sysmodules/pm/source/launch.h b/sysmodules/pm/source/launch.h index 101f4fe..add0abb 100644 --- a/sysmodules/pm/source/launch.h +++ b/sysmodules/pm/source/launch.h @@ -13,5 +13,5 @@ Result LaunchAppDebug(Handle *outDebug, const FS_ProgramInfo *programInfo, u32 l Result autolaunchSysmodules(void); // Custom -Result DebugNextApplicationByForce(void); +Result DebugNextApplicationByForce(bool debug); Result LaunchTitleDebug(Handle *outDebug, const FS_ProgramInfo *programInfo, u32 launchFlags); diff --git a/sysmodules/pm/source/pmdbg.c b/sysmodules/pm/source/pmdbg.c index f846e94..fc49a23 100644 --- a/sysmodules/pm/source/pmdbg.c +++ b/sysmodules/pm/source/pmdbg.c @@ -48,7 +48,7 @@ void pmDbgHandleCommands(void *ctx) cmdbuf[4] = pid; break; case 0x101: - cmdbuf[1] = DebugNextApplicationByForce(); + cmdbuf[1] = DebugNextApplicationByForce(cmdbuf[1] != 0); cmdbuf[0] = IPC_MakeHeader(0x101, 1, 0); break; case 0x102: diff --git a/sysmodules/rosalina/include/pmdbgext.h b/sysmodules/rosalina/include/pmdbgext.h index 788fd32..b8cf9e8 100644 --- a/sysmodules/rosalina/include/pmdbgext.h +++ b/sysmodules/rosalina/include/pmdbgext.h @@ -7,5 +7,5 @@ #include <3ds/services/pmdbg.h> Result PMDBG_GetCurrentAppTitleIdAndPid(u64 *outTitleId, u32 *outPid); -Result PMDBG_DebugNextApplicationByForce(void); +Result PMDBG_DebugNextApplicationByForce(bool debug); Result PMDBG_LaunchTitleDebug(Handle *outDebug, const FS_ProgramInfo *programInfo, u32 launchFlags); diff --git a/sysmodules/rosalina/include/sock_util.h b/sysmodules/rosalina/include/sock_util.h index 907311e..7215338 100644 --- a/sysmodules/rosalina/include/sock_util.h +++ b/sysmodules/rosalina/include/sock_util.h @@ -74,4 +74,5 @@ Result server_init(struct sock_server *serv); void server_bind(struct sock_server *serv, u16 port); void server_run(struct sock_server *serv); void server_kill_connections(struct sock_server *serv); +void server_set_should_close_all(struct sock_server *serv); void server_finalize(struct sock_server *serv); diff --git a/sysmodules/rosalina/source/hbloader.c b/sysmodules/rosalina/source/hbloader.c index a043e67..2d23e54 100644 --- a/sysmodules/rosalina/source/hbloader.c +++ b/sysmodules/rosalina/source/hbloader.c @@ -332,7 +332,7 @@ void HBLDR_HandleCommands(void *ctx) { nextApplicationGdbCtx->debug = 0; nextApplicationGdbCtx->pid = 0xFFFFFFFF; - res = PMDBG_DebugNextApplicationByForce(); + res = PMDBG_DebugNextApplicationByForce(true); if (R_FAILED(res)) { nextApplicationGdbCtx->flags = 0; diff --git a/sysmodules/rosalina/source/menus/debugger.c b/sysmodules/rosalina/source/menus/debugger.c index 16df912..174bf3b 100644 --- a/sysmodules/rosalina/source/menus/debugger.c +++ b/sysmodules/rosalina/source/menus/debugger.c @@ -147,11 +147,23 @@ void DebuggerMenu_DisableDebugger(void) if(initialized) { + GDB_LockAllContexts(&gdbServer); + svcSignalEvent(gdbServer.super.shall_terminate_event); - server_kill_connections(&gdbServer.super); - res = MyThread_Join(&debuggerDebugThread, 5 * 1000 * 1000 * 1000LL); + //server_kill_connections(&gdbServer.super); + server_set_should_close_all(&gdbServer.super); + + GDB_UnlockAllContexts(&gdbServer); + + res = MyThread_Join(&debuggerDebugThread, 2 * 1000 * 1000 * 1000LL); if(res == 0) - res = MyThread_Join(&debuggerSocketThread, 5 * 1000 * 1000 * 1000LL); + res = MyThread_Join(&debuggerSocketThread, 2 * 1000 * 1000 * 1000LL); + + Handle dummy = 0; + PMDBG_RunQueuedProcess(&dummy); + svcCloseHandle(dummy); + PMDBG_DebugNextApplicationByForce(false); + nextApplicationGdbCtx = NULL; svcKernelSetState(0x10000, 2); } @@ -188,7 +200,7 @@ void DebuggerMenu_DebugNextApplicationByForce(void) { nextApplicationGdbCtx->debug = 0; nextApplicationGdbCtx->pid = 0xFFFFFFFF; - res = PMDBG_DebugNextApplicationByForce(); + res = PMDBG_DebugNextApplicationByForce(true); if(R_SUCCEEDED(res)) sprintf(buf, "Operation succeeded.\nUse port %d to connect to the next launched\napplication.", nextApplicationGdbCtx->localPort); else diff --git a/sysmodules/rosalina/source/pmdbgext.c b/sysmodules/rosalina/source/pmdbgext.c index 420c452..061bddd 100644 --- a/sysmodules/rosalina/source/pmdbgext.c +++ b/sysmodules/rosalina/source/pmdbgext.c @@ -22,11 +22,12 @@ Result PMDBG_GetCurrentAppTitleIdAndPid(u64 *outTitleId, u32 *outPid) return cmdbuf[1]; } -Result PMDBG_DebugNextApplicationByForce(void) +Result PMDBG_DebugNextApplicationByForce(bool debug) { Result ret = 0; u32 *cmdbuf = getThreadCommandBuffer(); - cmdbuf[0] = IPC_MakeHeader(0x101, 0, 0); + cmdbuf[0] = IPC_MakeHeader(0x101, 1, 0); + cmdbuf[1] = (u32)debug; if(R_FAILED(ret = svcSendSyncRequest(*pmDbgGetSessionHandle()))) return ret; return cmdbuf[1]; diff --git a/sysmodules/rosalina/source/sock_util.c b/sysmodules/rosalina/source/sock_util.c index 5314696..020a65d 100644 --- a/sysmodules/rosalina/source/sock_util.c +++ b/sysmodules/rosalina/source/sock_util.c @@ -152,7 +152,6 @@ void server_run(struct sock_server *serv) serv->running = true; svcSignalEvent(serv->started_event); - while(serv->running && !terminationRequest) { idx = -1; @@ -205,6 +204,7 @@ void server_run(struct sock_server *serv) { fds[serv->nfds].fd = client_sockfd; fds[serv->nfds].events = POLLIN; + fds[serv->nfds].revents = 0; int new_idx = serv->nfds; serv->nfds++; @@ -215,6 +215,7 @@ void server_run(struct sock_server *serv) new_ctx->serv = curr_ctx; new_ctx->i = new_idx; new_ctx->n = 0; + new_ctx->should_close = false; serv->ctx_ptrs[new_idx] = new_ctx; @@ -259,6 +260,14 @@ abort_connections: svcClearEvent(serv->started_event); } +void server_set_should_close_all(struct sock_server *serv) +{ + nfds_t nfds = serv->nfds; + + for(unsigned int i = 0; i < nfds; i++) + serv->ctx_ptrs[i]->should_close = true; +} + void server_kill_connections(struct sock_server *serv) { struct pollfd *fds = serv->poll_fds;