Learn how to get information from the Cortex-M4 processor on a runtime error.
Author Sebastian Wikström
Last revision 02/16/2024
The Arduino UNO R4 WiFi uses CmBacktrace to print useful information from the Arm Cortex-M4 processor as serial output when a runtime error occurs. The output includes an
addr2line
command that can used to produce a stack trace (also stack backtrace, or stack traceback), which can be used to find the source of the error.
In this tutorial, we will show to enable this feature.
To get the runtime error information in the Serial Monitor, follow the steps below:
Ensure that a serial connection must be initiated before the error occurs, by calling Serial.begin() in your sketch. You can include this code inside the beginning of the
setup()
function:
1Serial.begin(115200);
2while (!Serial);
Connect your UNO R4 WiFi board to your computer.
Open the Serial Monitor. Click the Serial Monitor iconSerial Monitor button in the top-right corner or select Tools > Serial Monitor in the menu bar.
If you made any changes to your sketch, click the Upload iconUpload button. Otherwise, you can simply press the RESET button on the board.
If an exception occurs on the board, information will be displayed in the Serial Monitor.
Serial output.
Following the "Registers information" table, the type of fault will be specified. You can read more about the types here.
utility is included in the Arduino UNO R4 Boards boards package. However, running it in this way requires modifying the command included in the output. For convenience, you may want to install
addr2line
on your system.
To install
addr2line
(optional), use the OS specific instructions below:
Windows (WSL):
addr2line
is not available as a native Windows application, but can be run with Windows Subsystem for Linux (WSL). The Ubuntu distribution of Linux is installed by default and should come with
Look for the topmost line in the output that's inside your sketch. The number following the path is the line number where the error occurred. For example,
indicates that the error occurred on line 67. By reading further down the output, you can step backward through the function calls that were made.
Reading the addr2line stack trace in Terminal.
Open the sketch in Arduino IDE and find the line number from the previous step (the number is displayed to the left of each line).
Analyze the row where the error occurred and try to understand what may be triggering the error.
If you're not sure, use the Serial.println() function to output the values of any variables being used. Then upload the sketch again, and use the serial output to see what the states of those variables were before the error occurred.
To see from where the function was called, look at the preceding function call in the
addr2line
output.
In this example, an out-of-bounds access of the
numbers
array occurs after a few iterations of the
while(true)
loop:
Analyzing the code.
Additional Instructions
Copy the
addr2line
Command (Board Package)
These instructions replace the instructions in 1. Copy