views:

1290

answers:

5

Is it possible to convert floating point exceptions (signals) into C++ exceptions on x86 Linux?

This is for debugging purposes, so nonportability and imperfection is okay (e.g., if it isn't 100% guaranteed that all destructors are called).

+3  A: 

Due to the way signals and exceptions work, you can't do it immediately when the signal is thrown - exceptions rely on certain aspects of the stack that aren't true when a signal handler gets called.

You can set a global variable in the signal handler, and then check this at key points in the program and throw an exception if it's set. This doesn't give you the exact information about the thrown exception, though.

Branan
You can compute and store the stack trace in the signal handler using backtrace_symbols, which at least gives you a complete stack trace.
Geoffrey Irving
A: 

I don't have a ready made solution, but one thing you could look at are signals (not sure whether you can safely throw C++ exceptions from them, but it should help for debugging anyway.)

You could install a signal handler for, I think, SIGFPE, and use that for your debugging purposes.

Tobi
A: 

The basic idea will be for you to install the appropriate signal handlers for floating point exceptions. Inside your signal handler, you can throw an exception (or send a user-defined signal to another process which will raise the exception, or send a message to another thread for something similar, etc. etc. etc). There are any number of ways to actually throw the exception - the main thing is to handle the signal.

Ben Collins
+3  A: 

the gcc option -fnon-call-exceptions might be of some use to you. Couldn't find any documentation on it though so your mileage may vary.

+6  A: 
jwfearn