How to catch unhandled exceptions ?

BogDan bog_dan_ro at
Sun Jun 2 14:12:10 CEST 2013



>>  Is there any way to catch all system unhandled exceptions (seg fault,
>>  fpu fault, etc.) and "transform" them into C++ exceptions ?
> I'm afraid you're confusing two kinds of exceptions here.

I know very well the difference between the two kinds of exceptions :)
I've implemented (heavily inspired by the GNU's libjava see 
gcc/gcc/libjava/include/x86_64-signal.h) this kind of mechanism in
linux, where I could transform signals like SIG_SEG, SIG_FPU into 
C/C++ exceptions. I think my problem is English language :)

> Hardware exceptions (e.g., page faults) are raised by the CPU during
> execution and are delivered to a kernel-level handler function, because
> that's what the hardware specification says. Fiasco.OC being a
> microkernel, we do not handle those exceptions in a kernel, but reflect
> them to user-level handler threads - the page fault handler and the
> exception handler. Each thread on top of Fiasco.OC has a PF handler and
> an exception handler thread assigned. Whenever a CPU exception occurs
> while a thread executes, the respective handler threads get a notification.

> This is also what the functions you found (thread control + exc_handler)
> do - they can be used to assign PF and exception handlers to a thread.

So far so good! This is exactly what I want to do, to have a way to be notified
by the kernel about any exception of a thread. Now my problem is that I
get no notification when something bad happens (e.g. division by 0 or a 
page fault), is there any example where I can look to see how to use 
thread control + exc_handler and co ? I don't think I really know what to
pass to exc_handler method, I tried to pass a L4::Server_Object::obj_cap()
object but nothing happened ...

> In contrast, C++ language-level exceptions are raised synchronously by
> your code (or some library) using the C++ 'throw' keyword. These
> language-level exceptions can be caught and handled using try/catch-blocks.

> Transforming hardware exceptions into C++ exceptions appears to be
> difficult: Suppose, your thread raises a page fault and you want to make
> this a C++ exception - what happens if your exception handler raises
> another page fault - leading to another exception that raises yet
> another page fault ...

I think that will be a very exceptional case, where the exception handler
will raise another exception. But I think it can be handled somehow e.g. 
in the handler function you can guard it with an (atomic) bool variable.
That variable will be set if the handler raises another exception and 
it return immediately, this way the task will be killed immediately.

> Also, C++ exceptions need to be caught in the thread that raised the
> exception, otherwise the exceptions causes a call to std::terminate,
> wich will abort your application. Hence, you'd need try/catch statements
> for all potential hardware exceptions around all your threads' code.

Of course you need to add try/catch statements :) The beauty is that if
you'll do it, you'll have a chance to survive to save your data, if not, 
then, as you said, std::terminate will be called and you get the same

> I'm not saying it can't be done (I vaguely remember Choices OS [1] doing
> this), it's just not what we are doing on Fiasco right now.

AH, sorry , I think is a misunderstanding here, I don't want you to do it in
Fiasco (even I think it will be very nice feature), I want to be able to do it
in my apps (at user-level not kernel level)!

> [1]

So, I need your help in two (I hope) simple things:
 - how to set the handler.
 - how to resume the thread execution that raised the hardware exception
to another address (where actually is the code that will throw a C++ exception) .

Many thanks !


More information about the l4-hackers mailing list