Skip navigation.
 
mlC++ throw fails after hardware exception and Mach exception handling
FROM : Richard Brandt
DATE : Fri Aug 29 02:37:31 2008

I've been trying to get hardware exception handling working on OS X in the context of a C++ application running on Intel-based Macs.

Versions:
Mac OS X 10.4 (Tiger)
GCC 4.0.1 (Apple build 5370)

Our needs are:

* trap the usual hardware exceptions: integer and float divide by zero, float invalid operation, illegal memory references and so forth.

* after an exception, transfer execution in the victim thread to a glue function which will invoke the application's exception handler.  Ideally I want this little glue function to throw a C++ exception which will be caught in the application code.  The advantage of using a C++ thrown exception is that we get stack unwinding.  The alternative, setjmp() / longjmp() doesn't perform stack unwinding.  (Are there other alternatives for transferring control that I'm not aware of?)


I've started from the example in "Mac OS X Internals: A Systems Approach", page 1123.  This is a really good book by the way.

I have a Mach exception handling thread set up,
which calls its thread handler function,
which retrieves a Mach message indicating an exception has occurred,
and calls exc_server() to dispatch a call to catch_exception_raise(),
which does get called, and is able to retrieve the thread state,
and modify it by setting the EIP (instruction pointer) field so that
when the victim thread is resumed, execution should pick up in my
little glue function.  The thread state is then saved.
All this works fine, my glue function is called, and it throws a C++ object.

Catch handlers exist, both for the specific C++ class thrown, as well as catch(...) just in case.

No catch occurs.  The program prints:
  terminate called after throwing an instance of 'TestException'
  Abort trap

It seems that all the machinery is in place and working.  It appears something has been damaged so that the Gnu/Apple C++ runtime library thinks there are no catch clauses to throw to.

Prior to the exception and recovery, try / throw / catch works as expected.  I've been programming in C++ for a long long time, so I don't think I've made any dumb mistakes.  I only make smart mistakes now....  :)


Any thoughts would be welcome.  I'd be more than happy to supply source code for my little test program if anybody wants to take a look.

Thanks,
Rich Brandt

Related mailsAuthorDate
No related mails found.