Sysprogs forums › Forums › VisualGDB › Signals/Exceptions on Raspberry Pi
Tagged: Pi;Linux;Signals;Exceptions
- This topic has 1 reply, 2 voices, and was last updated 9 years, 2 months ago by support.
-
AuthorPosts
-
October 5, 2015 at 16:23 #7041pedleyParticipant
I am evaluating VisualGdb for developing a long-running process application (VisualGdb/Vs2012/Raspberry Pi Raspbian/Gnu g++0x (gdb) cross compiler on Win7 PC). Signals are giving me a headache. I am trying to convert them to c++ exceptions and then to catch them, with a view to handling them gracefully – not terminating unless necessary.
Below is some simple test code – leaving out provision for generic handling, singleton exceptions, etc. The problem is the same. The signal is caught and an exception thrown by its handler. This happens OK but the exception is never caught. Instead, a dialog appears reporting a SIGABRT and on clicking ‘Continue’, the program terminates with the SIGABRT.
Can anyone please tell me how I prevent the SIGABRT and allow the exception to be caught and the program potentially continue? I have tried playing with the ‘exception settings’ but to no avail.
The output from the test program is:
Process /tmp/SignalTest created; pid = 10535
Listening on port 2000
Remote debugging from host 192.168.1.10
sigaction returned: 0
About to throw exception …
terminate called after throwing an instance of ‘SignalExceptionClass’
what(): Memory Error
Child terminated with signal = 0x6 (SIGABRT)
GDBserver exiting
#include <iostream>
#include <exception>
#include <signal.h>
using namespace std;
//*** This is the exception class to be thrown
class SignalExceptionClass : public exception
{
string Message;
public:
SignalExceptionClass (string msg) : exception(), Message(msg) {}
virtual const char *what () const throw()
{ return Message.c_str (); }
~SignalExceptionClass () throw() {}
};
//*** And this the handler for the SIGSEGV signal
void SignalHandler (int sig)
{
cout << "About to throw exception ...\n";
throw SignalExceptionClass ("Memory Error");
}
//*** Main Program
int main(int argc, char *argv[])
{
//*** Set up the signal handler
struct sigaction sigAct;
sigAct.sa_handler = SignalHandler;
sigemptyset (&sigAct.sa_mask);
sigAct.sa_flags = 0;
int rc = sigaction (SIGSEGV, &sigAct, 0);
cout << "sigaction returned: " << rc << endl;
//*** Deliberately cause a segmentation fault
try
{
int *p = 0;
cout << *p << endl;
}
//*** Try to catch the specific exception thrown - never reached
catch (SignalExceptionClass &e)
{ cout << "Memory Exception Caught\n";
}
//*** Try to catch any remaining exception type - never reached
catch (...)
{ cout << "Unknown Exception Caught\n";
}
cout << "End of Main Program\n";
return 0;
}
October 9, 2015 at 05:16 #7053supportKeymasterHi,
This is a known limitation caused by the way signals are implemented in Linux. You read more about it on StackOverflow: http://stackoverflow.com/questions/1717991/throwing-an-exception-from-within-a-signal-handler
-
AuthorPosts
- You must be logged in to reply to this topic.