4#ifdef USE_ESP32_CRASH_HANDLER
12#include <esp_private/panic_internal.h>
15#if CONFIG_IDF_TARGET_ARCH_XTENSA
16#include <esp_cpu_utils.h>
17#include <esp_debug_helpers.h>
18#include <xtensa_context.h>
19#elif CONFIG_IDF_TARGET_ARCH_RISCV
20#include <riscv/rvruntime-frames.h>
23static constexpr uint32_t CRASH_MAGIC = 0xDEADBEEF;
24static constexpr size_t MAX_BACKTRACE = 16;
28static inline bool IRAM_ATTR is_code_addr(
uint32_t addr) {
29 return (addr >= SOC_IROM_LOW && addr < SOC_IROM_HIGH) || (addr >= SOC_IRAM_LOW && addr < SOC_IRAM_HIGH);
32#if CONFIG_IDF_TARGET_ARCH_RISCV
36static inline bool is_return_addr(
uint32_t addr) {
37 if (!is_code_addr(addr) || addr < 4)
44 memcpy(&inst, (
const void *) (addr - 4),
sizeof(inst));
49 if ((opcode == 0x6f || opcode == 0x67) && rd == 0x80)
54 uint16_t c_inst = *(uint16_t *) (addr - 2);
55 if ((c_inst & 0xf07f) == 0x9002 && (c_inst & 0x0f80) != 0)
65#if CONFIG_IDF_TARGET_ARCH_XTENSA
68static uint8_t IRAM_ATTR walk_xtensa_backtrace(XtExcFrame *frame,
uint32_t *out, uint8_t max) {
69 esp_backtrace_frame_t bt_frame = {
76 uint32_t first_pc = esp_cpu_process_stack_pc(bt_frame.pc);
77 if (is_code_addr(first_pc)) {
78 out[count++] = first_pc;
80 while (count < max && bt_frame.next_pc != 0) {
81 if (!esp_backtrace_get_next_frame(&bt_frame))
83 uint32_t pc = esp_cpu_process_stack_pc(bt_frame.pc);
84 if (is_code_addr(
pc)) {
92#if CONFIG_IDF_TARGET_ARCH_RISCV
95static uint8_t IRAM_ATTR capture_riscv_backtrace(RvExcFrame *frame,
uint32_t *out, uint8_t max, uint8_t *reg_count) {
97 if (is_code_addr(frame->mepc)) {
98 out[count++] = frame->mepc;
100 if (is_code_addr(frame->ra) && frame->ra != frame->mepc) {
101 out[count++] = frame->ra;
105 for (
uint32_t i = 0; i < 64 && count < max; i++) {
107 if (is_code_addr(
val) &&
val != frame->mepc &&
val != frame->ra) {
122static constexpr uint32_t CRASH_DATA_VERSION = 2;
127 uint8_t backtrace_count;
128 uint8_t reg_frame_count;
130 uint8_t pseudo_excause;
133 uint8_t crashed_core;
134#if SOC_CPU_CORES_NUM > 1
135 static_assert(SOC_CPU_CORES_NUM == 2,
"Dual-core logic assumes exactly 2 cores");
136 uint8_t other_backtrace_count;
137 uint8_t other_reg_frame_count;
138 uint32_t other_backtrace[MAX_BACKTRACE];
145static
bool s_crash_data_valid = false;
149static const char *
const TAG =
"esp32.crash";
152 if (s_raw_crash_data.magic == CRASH_MAGIC && s_raw_crash_data.version == CRASH_DATA_VERSION) {
153 s_crash_data_valid =
true;
155 if (s_raw_crash_data.backtrace_count > MAX_BACKTRACE)
156 s_raw_crash_data.backtrace_count = MAX_BACKTRACE;
157 if (s_raw_crash_data.reg_frame_count > s_raw_crash_data.backtrace_count)
158 s_raw_crash_data.reg_frame_count = s_raw_crash_data.backtrace_count;
159 if (s_raw_crash_data.exception > 4)
160 s_raw_crash_data.exception = 4;
161 if (s_raw_crash_data.pseudo_excause > 1)
162 s_raw_crash_data.pseudo_excause = 0;
163 if (s_raw_crash_data.crashed_core >= SOC_CPU_CORES_NUM)
164 s_raw_crash_data.crashed_core = 0;
165#if SOC_CPU_CORES_NUM > 1
166 if (s_raw_crash_data.other_backtrace_count > MAX_BACKTRACE)
167 s_raw_crash_data.other_backtrace_count = MAX_BACKTRACE;
168 if (s_raw_crash_data.other_reg_frame_count > s_raw_crash_data.other_backtrace_count)
169 s_raw_crash_data.other_reg_frame_count = s_raw_crash_data.other_backtrace_count;
182 s_raw_crash_data.magic = 0;
188static const char *get_exception_reason() {
189#if CONFIG_IDF_TARGET_ARCH_XTENSA
190 if (s_raw_crash_data.pseudo_excause) {
193 static const char *
const PSEUDO_REASON[] = {
195 "Unhandled debug exception",
197 "Unhandled kernel exception",
198 "Coprocessor exception",
199 "Interrupt wdt timeout on CPU0",
200 "Interrupt wdt timeout on CPU1",
203 uint32_t cause = s_raw_crash_data.cause;
204 if (cause <
sizeof(PSEUDO_REASON) /
sizeof(PSEUDO_REASON[0]))
205 return PSEUDO_REASON[cause];
206 return PSEUDO_REASON[0];
209 static const char *
const REASON[] = {
210 "IllegalInstruction",
212 "InstructionFetchError",
216 "IntegerDivideByZero",
219 "LoadStoreAlignment",
223 "LoadStorePIFDataError",
225 "LoadStorePIFAddrError",
228 "InstFetchPrivilege",
230 "InstrFetchProhibited",
235 "LoadStoreTLBMultihit",
236 "LoadStorePrivilege",
241 uint32_t cause = s_raw_crash_data.cause;
242 if (cause <
sizeof(REASON) /
sizeof(REASON[0]) && REASON[cause] !=
nullptr)
243 return REASON[cause];
244#elif CONFIG_IDF_TARGET_ARCH_RISCV
248 if (s_raw_crash_data.pseudo_excause)
250 static const char *
const REASON[] = {
251 "Instruction address misaligned",
252 "Instruction access fault",
253 "Illegal instruction",
255 "Load address misaligned",
257 "Store address misaligned",
258 "Store access fault",
259 "Environment call from U-mode",
260 "Environment call from S-mode",
262 "Environment call from M-mode",
263 "Instruction page fault",
268 uint32_t cause = s_raw_crash_data.cause;
269 if (cause <
sizeof(REASON) /
sizeof(REASON[0]) && REASON[cause] !=
nullptr)
270 return REASON[cause];
276static const char *get_exception_type() {
277 static const char *
const TYPES[] = {
284 uint8_t exc = s_raw_crash_data.exception;
285 if (exc <
sizeof(TYPES) /
sizeof(TYPES[0]))
291static void log_backtrace(
const uint32_t *addrs, uint8_t count, uint8_t reg_frame_count) {
293 for (uint8_t i = 0; i < count; i++) {
295#if CONFIG_IDF_TARGET_ARCH_RISCV
296 if (i >= reg_frame_count && !is_return_addr(addr))
298 const char *source = (i < reg_frame_count) ?
"backtrace" :
"stack scan";
300 const char *source =
"backtrace";
302 ESP_LOGE(TAG,
" BT%d: 0x%08" PRIX32
" (%s)", bt_num++, addr, source);
307static int append_addrs_to_hint(
char *buf,
int size,
int pos,
const uint32_t *addrs, uint8_t count,
308 uint8_t reg_frame_count) {
309 for (uint8_t i = 0; i < count &&
pos <
size - 12; i++) {
311#if CONFIG_IDF_TARGET_ARCH_RISCV
312 if (i >= reg_frame_count && !is_return_addr(addr))
315 pos += snprintf(buf + pos, size - pos,
" 0x%08" PRIX32, addr);
326 if (!s_crash_data_valid)
329 ESP_LOGE(TAG,
"*** CRASH DETECTED ON PREVIOUS BOOT ***");
330 const char *reason = get_exception_reason();
331 if (reason !=
nullptr) {
332 ESP_LOGE(TAG,
" Reason: %s - %s", get_exception_type(), reason);
334 ESP_LOGE(TAG,
" Reason: %s", get_exception_type());
336 ESP_LOGE(TAG,
" Crashed core: %d", s_raw_crash_data.crashed_core);
337 ESP_LOGE(TAG,
" PC: 0x%08" PRIX32
" (fault location)", s_raw_crash_data.pc);
338 log_backtrace(s_raw_crash_data.backtrace, s_raw_crash_data.backtrace_count, s_raw_crash_data.reg_frame_count);
340#if SOC_CPU_CORES_NUM > 1
341 if (s_raw_crash_data.other_backtrace_count > 0) {
342 int other_core = 1 - s_raw_crash_data.crashed_core;
343 ESP_LOGE(TAG,
" Other core (%d) backtrace:", other_core);
344 log_backtrace(s_raw_crash_data.other_backtrace, s_raw_crash_data.other_backtrace_count,
345 s_raw_crash_data.other_reg_frame_count);
351 int pos = snprintf(hint,
sizeof(hint),
"Use: addr2line -pfiaC -e firmware.elf 0x%08" PRIX32, s_raw_crash_data.pc);
352 pos = append_addrs_to_hint(hint,
sizeof(hint),
pos, s_raw_crash_data.backtrace, s_raw_crash_data.backtrace_count,
353 s_raw_crash_data.reg_frame_count);
354#if SOC_CPU_CORES_NUM > 1
355 append_addrs_to_hint(hint,
sizeof(hint),
pos, s_raw_crash_data.other_backtrace,
356 s_raw_crash_data.other_backtrace_count, s_raw_crash_data.other_reg_frame_count);
358 ESP_LOGE(TAG,
"%s", hint);
374 s_raw_crash_data.pc = (
uint32_t) info->addr;
375 s_raw_crash_data.backtrace_count = 0;
376 s_raw_crash_data.reg_frame_count = 0;
377 s_raw_crash_data.exception = (uint8_t) info->exception;
378 s_raw_crash_data.pseudo_excause = info->pseudo_excause ? 1 : 0;
379 s_raw_crash_data.crashed_core = (uint8_t) info->core;
380#if SOC_CPU_CORES_NUM > 1
381 s_raw_crash_data.other_backtrace_count = 0;
382 s_raw_crash_data.other_reg_frame_count = 0;
385#if CONFIG_IDF_TARGET_ARCH_XTENSA
387 if (info->frame !=
nullptr) {
388 auto *xt_frame = (XtExcFrame *) info->frame;
389 s_raw_crash_data.cause = xt_frame->exccause;
390 s_raw_crash_data.backtrace_count = walk_xtensa_backtrace(xt_frame, s_raw_crash_data.backtrace, MAX_BACKTRACE);
393#if SOC_CPU_CORES_NUM > 1
397 if (info->core >= 0 && info->core < SOC_CPU_CORES_NUM) {
398 int other_core = 1 - info->core;
399 auto *other_frame = (XtExcFrame *) g_exc_frames[other_core];
400 if (other_frame !=
nullptr) {
401 s_raw_crash_data.other_backtrace_count =
402 walk_xtensa_backtrace(other_frame, s_raw_crash_data.other_backtrace, MAX_BACKTRACE);
407#elif CONFIG_IDF_TARGET_ARCH_RISCV
409 if (info->frame !=
nullptr) {
410 auto *rv_frame = (RvExcFrame *) info->frame;
411 s_raw_crash_data.cause = rv_frame->mcause;
412 s_raw_crash_data.backtrace_count =
413 capture_riscv_backtrace(rv_frame, s_raw_crash_data.backtrace, MAX_BACKTRACE, &s_raw_crash_data.reg_frame_count);
416#if SOC_CPU_CORES_NUM > 1
418 if (info->core >= 0 && info->core < SOC_CPU_CORES_NUM) {
419 int other_core = 1 - info->core;
420 auto *other_frame = (RvExcFrame *) g_exc_frames[other_core];
421 if (other_frame !=
nullptr) {
422 s_raw_crash_data.other_backtrace_count = capture_riscv_backtrace(
423 other_frame, s_raw_crash_data.other_backtrace, MAX_BACKTRACE, &s_raw_crash_data.other_reg_frame_count);
430 s_raw_crash_data.version = CRASH_DATA_VERSION;
431 s_raw_crash_data.magic = CRASH_MAGIC;
struct @65::@66 __attribute__
Wake the main loop task from an ISR. ISR-safe.
void __real_esp_panic_handler(panic_info_t *info)
void IRAM_ATTR __wrap_esp_panic_handler(panic_info_t *info)
bool crash_handler_has_data()
Returns true if crash data was found this boot.
void crash_handler_log()
Log crash data if a crash was detected on previous boot.
void crash_handler_read_and_clear()
Read and validate crash data from NOINIT memory.
void crash_handler_clear()
Clear the magic marker and mark crash data as consumed.