Skip to content

Commit

Permalink
Introduce a new function to manage program interruptions, enhancing c…
Browse files Browse the repository at this point in the history
…ontrol flow and error handling.

Modify the 'enable' variable type to bool for increased clarity.
  • Loading branch information
aramSofthenge committed Dec 4, 2023
1 parent e4a4d76 commit 2d4a782
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 7 deletions.
3 changes: 3 additions & 0 deletions tools/kwiver_tool_runner.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <vital/applets/kwiver_applet.h>
#include <vital/applets/applet_context.h>

#include "vital/kwiversys/kwiversys/SystemInformation.hxx"
#include <vital/applets/applet_registrar.h>
#include <vital/exceptions/base.h>
#include <vital/plugin_loader/plugin_factory.h>
Expand Down Expand Up @@ -171,6 +172,8 @@ void help_applet( const command_line_parser& options,
// ============================================================================
int main(int argc, char *argv[])
{
kwiversys::SystemInformation::InterruptionHandler();

//
// Global shared context
// Allocated on the stack so it will automatically clean up
Expand Down
25 changes: 21 additions & 4 deletions vital/kwiversys/SystemInformation.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,11 @@ class SystemInformationImplementation

// enable/disable stack trace signal handler.
static
void SetStackTraceOnError(int enable);
void SetStackTraceOnError(bool enable);

// interrupt the program
static
void InterruptionHandler(int sigNo);

// get current stack
static
Expand Down Expand Up @@ -835,11 +839,16 @@ SystemInformation::LongLong SystemInformation::GetProcessId()
return this->Implementation->GetProcessId();
}

void SystemInformation::SetStackTraceOnError(int enable)
void SystemInformation::SetStackTraceOnError(bool enable)
{
SystemInformationImplementation::SetStackTraceOnError(enable);
}

void SystemInformation::InterruptionHandler()
{
signal(SIGINT, SystemInformationImplementation::InterruptionHandler);
}

std::string SystemInformation::GetProgramStack(int firstFrame, int wholePath)
{
return SystemInformationImplementation::GetProgramStack(firstFrame, wholePath);
Expand Down Expand Up @@ -1290,7 +1299,7 @@ void StacktraceSignalHandler(

// restore the previously registered handlers
// and abort
SystemInformationImplementation::SetStackTraceOnError(0);
SystemInformationImplementation::SetStackTraceOnError(false);
abort();
//In the case multiple signals happen at the same time, only one will be
//processed, since the code terminates before the mutex is unlocked.
Expand Down Expand Up @@ -3760,7 +3769,7 @@ std::string SystemInformationImplementation::GetProgramStack(
/**
when set print stack trace in response to common signals.
*/
void SystemInformationImplementation::SetStackTraceOnError(int enable)
void SystemInformationImplementation::SetStackTraceOnError(bool enable)
{
#if !defined(_WIN32) && !defined(__MINGW32__) && !defined(__CYGWIN__)
static int saOrigValid=0;
Expand Down Expand Up @@ -3825,6 +3834,14 @@ void SystemInformationImplementation::SetStackTraceOnError(int enable)
#endif
}

void SystemInformationImplementation::InterruptionHandler([[maybe_unused]] int sigNo)
{
std::cerr << "\nSignal " << sigNo << " received, terminating the program." << std::endl;

// Terminate the program
std::_Exit(EXIT_SUCCESS);
}

bool SystemInformationImplementation::QueryWindowsMemory()
{
#if defined(_WIN32)
Expand Down
8 changes: 7 additions & 1 deletion vital/kwiversys/SystemInformation.hxx.in
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,13 @@ public:
// produce an informative stack trace the application should
// be dynamically linked and compiled with debug symbols.
static
void SetStackTraceOnError(int enable);
void SetStackTraceOnError(bool enable);

// Handle the SIGINT signal, triggered by Ctrl+C. This static
// function is designed to respond exclusively to SIGINT, leading to
// a termination of the application.
static
void InterruptionHandler();

// format and return the current program stack in a string. In
// order to produce an informative stack trace the application
Expand Down
4 changes: 2 additions & 2 deletions vital/kwiversys/testSystemInformation.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,13 @@ int testSystemInformation(int, char*[])
<< std::endl;

/* test segv handler
info.SetStackTraceOnError(1);
info.SetStackTraceOnError(true);
double *d = (double*)100;
*d=0;
*/

/* test abort handler
info.SetStackTraceOnError(1);
info.SetStackTraceOnError(true);
abort();
*/

Expand Down

0 comments on commit 2d4a782

Please sign in to comment.