Exception handler...branching without using the stack pointer

J

JT

We have an exception handler which needs some tweaking because when we
changed compilers, the embedded assembly didn't work. Ideally, we
would like a complete C++ solution, without assembly statements.

One assumption is that the stack pointer may be invalid. So, we attach
a VxWorks exception handler to a method which has no local data and
does not call other methods (and so does not generate code which uses
the stack pointer). This method counts how many times it has been
called to guard against recursive exceptions in the event the stack
pointer is invalid. After the guard code, the method branches (with
embedded assembly) to the address of the entry of another method to
generically handle PowerPC exceptions.

So, is there another way of doing this all in C++? If not, is there
another way of branching using assembly that would be more portable
than using the address of the method.

Thanks
 
B

Branimir Maksimovic

JT said:
We have an exception handler which needs some tweaking because when we
changed compilers, the embedded assembly didn't work. Ideally, we
would like a complete C++ solution, without assembly statements.

One assumption is that the stack pointer may be invalid. So, we attach
a VxWorks exception handler to a method which has no local data and
does not call other methods (and so does not generate code which uses
the stack pointer). This method counts how many times it has been
called to guard against recursive exceptions in the event the stack
pointer is invalid. After the guard code, the method branches (with
embedded assembly) to the address of the entry of another method to
generically handle PowerPC exceptions.

This is useless for different compilers. You are digging into compilers
exception handling features and each compiler can do different things.
Simply do this:

jump_buf env;
const int FAILURE = 1;
int main()
{
int indication = setjmp(env); // returns 0 normaly, on FAILURE resets
the stack
// so you can call your
failure handler normaly
if(indication == FAILURE) my_failure_handler();
entry_function();
return 0;
}

and then attach in whichever way your want your handler:

void my_attached_exception_handler(...)
{
static bool failure_state;
if(failure_state == true) longjmp(env,FAILURE); // returns stack to state
before setjmp
}


Greetings, Bane.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top