Exception handling, Name, Synopsis – Comtrol eCos User Manual

Page 57: Description

Advertising
background image

Exception handling

Name

cyg_exception_set_handler, cyg_exception_clear_handler,

cyg_exception_call_handler

— Handle processor exceptions

Synopsis

#include

<

cyg/kernel/kapi.h

>

void cyg_exception_set_handler(cyg_code_t exception_number, cyg_exception_handler_t*

new_handler, cyg_addrword_t new_data, cyg_exception_handler_t** old_handler,

cyg_addrword_t* old_data);

void cyg_exception_clear_handler(cyg_code_t exception_number);

void cyg_exception_call_handler(cyg_handle_t thread, cyg_code_t exception_number,

cyg_addrword_t exception_info);

Description

Sometimes code attempts operations that are not legal on the current hardware, for example dividing by zero, or ac-
cessing data through a pointer that is not properly aligned. When this happens the hardware will raise an exception.
This is very similar to an interrupt, but happens synchronously with code execution rather than asynchronously and
hence can be tied to the thread that is currently running.

The exceptions that can be raised depend very much on the hardware, especially the processor. The corresponding
documentation should be consulted for more details. Alternatively the architectural HAL header file

hal_intr.h

,

or one of the variant or platform header files it includes, will contain appropriate definitions. The details of how to
handle exceptions, including whether or not it is possible to recover from them, also depend on the hardware.

Exception

handling

is

optional,

and

can

be

disabled

through

the

configuration

option

CYGPKG_KERNEL_EXCEPTIONS

. If an application has been exhaustively tested and is trusted never to raise

a hardware exception then this option can be disabled and code and data sizes will be reduced somewhat.
If exceptions are left enabled then the system will provide default handlers for the various exceptions, but
these do nothing. Even the specific type of exception is ignored, so there is no point in attempting to
decode this and distinguish between say a divide-by-zero and an unaligned access. If the application installs
its own handlers and wants details of the specific exception being raised then the configuration option

CYGSEM_KERNEL_EXCEPTIONS_DECODE

has to be enabled.

An alternative handler can be installed using

cyg_exception_set_handler

. This requires a code for the excep-

tion, a function pointer for the new exception handler, and a parameter to be passed to this handler. Details of the
previously installed exception handler will be returned via the remaining two arguments, allowing that handler to
be reinstated, or null pointers can be used if this information is of no interest. An exception handling function
should take the following form:

void

my_exception_handler(cyg_addrword_t data, cyg_code_t exception, cyg_addrword_t info)

57

Advertising