Skip to content

Commit 8c263e6

Browse files
author
Amanda Butler
authored
Merge pull request #831 from SenRamakri/sen_CrashReportingDocs
Crash reporting documentation
2 parents f33f11a + 6a0530a commit 8c263e6

File tree

2 files changed

+199
-61
lines changed

2 files changed

+199
-61
lines changed

docs/api/platform/Error.md

Lines changed: 194 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<h2 id="error-handling">Error handling</h2>
22

3-
Mbed OS provides error code and error status definitions and error handling APIs for error construction, reporting and retrieving previously reported errors. Mbed OS also provides functions and macros to generate and define new error status values, extract information from error status values and to report errors into the system. Any software layer, such as applications, drivers, HAL and protocol stacks, can use these error handling APIs. The error functions also facilitate emitting an error message through STDOUT. `mbed_error.h` declares the error functions that Mbed OS provides.
3+
Mbed OS provides error status definitions and APIs for error construction, reporting and retrieving previously reported errors. Mbed OS also provides functions and macros to generate and define new error status values, extract information from error status values and to report errors into the system. Any software layer, such as applications, drivers, HAL and protocol stacks, can use these error handling APIs. The error functions also facilitate emitting an error message through STDOUT. `mbed_error.h` declares the error functions that Mbed OS provides.
44

55
Conceptually, error handling is a platform service that the Mbed OS platform layer implements. Error handling provides the following:
66

@@ -94,7 +94,7 @@ Error Message: Fault exception
9494
Location: 0x5CD1
9595
Error Value: 0x4A2A
9696
Current Thread: Id: 0x20001E80 Entry: 0x5EB1 StackSize: 0x1000 StackMem: 0x20000E80 SP: 0x2002FF90
97-
For more info, visit: https://mbed.com/s/error?error=0x80FF013D&mbedos=999999&core=0x410FC241&compile=1&ver=5060528
97+
For more info, visit: https://mbed.com/s/error?error=0x80FF013D
9898
-- MbedOS Error Info --
9999
```
100100

@@ -130,6 +130,45 @@ Mbed OS application and system developers may need to define error codes specifi
130130

131131
Some applications may want to do custom error handling when an error is reported using `MBED_ERROR()` or `MBED_WARNING()`. Applications can accomplish this by registering an error hook function with the Mbed OS error handling system using the **mbed_set_error_hook()** API. This function is called with error context information whenever the system handles an **MBED_ERROR()** or **MBED_WARNING()** invocation. This function should be implemented for re-entrancy because multiple threads may invoke `MBED_ERROR()` or `MBED_WARNING()`, which may cause the error hook to be called in parallel.
132132

133+
### Crash reporting and auto-reboot
134+
135+
Whenever a fatal error happens in the system, the Mbed OS error handling system collects key information such as error code, error location, register context (in the case of fault exceptions) and so on. The error handing system stores that information in a reserved RAM region called Crash-data-RAM. The error information stored in Crash-data-RAM is in binary format and follows the `mbed_error_ctx` structure defined in `mbed_error.h`. The system then triggers a warm-reset without losing the RAM contents that store the error information. After the system reboots, during Mbed OS initialization, the Crash-data-RAM region is checked to find if there is valid error information captured. This is done by using a CRC value calculated over the stored error information and is appended as part of information stored in Crash-data-RAM. If the system detects that the reboot was triggered by a fatal error, it will invoke a callback function with a pointer to the error context structure stored in Crash-data-RAM. The default callback function is defined with the `WEAK` attribute, which the application can override. Below is the signature for the callback:
136+
137+
```void mbed_error_reboot_callback(mbed_error_ctx *error_context);```
138+
139+
<span class="notes">**Note:** This callback is invoked before the system starts executing the application `main()`. The implementation of callback should be aware any resource limitations or availability. Also, the callback is invoked only when there is a new error.</span>
140+
141+
#### Adding the Crash-data-RAM region for crash reporting
142+
143+
The crash reporting feature requires a special memory region, called Crash-data-RAM, to work. This region is 256 bytes and is allocated using linker scripts for the target for each toolchain. Although all platforms support crash reporting feature, not all targets are currently modified to allocate this Crash-data-RAM region.
144+
145+
See `mbed_lib.json` in the platform directory to see which targets are currently enabled with crash reporting. To enable crash reporting in other targets, you must modify the linker scripts for those targets to allocate the Crash-data-RAM region. You can refer to the linker scripts for one of the targets already enabled with crash reporting to understand how the Crash-data-RAM region is allocated. Below are some guidelines to make the linker script changes:
146+
147+
- The region size should be 256 bytes and aligned at 8-byte offset.
148+
- If you are enabling the Crash-data-RAM for the *ARM compiler*, linker scripts must export the following symbols:
149+
150+
__Image$$RW_m_crash_data$$ZI$$Base__ - Indicates start address of Crash-data-RAM region.
151+
__Image$$RW_m_crash_data$$ZI$$Size__ - Indicates size of Crash-data-RAM region.
152+
153+
- If you are enabling the Crash-data-RAM for the *GCC ARM compiler* or *IAR Compiler*, linker scripts must export the following symbols:
154+
155+
__\_\_CRASH_DATA_RAM_START\_\___ - Indicates start address of Crash-data-RAM region.
156+
__\_\_CRASH_DATA_RAM_END\_\___ - Indicates end address of Crash-data-RAM region.
157+
158+
It's important that this region is marked with the appropriate attributes (based on the toolchain) to mark it as an uninitialized region. For example, you can mark the ARM Compiler Crash-data-RAM with the attribute *EMPTY*. The only requirement about the placement of this region is that no other entity can overwrite this region during reboot or at runtime. However, to avoid fragmentation, it's best if you place this region just after the vector table region, or if there is no vector table region, iat the bottom of RAM (lowest address).
159+
160+
See [memory model](memory.html) for more info on the placement of this region.
161+
162+
#### Configuring crash reporting and autoreboot
163+
164+
The Mbed OS crash reporting implementation provides many options to configure the crash reporting behavior. Below is the list of configuration options available to configure crash reporting functionality. These configuration options are defined in `mbed_lib.json` under the platform directory:
165+
166+
- `crash-capture-enabled` - Enables crash context capture when the system enters a fatal error or crash. When this is disabled, it also disables other dependent options mentioned below.
167+
- `fatal-error-auto-reboot-enabled` - Setting this to true enables autoreboot on fatal errors.
168+
- `error-reboot-max` - Maximum number of auto-reboots(warm-resets) permitted on fatal errors. The system stop auto-rebooting once it reaches the maximum limit. Setting this value to 0 disable auto-reboot.
169+
170+
Crash reporting feature also provides APIs to read and clear error context information captured in Crash-data-RAM region. Please see the API reference below for [crash reporting related APIs](#crash-reporting-api).
171+
133172
### Error handling functions reference
134173

135174
The below link provides the documentation for all the APIs that Mbed OS provides for error definitions and handling:
@@ -298,12 +337,106 @@ void save_all_errors() {
298337
mbed_clear_all_errors();
299338
}
300339
```
340+
341+
#### Using `mbed_get_reboot_error_info()` to retrieve the reboot error info
342+
343+
In the example below, status variable `reboot_error_detected` is set to 1 when the callback is invoked. Then, the `main()`
344+
function reads the reboot error information using `mbed_get_reboot_error_info()`.
345+
346+
```CPP TODO
347+
mbed_error_ctx error_ctx;
348+
int reboot_error_detected = 0;
349+
350+
//Callback during reboot
351+
void mbed_error_reboot_callback(mbed_error_ctx *error_context)
352+
{
353+
printf("error callback received");
354+
reboot_error_detected = 1;
355+
}
356+
357+
// main() runs in its own thread in the OS
358+
int main()
359+
{
360+
if (reboot_error_detected == 1) {
361+
if (MBED_SUCCESS == mbed_get_reboot_error_info(&error_ctx)) {
362+
printf("\nSuccessfully read error context\n");
363+
}
364+
}
365+
//main continues...
366+
}
367+
```
368+
369+
#### Using `mbed_get_reboot_fault_context()` to retrieve the fault context info
370+
371+
The example code below checks for the exception error (`MBED_ERROR_HARDFAULT_EXCEPTION`) using `error_status` in the error context
372+
and then retrieves the fault context using `mbed_get_reboot_fault_context()`:
373+
374+
```CPP TODO
375+
mbed_error_ctx error_ctx;
376+
mbed_fault_context_t fault_ctx;
377+
int reboot_error_detected = 0;
378+
379+
//Callback during reboot
380+
void mbed_error_reboot_callback(mbed_error_ctx * error_context)
381+
{
382+
printf("error callback received");
383+
reboot_error_detected = 1;
384+
}
385+
386+
// main() runs in its own thread in the OS
387+
int main()
388+
{
389+
if (reboot_error_detected == 1) {
390+
if (MBED_SUCCESS == mbed_get_reboot_error_info(&error_ctx)) {
391+
printf("\nRead in reboot info\n");
392+
if (error_ctx.error_status == MBED_ERROR_HARDFAULT_EXCEPTION) {
393+
if (MBED_SUCCESS == mbed_get_reboot_fault_context(&fault_ctx)) {
394+
printf("\nRead in fault context info\n");
395+
}
396+
}
397+
}
398+
}
399+
//main continues...
400+
}
401+
```
402+
403+
#### Using `mbed_reset_reboot_error_info()` to clear the reboot error info
404+
405+
You can use `mbed_reset_reboot_error_info()` to clear the reboot error information:
406+
407+
```CPP TODO
408+
void clear_reboot_errors() {
409+
410+
//Clear the currently stored error info in Crash-data-RAM
411+
mbed_reset_reboot_error_info();
412+
}
413+
```
414+
415+
#### Using `mbed_reset_reboot_count()` to reset the reboot count
416+
417+
You can use `mbed_reset_reboot_error_info()` to specifically reset the reboot count stored in Crash-data-RAM. Calling this function sets the reboot count to 0:
418+
419+
```CPP TODO
420+
void clear_reboot_count() {
421+
422+
//Clear the currently stored error info in Crash-data-RAM
423+
mbed_reset_reboot_count();
424+
}
425+
```
426+
301427
### Error handling example
302428

303429
The example application below demonstrates usage of error handling APIs:
304430

305431
[![View code](https://www.mbed.com/embed/?url=https://github.com/ARMmbed/mbed-os-example-error-handling)](https://github.com/ARMmbed/mbed-os-example-error-handling/blob/mbed-os-5.10.4/main.cpp)
306432

433+
### Crash reporting example
434+
435+
The example application below demonstrates the crash reporting feature:
436+
437+
[![View code](https://www.mbed.com/embed/?url=https://github.com/ARMmbed/mbed-os-example-crash-reporting)](https://github.com/ARMmbed/mbed-os-example-crash-reporting/blob/master/main.cpp)
438+
439+
307440
### List of Mbed OS defined error codes and descriptions
308441

309442
Below are the predefined Mbed system error codes and their descriptions:
@@ -313,65 +446,65 @@ Below are the predefined Mbed system error codes and their descriptions:
313446
MBED_ERROR_CODE_INVALID_DATA Invalid data
314447
MBED_ERROR_CODE_INVALID_FORMAT Invalid format
315448
MBED_ERROR_CODE_INVALID_INDEX Invalid Index
316-
MBED_ERROR_CODE_INVALID_SIZE Inavlid Size
317-
MBED_ERROR_CODE_INVALID_OPERATION Invalid Operation
318-
MBED_ERROR_CODE_NOT_FOUND Not Found
319-
MBED_ERROR_CODE_ACCESS_DENIED Access Denied
320-
MBED_ERROR_CODE_NOT_SUPPORTED Not supported
321-
MBED_ERROR_CODE_BUFFER_FULL Buffer Full
322-
MBED_ERROR_CODE_MEDIA_FULL Media/Disk Full
323-
MBED_ERROR_CODE_ALREADY_IN_USE Already in use
324-
MBED_ERROR_CODE_TIMEOUT Timeout error
325-
MBED_ERROR_CODE_NOT_READY Not Ready
326-
MBED_ERROR_CODE_FAILED_OPERATION Requested Operation failed
327-
MBED_ERROR_CODE_OPERATION_PROHIBITED Operation prohibited
328-
MBED_ERROR_CODE_OPERATION_ABORTED Operation failed
329-
MBED_ERROR_CODE_WRITE_PROTECTED Attempt to write to write-protected resource
330-
MBED_ERROR_CODE_NO_RESPONSE No response
331-
MBED_ERROR_CODE_SEMAPHORE_LOCK_FAILED Semaphore lock failed
332-
MBED_ERROR_CODE_MUTEX_LOCK_FAILED Mutex lock failed
333-
MBED_ERROR_CODE_SEMAPHORE_UNLOCK_FAILED Semaphore unlock failed
334-
MBED_ERROR_CODE_MUTEX_UNLOCK_FAILED Mutex unlock failed
335-
MBED_ERROR_CODE_CRC_ERROR CRC error or mismatch
336-
MBED_ERROR_CODE_OPEN_FAILED Open failed
337-
MBED_ERROR_CODE_CLOSE_FAILED Close failed
338-
MBED_ERROR_CODE_READ_FAILED Read failed
339-
MBED_ERROR_CODE_WRITE_FAILED Write failed
340-
MBED_ERROR_CODE_INITIALIZATION_FAILED Initialization failed
341-
MBED_ERROR_CODE_BOOT_FAILURE Boot failure
342-
MBED_ERROR_CODE_OUT_OF_MEMORY Out of memory
343-
MBED_ERROR_CODE_OUT_OF_RESOURCES Out of resources
344-
MBED_ERROR_CODE_ALLOC_FAILED Alloc failed
345-
MBED_ERROR_CODE_FREE_FAILED Free failed
346-
MBED_ERROR_CODE_OVERFLOW Overflow error
347-
MBED_ERROR_CODE_UNDERFLOW Underflow error
348-
MBED_ERROR_CODE_STACK_OVERFLOW Stack overflow error
349-
MBED_ERROR_CODE_ISR_QUEUE_OVERFLOW ISR queue overflow
350-
MBED_ERROR_CODE_TIMER_QUEUE_OVERFLOW Timer Queue overflow
351-
MBED_ERROR_CODE_CLIB_SPACE_UNAVAILABLE Standard library error - Space unavailable
352-
MBED_ERROR_CODE_CLIB_EXCEPTION Standard library error - Exception
353-
MBED_ERROR_CODE_CLIB_MUTEX_INIT_FAILURE Standard library error - Mutex Init failure
354-
MBED_ERROR_CODE_CREATE_FAILED Create failed
355-
MBED_ERROR_CODE_DELETE_FAILED Delete failed
356-
MBED_ERROR_CODE_THREAD_CREATE_FAILED Thread Create failed
357-
MBED_ERROR_CODE_THREAD_DELETE_FAILED Thread Delete failed
358-
MBED_ERROR_CODE_PROHIBITED_IN_ISR_CONTEXT Operation Prohibited in ISR context
359-
MBED_ERROR_CODE_PINMAP_INVALID Pinmap Invalid
360-
MBED_ERROR_CODE_RTOS_EVENT Unknown Rtos Error
361-
MBED_ERROR_CODE_RTOS_THREAD_EVENT Rtos Thread Error
362-
MBED_ERROR_CODE_RTOS_MUTEX_EVENT Rtos Mutex Error
363-
MBED_ERROR_CODE_RTOS_SEMAPHORE_EVENT Rtos Semaphore Error
364-
MBED_ERROR_CODE_RTOS_MEMORY_POOL_EVENT Rtos Memory Pool Error
365-
MBED_ERROR_CODE_RTOS_TIMER_EVENT Rtos Timer Error
366-
MBED_ERROR_CODE_RTOS_EVENT_FLAGS_EVENT Rtos Event flags Error
367-
MBED_ERROR_CODE_RTOS_MESSAGE_QUEUE_EVENT Rtos Message queue Error
368-
MBED_ERROR_CODE_DEVICE_BUSY Device Busy
369-
MBED_ERROR_CODE_CONFIG_UNSUPPORTED Configuration not supported
370-
MBED_ERROR_CODE_CONFIG_MISMATCH Configuration mismatch
371-
MBED_ERROR_CODE_ALREADY_INITIALIZED Already initialized
372-
MBED_ERROR_CODE_HARDFAULT_EXCEPTION HardFault exception
373-
MBED_ERROR_CODE_MEMMANAGE_EXCEPTION MemManage exception
374-
MBED_ERROR_CODE_BUSFAULT_EXCEPTION BusFault exception
449+
MBED_ERROR_CODE_INVALID_SIZE Invalid Size
450+
MBED_ERROR_CODE_INVALID_OPERATION Invalid Operation
451+
MBED_ERROR_CODE_NOT_FOUND Not Found
452+
MBED_ERROR_CODE_ACCESS_DENIED Access Denied
453+
MBED_ERROR_CODE_NOT_SUPPORTED Not supported
454+
MBED_ERROR_CODE_BUFFER_FULL Buffer Full
455+
MBED_ERROR_CODE_MEDIA_FULL Media/Disk Full
456+
MBED_ERROR_CODE_ALREADY_IN_USE Already in use
457+
MBED_ERROR_CODE_TIMEOUT Timeout error
458+
MBED_ERROR_CODE_NOT_READY Not Ready
459+
MBED_ERROR_CODE_FAILED_OPERATION Requested Operation failed
460+
MBED_ERROR_CODE_OPERATION_PROHIBITED Operation prohibited
461+
MBED_ERROR_CODE_OPERATION_ABORTED Operation failed
462+
MBED_ERROR_CODE_WRITE_PROTECTED Attempt to write to write-protected resource
463+
MBED_ERROR_CODE_NO_RESPONSE No response
464+
MBED_ERROR_CODE_SEMAPHORE_LOCK_FAILED Semaphore lock failed
465+
MBED_ERROR_CODE_MUTEX_LOCK_FAILED Mutex lock failed
466+
MBED_ERROR_CODE_SEMAPHORE_UNLOCK_FAILED Semaphore unlock failed
467+
MBED_ERROR_CODE_MUTEX_UNLOCK_FAILED Mutex unlock failed
468+
MBED_ERROR_CODE_CRC_ERROR CRC error or mismatch
469+
MBED_ERROR_CODE_OPEN_FAILED Open failed
470+
MBED_ERROR_CODE_CLOSE_FAILED Close failed
471+
MBED_ERROR_CODE_READ_FAILED Read failed
472+
MBED_ERROR_CODE_WRITE_FAILED Write failed
473+
MBED_ERROR_CODE_INITIALIZATION_FAILED Initialization failed
474+
MBED_ERROR_CODE_BOOT_FAILURE Boot failure
475+
MBED_ERROR_CODE_OUT_OF_MEMORY Out of memory
476+
MBED_ERROR_CODE_OUT_OF_RESOURCES Out of resources
477+
MBED_ERROR_CODE_ALLOC_FAILED Alloc failed
478+
MBED_ERROR_CODE_FREE_FAILED Free failed
479+
MBED_ERROR_CODE_OVERFLOW Overflow error
480+
MBED_ERROR_CODE_UNDERFLOW Underflow error
481+
MBED_ERROR_CODE_STACK_OVERFLOW Stack overflow error
482+
MBED_ERROR_CODE_ISR_QUEUE_OVERFLOW ISR queue overflow
483+
MBED_ERROR_CODE_TIMER_QUEUE_OVERFLOW Timer Queue overflow
484+
MBED_ERROR_CODE_CLIB_SPACE_UNAVAILABLE Standard library error - Space unavailable
485+
MBED_ERROR_CODE_CLIB_EXCEPTION Standard library error - Exception
486+
MBED_ERROR_CODE_CLIB_MUTEX_INIT_FAILURE Standard library error - Mutex Init failure
487+
MBED_ERROR_CODE_CREATE_FAILED Create failed
488+
MBED_ERROR_CODE_DELETE_FAILED Delete failed
489+
MBED_ERROR_CODE_THREAD_CREATE_FAILED Thread Create failed
490+
MBED_ERROR_CODE_THREAD_DELETE_FAILED Thread Delete failed
491+
MBED_ERROR_CODE_PROHIBITED_IN_ISR_CONTEXT Operation Prohibited in ISR context
492+
MBED_ERROR_CODE_PINMAP_INVALID Pinmap Invalid
493+
MBED_ERROR_CODE_RTOS_EVENT Unknown Rtos Error
494+
MBED_ERROR_CODE_RTOS_THREAD_EVENT Rtos Thread Error
495+
MBED_ERROR_CODE_RTOS_MUTEX_EVENT Rtos Mutex Error
496+
MBED_ERROR_CODE_RTOS_SEMAPHORE_EVENT Rtos Semaphore Error
497+
MBED_ERROR_CODE_RTOS_MEMORY_POOL_EVENT Rtos Memory Pool Error
498+
MBED_ERROR_CODE_RTOS_TIMER_EVENT Rtos Timer Error
499+
MBED_ERROR_CODE_RTOS_EVENT_FLAGS_EVENT Rtos Event flags Error
500+
MBED_ERROR_CODE_RTOS_MESSAGE_QUEUE_EVENT Rtos Message queue Error
501+
MBED_ERROR_CODE_DEVICE_BUSY Device Busy
502+
MBED_ERROR_CODE_CONFIG_UNSUPPORTED Configuration not supported
503+
MBED_ERROR_CODE_CONFIG_MISMATCH Configuration mismatch
504+
MBED_ERROR_CODE_ALREADY_INITIALIZED Already initialized
505+
MBED_ERROR_CODE_HARDFAULT_EXCEPTION HardFault exception
506+
MBED_ERROR_CODE_MEMMANAGE_EXCEPTION MemManage exception
507+
MBED_ERROR_CODE_BUSFAULT_EXCEPTION BusFault exception
375508
MBED_ERROR_CODE_USAGEFAULT_EXCEPTION UsageFault exception
376509
MBED_ERROR_CODE_BLE_NO_FRAME_INITIALIZED BLE No frame initialized
377510
MBED_ERROR_CODE_BLE_BACKEND_CREATION_FAILED BLE Backend creation failed

0 commit comments

Comments
 (0)