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
If you made any changes to your sketch, click the
If an exception occurs on the board, information will be displayed in the Serial Monitor.
Following the "Registers information" table, the type of fault will be specified. You can read more about the types here.
The last line includes a command for the
addr2line
utility. See Running addr2line for more information.You can use the
addr2line
tool to generate a stack trace. See the instructions below:Note:
is not available as a native Windows application, but can be run with Windows Subsystem for Linux (WSL).addr2line
The
addr2line
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: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 addr2line
.addr2line
can be installed with Homebrew by running brew install binutils
in Terminal.addr2line
may already be installed on your system. Otherwise, run apt-get install binutils
in Terminal (Ubuntu, Debian), or see command-not-found.com/addr2line for other distributions.addr2line
CommandWindows (WSL):
C:
with /mnt/c
and replace all backslashes (\
) with forward slashes (/
).macOS:
Copy the command from the serial output (no modification required).
Linux:
Copy the command from the serial output (no modification required).
If you don't want to install
, you can use the addr2line
from the board package. See the instructions hereaddr2line
addr2line
CommandNote: The sketch needs to have been compiled on the same computer you are running
on.addr2line
Windows (WSL):
Ubuntu
into Windows Powershell and press Enter.macOS:
Linux:
Optionally, you can add the
-p
flag to your command for a more readable format.addr2line
OutputBy default, the command outputs the following for each function call:
0x00004188
", "0x0000426e
", ... (remove the -a
flag if you don't want to include these)_ZN4UART5writeEh
", "loop
", .../Users/sebastianwikstrom/Documents/Arduino/ErrorInducer/ErrorInducer.ino:67
, ...Follow these steps:
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,
/Users/username/Documents/Arduino/ErrorInducer/ErrorInducer.ino:67
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.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.
addr2line
output.In this example, an out-of-bounds access of the
numbers
array occurs after a few iterations of the while(true)
loop:addr2line
Command (Board Package)These instructions replace the instructions in 1. Copy
Commandaddr2line
If you want to use the
addr2line
directly from the board package, follow the instructions belowWindows (WSL):
addr2line
with /mnt/c/Users/User/Appdata/Local/Arduino15/packages/arduino/tools/arm-none-eabi-gcc/7-2017q4/bin/arm-none-eabi-addr2line
-e
flag, replace C:
with /mnt/c
and replace all backslashes (\
) with forward slashes (/
).macOS:
addr2line
with ~/Library/Arduino15/packages/arduino/tools/arm-none-eabi-gcc/7-2017q4/bin/arm-none-eabi-addr2line
.Linux:
addr2line
with .arduino15/packages/arduino/tools/arm-none-eabi-gcc/7-2017q4/bin/arm-none-eabi-addr2line
.