How to catch unhandled exceptions ?

Björn Döbel doebel at os.inf.tu-dresden.de
Tue Jun 4 19:50:04 CEST 2013


On 03.06.2013 12:46, BogDan wrote:
> Hi,
>
>> [..]
>>>    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 ...
>>
>> The page fault handler and the exception handler are Fiasco.OC threads.
>> Hence, you pass thread capabilities to those calls. Page faults go to
>> the PF handler , all other exceptions end up in the exception handler
>> thread.
>>
>> In the thread you would then do something like
>>
>> while (1) {
>>      msg = l4_ipc_receive();
>>      inject_exception();
>> }
>>
>> In inject_exception() you can use the Thread::ex_regs system call, which
>> allows you to modify the IP/SP of a thread.
>>
>
> Still no joy :(, here http://paste.kde.org/757352/ what I'm using ... I still
> don't understand what I'm doing so wrong there ... Also I don't know
> how to get the caller thread l4_cap_idx_t struct from l4_utcb_t.

You can't. The UTCB is _your thread's_ UTCB. When an IPC is sent, data 
is copied from the sender's UTCB to the receiver's UTCB. Just from the 
message you cannot decide which thread sent it.

To know which thread sent an exception you can do two things I think:

1) Have one exc handler thread for every user thread. This will cost a
    little stack space for each handler, but this stack probably doesn't
    need to be too large. In this case the user-handler mapping is fix.

2) An exception IPC is in essence still an IPC, which means it can be
    sent to any object that is able to receive IPCs. Fiasco provides IPC
    gates as generic communication channels. Each gate is a dedicated
    channel that exactly one thread can listen on, while any thread that
    has the channel capability can send a message (and wait for a reply)
    on it. IPC gates have a special 'label' that can be defined during
    creation.

    When you do ipc_receive(), you don't need to pass a specific gate
    capability to wait on. Instead, you wait for a message from any gate
    your thread is bound to receive from. You can then identify the gate
    you got the message from by looking at the 'label' part of the recv
    message tag.

    So instead of creating N exc handler threads, you can also create N
    IPC gates with different labels and have on exc. handler thread wait
    on these gates (saving on stack space). The handler can then check
    the msgtag's IPC label to figure out which thread raised the
    exception.

>> [..]
>>>>    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
>>>    effect.
>>
>> There may be libraries (e.g., libc_be_signal) that spawn their own
>> threads and where you do not have control over potential try/catch
>> statements in their code. But that might not be an issue for you.
>>
>
>
> If I set the handler to main thread, then all the thread will have the
> same handler set, right ?

A thread inherits the exc. and pf handlers from its creator if not 
specified otherwise. Just make sure you don't set a thread to be its own 
handler as this will of course lead to a deadlock.

Bjoern
-- 
Dipl.-Inf. Bjoern Doebel    Mail:  doebel at tudos.org
TU Dresden, OS Chair        Phone: +49 351 463 38 799
Noethnitzer Str. 46         Fax:   +49 351 463 38 284
01187 Dresden, Germany      WWW:   http://www.tudos.org/~doebel
--
"When the seagulls follow the trawler, it's because they think
  sardines will be thrown into the sea." (Eric Cantona)




More information about the l4-hackers mailing list