Skip to content

Commit

Permalink
Rough draft: allow SpawnDebugger after user close
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
garfieldnate committed Jul 21, 2024
1 parent 6198d85 commit 9c2e4f7
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion Core/ClientSML/src/sml_ClientAgent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 9c2e4f7

Please sign in to comment.