Category: C / C++

How to decode the exception backtrace of an ESP32

ESP32

When the execution of code on an ESP32 throws an exception, the output might look like this:

Guru Meditation Error: Core  1 panic'ed (LoadProhibited). Exception was unhandled.??
??
Core  1 register dump:??
PC      : 0x420383b2  PS      : 0x00060c30  A0      : 0x820059ec  A1      : 0x3fcebd70  ??
A2      : 0x3fc97708  A3      : 0x3fcebdcb  A4      : 0x00000001  A5      : 0x0000ff00  ??
A6      : 0x00ff0000  A7      : 0xff000000  A8      : 0x3fcecf5b  A9      : 0x0000723f  ??
A10     : 0x3fcecf5b  A11     : 0x00000001  A12     : 0x00000001  A13     : 0x3fcf06b4  ??
A14     : 0x00000001  A15     : 0x00000003  SAR     : 0x00000017  EXCCAUSE: 0x0000001c  ??
EXCVADDR: 0x0000728b  LBEG    : 0x40056f08  LEND    : 0x40056f12  LCOUNT  : 0x00000000  ??
??
??
Backtrace: 0x420383af:0x3fcebd70 0x420059e9:0x3fcebd90 0x42004e26:0x3fcebdb0 0x420035a2:0x3fcebe00 0x42003595:0x3fcebe20 0x4200542c:0x3fcebe40 0x42008891:0x3fcebe60??
...

The Espressif tools contain a binary called xtensa-esp32-elf-addr2line which will decode the backtrace addresses and return details about the source files, lines and function names, etc.

To run the tool, call:

/home/user/.espressif/tools/xtensa-esp32-elf/esp-2020r3-8.4.0/xtensa-esp32-elf/bin/xtensa-esp32-elf-addr2line -fe /path/to/build/<ELFFILE>.elf <BACKTRACE>

In the command above, simply…

  • replace the path to the xtensa-esp32-elf-addr2line binary
    (in my case the binary is located in the users home directory in the folder .espressif)
  • replace the path to your elf file
    (normally, the elf file is located in the build path and generated during compilation)
  • replace the backtrace output string
    (it’s the string that is shown after Backtrace: )

 

How to set C, C++ or Fortran compiler for CMake

To use a different compiler (e.g. Intel Compiler, CLANG or PGI) or a different version then CMake uses by default, one can either set environment variables or modify
the CMakeLists.txt file.

CMake evaluates the environment variables CC for the C compiler, CXX for the C++ compiler and FC
for the Fortran compiler:

CC=/path/to/icc cmake ..
CXX=/path/to/icpc cmake ..
FC=/path/to/ifort cmake ..

For a more permanent solution, one can also edit the CMakeLists.txt file:

SET(CMAKE_C_COMPILER /path/to/pgcc)
SET(CMAKE_CXX_COMPILER /path/to/pgc++)
SET(CMAKE_FC_COMPILER /path/to/pgfortran)

BTW: The environment variables LDFLAGS, CFLAGS, CXXFLAGS or FFLAGS are also evaluated by CMake.