The Error module provides facilities for handling errors, but the Log
module also provides features for logging error events. These are separate
concepts; however, to ensure that users do not have to both raise and log
an error, the Error module will automatically log an error event when one
is raised. The Error module logs the standard
Log.L_error event,
passing it the error message and arguments.
The error event is logged to the Error module's logger using the Error
module's diags mask. Logging of errors is enabled by default in the diags
mask, but the event will not be logged unless a logger is configured for
the Error module as well.
To make the error event appear as though it is coming from the module which
called Error_raise, the event is logged with the caller's module id and
with the caller's call site information.
Example 1: The following example shows how a module, named ModA,
defines a custom error type and shows how this error is raised by
the module. The module defines an Id of E_notEven in its module
specification file (in this case, ModA.xdc). The error's message
string takes only one argument. The module also defines a mayFail()
function that takes an error block. In the module's C source file,
the function checks for the error condition and raises the error if
needed.
Example 2: The following C code supplies an error block to a function
that requires one and tests the error block to see if the function
raised an error. Note that an error block must be initialized before
it can be used and same error block may be passed to several functions.
#include <xdc/runtime/Error.h>
Void myFunc(..., Error_Block *eb)
{
...
if (...error condition detected...) {
String myErrorMsg = "my custom error message";
Error_raise(eb, Error_E_generic, myErrorMsg, 0);
...add error handling code here...
return;
}
}
const Error_NUMARGS |
|
Maximum number of arguments supported by an error
#define Error_NUMARGS (Int)2
enum Error_Policy |
|
Error handling policies
typedef enum Error_Policy {
Error_TERMINATE,
Error_UNWIND
} Error_Policy;
VALUES
TERMINATE
All raised errors are fatal. A call to
Error_raise will never return to the caller.
UNWIND
Errors are returned to the caller. A call to
Error_raise will return back to the caller.
DETAILS
Regardless of the current policy in use, raising an error by
calling
Error_raise will always invoke the
error raise hook function assigned to the
Error.raiseHook configuration parameter.
typedef Error_HookFxn |
|
Function called whenever an error is raised
DETAILS
The first parameter and only parameter passed to this function is a
pointer to an
Error_Block. Even if the client passes a
NULL error
block pointer to
Error_raise, this parameter passed
to this "hook" function is always
non-NULL.
typedef Error_Id |
|
Error identifier
DETAILS
Each type of error raised is defined with a metaonly
Error.Desc. An
Error_Id is a 32-bit target value that
encodes the information in the
Desc. Target programs use
Error_Id values to "raise" and check for specific errors.
WARNING
Id values may vary among different
configurations of an application. For example, the addition of a
new module to a program may result in a different absolute value for
E_generic. If you need error numbers that remain
invariant, use the user definable
Desc.code field.
struct Error_Block |
|
Error block
typedef struct Error_Block Error_Block;
DETAILS
An opaque structure used to store information about errors once raised.
This structure must be initialized via
Error_init()
before being used for the first time.
struct Error_Data |
|
Error args
typedef struct Error_Data {
} Error_Data;
DETAILS
The two arguments (arg1, arg2) passed to
raise are
stored in one of these arrays within the associated Error_Block.
To access these arguments use
getData to obtain a
pointer to the Error_Block's Data array.
SEE
config Error_E_generic // module-wide |
|
Generic error
DETAILS
This error takes advantage of the $S specifier to allow for recursive
formatting of the error message passed to error raise.
For example, the following is possible:
Error_raise(eb, Error_E_generic, "Error occurred, code: %d", code);
SEE
config Error_E_memory // module-wide |
|
Out of memory error
DETAILS
The first parameter must be the heap instance handle. The second
parameter is the size of the object for which the allocation failed.
config Error_E_msgCode // module-wide |
|
Generic error that displays a string and a numeric value
config Error_maxDepth // module-wide |
|
Maximum number of concurrent calls to raiseHook
extern const UInt16 Error_maxDepth;
DETAILS
To prevent errors that occur in the raiseHook function from
causing an infinite recursion, the maximum number of concurrent
calls to
raiseHook is limited by
Error_maxDepth. If
the number of concurrent calls exceeds
Error_maxDepth, the
raiseHook function is not called.
In multi-threaded systems, errors raised by separate threads may
be detected as recursive calls to raiseHook. So, setting
Error.maxDepth to a small value may - in rare instances - result in
errorHook not being called for some raised errors.
If it is important that all raised errors trigger a call to the
raiseHook function, set Error.maxDepth to an impossibly large
number (0xffff) and either ensure that the raise hook never calls a
function that can raise an error or add checks in raiseHook to
protect against "double faults".
config Error_policy // module-wide |
|
System-wide error handling policy
config Error_raiseHook // module-wide |
|
The function to call whenever an error is raised
DETAILS
This function is always called when an error is raised, even if the
Error policy is
TERMINATE. In rare cases, it is
possible that a raised error does not trigger a call to
raiseHook;
see
maxDepth.
By default, this function is set to
Error_print
which causes the error to be formatted and output via
System_printf.
SEE
Error_check() // module-wide |
|
Return TRUE if an error was raised
ARGUMENTS
eb
pointer to an Error_Block or NULL
RETURNS
If
eb is non-
NULL and
Error.policy == UNWIND and
an error was raised on
eb, this function returns
TRUE. Otherwise,
it returns
FALSE.
Error_getCode() // module-wide |
|
Get an error's code
ARGUMENTS
eb
non-NULL pointer to an Error_Block
RETURNS
getCode returns the error code associated with this error block.
SEE
Error_getData() // module-wide |
|
Get an error's argument list
ARGUMENTS
eb
non-NULL pointer to an Error_Block
RETURNS
getData returns an array of type
Data with
NUMARGS elements containing the arguments provided
at the time the error was raised.
SEE
Error_getId() // module-wide |
|
Get an error's id
ARGUMENTS
eb
non-NULL pointer to an Error_Block
WARNING
Error_Id values may vary among different configurations
of an application. For example, the addition of a new module to a
program may result in a different absolute value for
E_generic. If you need error numbers that remain
invariant, use the user definable
Desc.code field.
SEE
Error_getMsg() // module-wide |
|
Get an error's "printf" format string
ARGUMENTS
eb
non-NULL pointer to an Error_Block
SEE
Error_getSite() // module-wide |
|
Get an error's call site info
ARGUMENTS
eb
non-NULL pointer to an Error_Block
RETURNS
getSite returns a pointer to an initialized
Types.Site structure. However, in the
event that the call site was compiled with
xdc_FILE defined to
be
NULL (to minimize string space overhead) the
file
field may be set to
NULL.
SEE
Error_init() // module-wide |
|
Put an error block into its initial state
ARGUMENTS
eb
pointer to an Error_Block or NULL
If eb is NULL this function simply returns.
DETAILS
To ensure reliable error detection, clients must call init for
an Error_Block prior to any use.
If the same Error Block is used multiple times, only the last error
raised is retained.
Error_print() // module-wide |
|
Print error using System.printf()
ARGUMENTS
eb
pointer to an Error_Block or NULL
If eb is NULL this function simply returns with no output.
DETAILS
This function prints the error using System_printf(). The output
is on a single line terminated with a new line character and has the
following form:
<site>: <file>, line <line_num>: <err_msg>
where <site> is the module that raised the error, <file> and
<line_num> are the file and line number of the containing the call
site of the Error_raise(), and <err_msg> is the error message
rendered with the arguments associated with the error.
WARNING
This function is not protected by a gate and, as a result,
if two threads call this method concurrently, the output of the two
calls will be intermingled. To prevent intermingled error output,
you can either wrap all calls to this method with an appropriate
Gate_enter/Gate_leave pair or simply ensure that only one
thread in the system ever calls this method.
Error_raise() // module-wide |
|
Raise an error
ARGUMENTS
eb
pointer to an Error_Block or NULL
If
eb is
NULL or
Error.policy == TERMINATE,
this function does not return to the caller; after calling any
configured
raiseHook,
System_abort is called with the
string
"xdc.runtime.Error.raise: terminating execution\n".
id
the error to raise
This pointer identifies the class of error being raised;
the error class indicates how to interpret any subsequent
arguments passed to
raise.
arg1
error's first argument
The argument interpreted by the first control character
in the error message format string. It is ignored if not needed.
arg2
error's second argument
The argument interpreted by the second control character
in the error message format string. It is ignored if not needed.
DETAILS
This function is used to raise an Error by writing call site,
error ID, and error argument information into the Error_Block
pointed to by eb.
If Error_raise is called more than once on an Error_Block object,
the previous error information is overwritten; only the last error
is retained in the Error_Block object.
In all cases, any configured
Error.raiseHook
function is called with a non-
NULL pointer to a fully
initialized
Error_Block object.
Module-Wide Built-Ins |
|
// Get this module's unique id
Bool Error_Module_startupDone();
// Test if this module has completed startup
// The heap from which this module allocates memory
Bool Error_Module_hasMask();
// Test whether this module has a diagnostics mask
Bits16 Error_Module_getMask();
// Returns the diagnostics mask for this module
Void Error_Module_setMask(Bits16 mask);
// Set the diagnostics mask for this module
const Error.NUMARGS |
|
Maximum number of arguments supported by an error
XDCscript usage |
meta-domain |
C SYNOPSIS
enum Error.Policy |
|
Error handling policies
XDCscript usage |
meta-domain |
values of type Error.Policy
const Error.TERMINATE;
const Error.UNWIND;
VALUES
TERMINATE
All raised errors are fatal. A call to
Error_raise will never return to the caller.
UNWIND
Errors are returned to the caller. A call to
Error_raise will return back to the caller.
DETAILS
Regardless of the current policy in use, raising an error by
calling
Error_raise will always invoke the
error raise hook function assigned to the
Error.raiseHook configuration parameter.
C SYNOPSIS
struct Error.Data |
|
Error args
XDCscript usage |
meta-domain |
var obj = new Error.Data;
DETAILS
The two arguments (arg1, arg2) passed to
raise are
stored in one of these arrays within the associated Error_Block.
To access these arguments use
getData to obtain a
pointer to the Error_Block's Data array.
SEE
C SYNOPSIS
metaonly struct Error.Desc |
|
Error descriptor
XDCscript usage |
meta-domain |
var obj = new Error.Desc;
obj.msg = String ...
obj.code = UInt16 ...
FIELDS
msg
The error message using a printf style format string,
but limited to NUMARGS arguments.
This format string together with the two arguments passed
to Error_raise` are used to create a human readable
error message.
code
A user assignable code, 0 by default. The user may
optionally set this field during config to give the
error a well-known numeric code.
DETAILS
Each type of error is defined with an error descriptor. This
structure groups common information about the errors of this type.
config Error.E_generic // module-wide |
|
Generic error
XDCscript usage |
meta-domain |
DETAILS
This error takes advantage of the $S specifier to allow for recursive
formatting of the error message passed to error raise.
For example, the following is possible:
Error_raise(eb, Error_E_generic, "Error occurred, code: %d", code);
SEE
C SYNOPSIS
config Error.E_memory // module-wide |
|
Out of memory error
XDCscript usage |
meta-domain |
msg: "out of memory: heap=0x%x, size=%u"
};
DETAILS
The first parameter must be the heap instance handle. The second
parameter is the size of the object for which the allocation failed.
C SYNOPSIS
config Error.E_msgCode // module-wide |
|
Generic error that displays a string and a numeric value
XDCscript usage |
meta-domain |
C SYNOPSIS
config Error.maxDepth // module-wide |
|
Maximum number of concurrent calls to raiseHook
XDCscript usage |
meta-domain |
Error.maxDepth = UInt16 16;
DETAILS
To prevent errors that occur in the raiseHook function from
causing an infinite recursion, the maximum number of concurrent
calls to
raiseHook is limited by
Error_maxDepth. If
the number of concurrent calls exceeds
Error_maxDepth, the
raiseHook function is not called.
In multi-threaded systems, errors raised by separate threads may
be detected as recursive calls to raiseHook. So, setting
Error.maxDepth to a small value may - in rare instances - result in
errorHook not being called for some raised errors.
If it is important that all raised errors trigger a call to the
raiseHook function, set Error.maxDepth to an impossibly large
number (0xffff) and either ensure that the raise hook never calls a
function that can raise an error or add checks in raiseHook to
protect against "double faults".
C SYNOPSIS
config Error.policy // module-wide |
|
System-wide error handling policy
XDCscript usage |
meta-domain |
C SYNOPSIS
config Error.raiseHook // module-wide |
|
The function to call whenever an error is raised
XDCscript usage |
meta-domain |
DETAILS
This function is always called when an error is raised, even if the
Error policy is
TERMINATE. In rare cases, it is
possible that a raised error does not trigger a call to
raiseHook;
see
maxDepth.
By default, this function is set to
Error_print
which causes the error to be formatted and output via
System_printf.
SEE
C SYNOPSIS
metaonly config Error.common$ // module-wide |
|
Common module configuration parameters
XDCscript usage |
meta-domain |
DETAILS
All modules have this configuration parameter. Its name
contains the '$' character to ensure it does not conflict with
configuration parameters declared by the module. This allows
new configuration parameters to be added in the future without
any chance of breaking existing modules.