From 9c2e4f7e54418484c8b3e3b4d279f065dbd70c8c Mon Sep 17 00:00:00 2001 From: Nathan Glenn Date: Sat, 20 Jul 2024 23:17:59 -0500 Subject: [PATCH] Rough draft: allow SpawnDebugger after user close Previously we would require the user to call KillDebugger() in order to be able to call SpawnDebugger() again, even when the user had manually closed the debugger. Still TODO: * print PID (or Windows equivalent) to help user find process to kill if needed * Check that all error messages are okay * Test on Windows * Note required manual test: ```python import Python_sml_ClientInterface as sml k = sml.Kernel.CreateKernelInNewThread() a = k.CreateAgent("hi") a.SpawnDebugger() a.SpawnDebugger() False a.SpawnDebugger() ``` See #483. --- Core/ClientSML/src/sml_ClientAgent.cpp | 34 +++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/Core/ClientSML/src/sml_ClientAgent.cpp b/Core/ClientSML/src/sml_ClientAgent.cpp index ba9c805088..d47682b584 100644 --- a/Core/ClientSML/src/sml_ClientAgent.cpp +++ b/Core/ClientSML/src/sml_ClientAgent.cpp @@ -94,6 +94,21 @@ namespace sml }; } +bool debugger_is_running(DebuggerProcessInformation* processInformation) +{ +#ifdef _WIN32 + DWORD exitCode; + if (GetExitCodeProcess(processInformation->debuggerProcessInformation.hProcess, &exitCode)) + { + return exitCode == STILL_ACTIVE; + } + return false; +#else // _WIN32 + int status; + return waitpid(processInformation->debuggerPid, &status, WNOHANG) == 0; +#endif // not _WIN32 +} + std::string get_soarlib_path() { std::string h = libraryPath; @@ -1473,6 +1488,7 @@ bool Agent::SpawnDebugger(int port, const char* jarpath) { if (!isfile(jarpath)) { + std::cerr << "SpawnDebugger: jarparth is not a file" << std::endl; return false; } p = jarpath; @@ -1564,7 +1580,20 @@ bool Agent::SpawnDebugger(int port, const char* jarpath) if (m_pDPI) { - return false; + if (!debugger_is_running(m_pDPI)) + { + // TODO: put deallocation in a method to share (smart pointer deallocator?) + delete m_pDPI; + m_pDPI = 0; + std::cerr << "Debugger was closed by user; destroying old debugger process info" << std::endl; + } else { + // TODO: also print the PID in case the user wants to kill it + std::cerr << "Debugger is already running" << std::endl; + return false; + } + + // std::cerr << "m_pDPI is already defined" << std::endl; + // return false; } m_pDPI = new DebuggerProcessInformation(); @@ -1684,6 +1713,8 @@ bool Agent::SpawnDebugger(int port, const char* jarpath) { delete m_pDPI; m_pDPI = 0; + std::cerr << "fork failed" << std::endl; + perror("fork"); return false; } @@ -1711,6 +1742,7 @@ bool Agent::SpawnDebugger(int port, const char* jarpath) char buffer[4096 + 1]; if (getcwd(buffer, 4096) != buffer) { + std::cerr << "SpawnDebugger: getcwd failed" << std::endl; perror("getcwd"); return false; }