Skip to content

Commit 6715706

Browse files
committed
Crash reporting documentation
1 parent e9ebe9d commit 6715706

File tree

2 files changed

+125
-2
lines changed

2 files changed

+125
-2
lines changed

docs/api/platform/Error.md

Lines changed: 120 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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,37 @@ 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+
Whenever a fatal error happens in the system, MbedOS error handling system collects key information such as error code, error location, register context(in the case of fault exceptions) etc. and stores them in a special area in 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 where we have the error information collected. After the system reboots, during MbedOS 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 `WEAK` attribute, which can be overridden by the application if required. Below is the signature for the callback:
135+
136+
```void mbed_error_reboot_callback(mbed_error_ctx *error_context);```
137+
138+
Note that this callback will be invoked before the system starts executing application `main()`. So the implementation of callback should be aware any resource limitations or availability of resources which are yet to be initialized by application `main()`. Also note that the callback is invoked only when there is a new error.
139+
140+
#### Adding Crash-data-RAM region for crash reporting
141+
As mentioned above, the crash reporting feature requires a special memory region, called Crash-data-RAM to work. This region is 256 bytes in size 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.
142+
See `mbed_lib.json` in platform directory to see which targets currently enabled with crash reporting. In order to enable crash reporting in other targets, you must modify the linker scripts for those targets to allocate the Crash-data-RAM region. You may refer 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.
143+
144+
* The region size should be 256 bytes and aligned at 8-byte offset.
145+
* If you are enabling the Crash-data-RAM for *ARM compiler*, linker script must export the following symbols:
146+
__Image$$RW_m_crash_data$$ZI$$Base__ - Indicates start address of Crash-data-RAM region.
147+
__Image$$RW_m_crash_data$$ZI$$Size__ - Indicates size of Crash-data-RAM region.
148+
* If you are enabling the Crash-data-RAM for *GCC ARM compiler* or *IAR Compiler*, linker script must export the following symbols:
149+
__\_\_CRASH_DATA_RAM_START\_\___ - Indicates start address of Crash-data-RAM region.
150+
__\_\_CRASH_DATA_RAM_END\_\___ - Indicates end address of Crash-data-RAM region.
151+
152+
It's important that these regions should be marked with appropriate attributes(based on toolchain) to mark them as uninitialized region. For example, for ARM Compiler Crash-data-RAM can be marked with attribute *EMPTY*. There is no hard requirement about the placement of this region. The only requirement is that it should be placed such that no other entity is overwriting this region when rebooted or at runtime. But in order to avoid fragmentation its best placed just after the vector table region, or if there is no vector table region, it can be placed at the bottom of RAM(lowest address).
153+
See [memory model](memory.html) for more info on the placement of this region.
154+
155+
#### Configuring crash reporting and auto-reboot
156+
MbedOS crash reporting implementation provides many options to configure the crash reporting behavior.
157+
Below is the list of new configuration options available to configure crash reporting functionality. These configuration options are defined in `mbed_lib.json` under platform directory.
158+
159+
`crash-capture-enabled` - Enables crash context capture when the system enters a fatal error/crash. When this is disabled it also disables other dependent options mentioned below.
160+
`fatal-error-auto-reboot-enabled` - Setting this to true enables auto-reboot on fatal errors.
161+
`reboot-crash-report-enabled` - Enables crash report printing over terminal when the system reboots after a fatal error. The format of this report is identical to error reporting structure mentioned in [error report](#error-reporting).
162+
`error-reboot-max` - Maximum number of auto-reboots permitted on fatal errors. The system will stop auto-rebooting once the maximum limit is reached. Setting this to value 0 will disable auto-reboot.
163+
133164
### Error handling functions reference
134165

135166
The below link provides the documentation for all the APIs that Mbed OS provides for error definitions and handling:
@@ -298,12 +329,99 @@ void save_all_errors() {
298329
mbed_clear_all_errors();
299330
}
300331
```
332+
333+
#### Using `mbed_get_reboot_error_info()` to retrieve the reboot error info
334+
The error context captured by the error handling system can be retrieved using mbed_get_reboot_error_info() API. See the below code for example usage of that API.
335+
In the example below, a status variable reboot_error_detected has been used to track the presence of error context capture.
336+
337+
```CPP TODO
338+
mbed_error_ctx error_ctx;
339+
int reboot_error_detected = 0;
340+
341+
//Callback during reboot
342+
void mbed_error_reboot_callback(mbed_error_ctx *error_context)
343+
{
344+
printf("error callback received");
345+
reboot_error_detected = 1;
346+
}
347+
348+
// main() runs in its own thread in the OS
349+
int main()
350+
{
351+
if (reboot_error_detected == 1) {
352+
if (MBED_SUCCESS == mbed_get_reboot_error_info(&error_ctx)) {
353+
printf("\nSuccessfully read error context\n");
354+
}
355+
//main continues...
356+
}
357+
```
358+
359+
#### Using `mbed_get_reboot_fault_context()` to retrieve the fault context info
360+
The fault context captured can be retrieved using mbed_get_reboot_fault_context() API. See the below code for example usage of that API. The example code below checks for error_status using the error context and then retrieves the fault context using mbed_get_reboot_fault_context() API.
361+
362+
```CPP TODO
363+
mbed_error_ctx error_ctx;
364+
mbed_fault_context_t fault_ctx;
365+
int reboot_error_detected = 0;
366+
367+
//Callback during reboot
368+
void mbed_error_reboot_callback(mbed_error_ctx * error_context)
369+
{
370+
printf("error callback received");
371+
reboot_error_detected = 1;
372+
}
373+
374+
// main() runs in its own thread in the OS
375+
int main()
376+
{
377+
if (reboot_error_detected == 1) {
378+
if (MBED_SUCCESS == mbed_get_reboot_error_info(&error_ctx)) {
379+
printf("\nRead in reboot info\n");
380+
if (error_ctx.error_status == MBED_ERROR_HARDFAULT_EXCEPTION) {
381+
if (MBED_SUCCESS == mbed_get_reboot_fault_context(&fault_ctx)) {
382+
printf("\nRead in fault context info\n");
383+
}
384+
}
385+
}
386+
}
387+
//main continues...
388+
}
389+
```
390+
391+
#### Using `mbed_reset_reboot_error_info()` to clear the reboot error info
392+
`mbed_reset_reboot_error_info()` API can be used to clear the reboot error info if required by the application.
393+
```CPP TODO
394+
void clear_reboot_errors() {
395+
396+
//Clear the currently stored error info in Crash-data-RAM
397+
mbed_reset_reboot_error_info();
398+
}
399+
```
400+
401+
#### Using `mbed_reset_reboot_count()` to reset the reboot count
402+
`mbed_reset_reboot_error_info()` API can be used to specifically reset the reboot count stored as part error information stored in Crash-data-RAM.
403+
Calling this function will set the reboot count to 0.
404+
```CPP TODO
405+
void clear_reboot_count() {
406+
407+
//Clear the currently stored error info in Crash-data-RAM
408+
mbed_reset_reboot_count();
409+
}
410+
```
411+
301412
### Error handling example
302413

303414
The example application below demonstrates usage of error handling APIs:
304415

305416
[![View code](https://www.mbed.com/embed/?url=https://os.mbed.com/teams/mbed-os-examples/code/mbed-os-example-error-handling/)](https://os.mbed.com/teams/mbed-os-examples/code/mbed-os-example-error-handling/file/9f7dd80f59f9/main.cpp)
306417

418+
### Crash reporting example
419+
420+
The example application below demonstrates crash reporting feature:
421+
422+
[![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)
423+
424+
307425
### List of Mbed OS defined error codes and descriptions
308426

309427
Below are the predefined Mbed system error codes and their descriptions:
@@ -313,7 +431,7 @@ Below are the predefined Mbed system error codes and their descriptions:
313431
MBED_ERROR_CODE_INVALID_DATA Invalid data
314432
MBED_ERROR_CODE_INVALID_FORMAT Invalid format
315433
MBED_ERROR_CODE_INVALID_INDEX Invalid Index
316-
MBED_ERROR_CODE_INVALID_SIZE Inavlid Size
434+
MBED_ERROR_CODE_INVALID_SIZE Invalid Size
317435
MBED_ERROR_CODE_INVALID_OPERATION Invalid Operation
318436
MBED_ERROR_CODE_NOT_FOUND Not Found
319437
MBED_ERROR_CODE_ACCESS_DENIED Access Denied

docs/reference/runtime/Memory.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ This is a basic overview of the memory model in Mbed OS.
3636
| ZI: Global data |
3737
| |
3838
+---------------------+
39+
| Optional |
40+
| Crash-Data-RAM |
41+
| region |
42+
+---------------------+
3943
| RW: Vector table |
4044
+=====================+ First address of RAM
4145
| | Last address of flash
@@ -61,6 +65,7 @@ Inside RAM, you can distinguish two logical types: static and dynamic memory. St
6165

6266
- Static:
6367
- Vector table (read and write).
68+
- Crash data RAM. (see [the error handling documentation](error-handling.html) for more information on this region)
6469
- Global data.
6570
- Static data.
6671
- Stacks for default threads (main, timer, idle and scheduler/ISR).

0 commit comments

Comments
 (0)