4#ifdef USE_ESP32_CRASH_HANDLER
13#include <esp_private/panic_internal.h>
16#if CONFIG_IDF_TARGET_ARCH_XTENSA
17#include <esp_cpu_utils.h>
18#include <esp_debug_helpers.h>
19#include <xtensa_context.h>
20#elif CONFIG_IDF_TARGET_ARCH_RISCV
21#include <riscv/rvruntime-frames.h>
24static constexpr uint32_t CRASH_MAGIC = 0xDEADBEEF;
25static constexpr size_t MAX_BACKTRACE = 16;
29static inline bool IRAM_ATTR is_code_addr(
uint32_t addr) {
30 return (addr >= SOC_IROM_LOW && addr < SOC_IROM_HIGH) || (addr >= SOC_IRAM_LOW && addr < SOC_IRAM_HIGH);
33#if CONFIG_IDF_TARGET_ARCH_RISCV
44 return (start >= irom_start &&
end <= irom_end) || (start >= SOC_IRAM_LOW &&
end <= SOC_IRAM_HIGH);
51static inline bool is_return_addr(
uint32_t addr) {
52 if (!is_code_addr(addr) || !is_readable_code(addr - 4, addr))
60 memcpy(&inst, (
const void *) (addr - 4),
sizeof(inst));
65 if ((opcode == 0x6f || opcode == 0x67) && rd == 0x80)
71 uint16_t c_inst = *(uint16_t *) (addr - 2);
72 if ((c_inst & 0xf07f) == 0x9002 && (c_inst & 0x0f80) != 0)
82#if CONFIG_IDF_TARGET_ARCH_XTENSA
85static uint8_t IRAM_ATTR walk_xtensa_backtrace(XtExcFrame *frame,
uint32_t *
out, uint8_t max) {
86 esp_backtrace_frame_t bt_frame = {
93 uint32_t first_pc = esp_cpu_process_stack_pc(bt_frame.pc);
94 if (is_code_addr(first_pc)) {
97 while (
count < max && bt_frame.next_pc != 0) {
98 if (!esp_backtrace_get_next_frame(&bt_frame))
100 uint32_t pc = esp_cpu_process_stack_pc(bt_frame.pc);
101 if (is_code_addr(pc)) {
109#if CONFIG_IDF_TARGET_ARCH_RISCV
112static uint8_t IRAM_ATTR capture_riscv_backtrace(RvExcFrame *frame,
uint32_t *
out, uint8_t max, uint8_t *reg_count) {
114 if (is_code_addr(frame->mepc)) {
117 if (is_code_addr(frame->ra) && frame->ra != frame->mepc) {
125 if (is_code_addr(
val) &&
val != frame->mepc &&
val != frame->ra) {
140static constexpr uint32_t CRASH_DATA_VERSION = 4;
141#if CONFIG_IDF_TARGET_ARCH_XTENSA
144static constexpr uint32_t XTENSA_EXCCAUSE_COUNT = XCHAL_EXCCAUSE_NUM;
145#elif CONFIG_IDF_TARGET_ARCH_RISCV
148static constexpr uint32_t RISCV_EXCEPTION_CAUSE_COUNT = 32;
154 uint8_t backtrace_count;
155 uint8_t reg_frame_count;
157 uint8_t pseudo_excause;
162 uint8_t crashed_core;
163#if SOC_CPU_CORES_NUM > 1
164 static_assert(SOC_CPU_CORES_NUM == 2,
"Dual-core logic assumes exactly 2 cores");
165 uint8_t other_backtrace_count;
166 uint8_t other_reg_frame_count;
167 uint32_t other_backtrace[MAX_BACKTRACE];
174static
bool s_crash_data_valid = false;
188static uint32_t s_current_build_time =
static_cast<uint32_t>(ESPHOME_BUILD_TIME);
194static void read_crash_data() {
195 if (s_raw_crash_data.magic == CRASH_MAGIC && s_raw_crash_data.version == CRASH_DATA_VERSION) {
196 s_crash_data_valid =
true;
198 if (s_raw_crash_data.backtrace_count > MAX_BACKTRACE)
199 s_raw_crash_data.backtrace_count = MAX_BACKTRACE;
200 if (s_raw_crash_data.reg_frame_count > s_raw_crash_data.backtrace_count)
201 s_raw_crash_data.reg_frame_count = s_raw_crash_data.backtrace_count;
202 if (s_raw_crash_data.exception > 4)
203 s_raw_crash_data.exception = 4;
204 if (s_raw_crash_data.pseudo_excause > 1)
205 s_raw_crash_data.pseudo_excause = 0;
206 if (s_raw_crash_data.crashed_core >= SOC_CPU_CORES_NUM)
207 s_raw_crash_data.crashed_core = 0;
208#if SOC_CPU_CORES_NUM > 1
209 if (s_raw_crash_data.other_backtrace_count > MAX_BACKTRACE)
210 s_raw_crash_data.other_backtrace_count = MAX_BACKTRACE;
211 if (s_raw_crash_data.other_reg_frame_count > s_raw_crash_data.other_backtrace_count)
212 s_raw_crash_data.other_reg_frame_count = s_raw_crash_data.other_backtrace_count;
219 return s_crash_data_valid;
226 s_raw_crash_data.magic = 0;
230static bool cause_slot_was_written() {
231#if CONFIG_IDF_TARGET_ARCH_XTENSA
232 return s_raw_crash_data.cause < XTENSA_EXCCAUSE_COUNT;
234 return s_raw_crash_data.cause < RISCV_EXCEPTION_CAUSE_COUNT;
241static const char *get_exception_reason() {
242 uint8_t exception = s_raw_crash_data.exception;
243 if (exception == PANIC_EXCEPTION_ABORT || exception == PANIC_EXCEPTION_TWDT) {
247 if (!cause_slot_was_written()) {
251#if CONFIG_IDF_TARGET_ARCH_XTENSA
252 if (s_raw_crash_data.pseudo_excause) {
255 static const char *
const PSEUDO_REASON[] = {
257 "Unhandled debug exception",
259 "Unhandled kernel exception",
260 "Coprocessor exception",
261 "Interrupt wdt timeout on CPU0",
262 "Interrupt wdt timeout on CPU1",
265 uint32_t cause = s_raw_crash_data.cause;
266 if (cause <
sizeof(PSEUDO_REASON) /
sizeof(PSEUDO_REASON[0]))
267 return PSEUDO_REASON[cause];
268 return PSEUDO_REASON[0];
271 static const char *
const REASON[] = {
272 "IllegalInstruction",
274 "InstructionFetchError",
278 "IntegerDivideByZero",
281 "LoadStoreAlignment",
285 "LoadStorePIFDataError",
287 "LoadStorePIFAddrError",
290 "InstFetchPrivilege",
292 "InstrFetchProhibited",
297 "LoadStoreTLBMultihit",
298 "LoadStorePrivilege",
313 uint32_t cause = s_raw_crash_data.cause;
314 if (cause <
sizeof(REASON) /
sizeof(REASON[0]) && REASON[cause] !=
nullptr)
315 return REASON[cause];
316#elif CONFIG_IDF_TARGET_ARCH_RISCV
320 if (s_raw_crash_data.pseudo_excause)
322 static const char *
const REASON[] = {
323 "Instruction address misaligned",
324 "Instruction access fault",
325 "Illegal instruction",
327 "Load address misaligned",
329 "Store address misaligned",
330 "Store access fault",
331 "Environment call from U-mode",
332 "Environment call from S-mode",
334 "Environment call from M-mode",
335 "Instruction page fault",
340 uint32_t cause = s_raw_crash_data.cause;
341 if (cause <
sizeof(REASON) /
sizeof(REASON[0]) && REASON[cause] !=
nullptr)
342 return REASON[cause];
348static const char *get_exception_type() {
349 static const char *
const TYPES[] = {
356 uint8_t exc = s_raw_crash_data.exception;
357 if (exc <
sizeof(TYPES) /
sizeof(TYPES[0]))
363static void log_backtrace(
const uint32_t *addrs, uint8_t
count, uint8_t reg_frame_count) {
365 for (uint8_t i = 0; i <
count; i++) {
367#if CONFIG_IDF_TARGET_ARCH_RISCV
368 if (i >= reg_frame_count && !is_return_addr(addr))
370 const char *source = (i < reg_frame_count) ?
"backtrace" :
"stack scan";
372 const char *source =
"backtrace";
374 ESP_LOGE(TAG,
" BT%d: 0x%08" PRIX32
" (%s)", bt_num++, addr, source);
379static int append_addrs_to_hint(
char *buf,
int size,
int pos,
const uint32_t *addrs, uint8_t
count,
380 uint8_t reg_frame_count) {
381 for (uint8_t i = 0; i <
count &&
pos <
size - 12; i++) {
383#if CONFIG_IDF_TARGET_ARCH_RISCV
384 if (i >= reg_frame_count && !is_return_addr(addr))
387 pos += snprintf(buf + pos, size - pos,
" 0x%08" PRIX32, addr);
395#if CONFIG_IDF_TARGET_ARCH_XTENSA
396static const char *
const FAULT_ADDR_REG =
"EXCVADDR";
397static const char *
const FAULT_ADDR_REG_LOWER =
"excvaddr";
398#elif CONFIG_IDF_TARGET_ARCH_RISCV
399static const char *
const FAULT_ADDR_REG =
"MTVAL";
400static const char *
const FAULT_ADDR_REG_LOWER =
"mtval";
405static bool has_fault_addr() {
406 return s_raw_crash_data.exception == PANIC_EXCEPTION_FAULT && !s_raw_crash_data.pseudo_excause &&
407 cause_slot_was_written();
417static uint8_t log_foreign_backtrace(
const uint32_t *addrs, uint8_t
count, uint8_t bt_num) {
418 for (uint8_t i = 0; i <
count; i++) {
419 ESP_LOGE(TAG,
" bt%d: 0x%08" PRIX32, bt_num++, addrs[i]);
424static void log_foreign_addresses() {
425 ESP_LOGE(TAG,
" Captured by a different firmware build; addresses belong to that build's ELF");
426 ESP_LOGE(TAG,
" pc: 0x%08" PRIX32, s_raw_crash_data.pc);
427 if (has_fault_addr()) {
428 ESP_LOGE(TAG,
" %s: 0x%08" PRIX32, FAULT_ADDR_REG_LOWER, s_raw_crash_data.fault_addr);
430 uint8_t bt_num = log_foreign_backtrace(s_raw_crash_data.backtrace, s_raw_crash_data.backtrace_count, 0);
431#if SOC_CPU_CORES_NUM > 1
432 if (s_raw_crash_data.other_backtrace_count > 0) {
434 ESP_LOGE(TAG,
" other core (%d):", 1 - s_raw_crash_data.crashed_core);
435 log_foreign_backtrace(s_raw_crash_data.other_backtrace, s_raw_crash_data.other_backtrace_count, bt_num);
451 ESP_LOGE(TAG,
"*** CRASH DETECTED ON PREVIOUS BOOT ***");
452 const char *reason = get_exception_reason();
453 if (reason !=
nullptr) {
454 ESP_LOGE(TAG,
" Reason: %s - %s (cause %" PRIu32
")", get_exception_type(), reason, s_raw_crash_data.cause);
456 ESP_LOGE(TAG,
" Reason: %s", get_exception_type());
458 ESP_LOGE(TAG,
" Crashed core: %d", s_raw_crash_data.crashed_core);
459 if (s_raw_crash_data.build_time != s_current_build_time) {
462 log_foreign_addresses();
465 ESP_LOGE(TAG,
" PC: 0x%08" PRIX32
" (fault location)", s_raw_crash_data.pc);
468 if (has_fault_addr()) {
469 ESP_LOGE(TAG,
" %s: 0x%08" PRIX32
" (faulting address)", FAULT_ADDR_REG, s_raw_crash_data.fault_addr);
471 log_backtrace(s_raw_crash_data.backtrace, s_raw_crash_data.backtrace_count, s_raw_crash_data.reg_frame_count);
473#if SOC_CPU_CORES_NUM > 1
474 if (s_raw_crash_data.other_backtrace_count > 0) {
475 int other_core = 1 - s_raw_crash_data.crashed_core;
476 ESP_LOGE(TAG,
" Other core (%d) backtrace:", other_core);
477 log_backtrace(s_raw_crash_data.other_backtrace, s_raw_crash_data.other_backtrace_count,
478 s_raw_crash_data.other_reg_frame_count);
485 static const char *
const ADDR2LINE_CMD =
"addr2line -pfiaC -e firmware.elf";
487 int pos = snprintf(hint,
sizeof(hint),
"Use: %s 0x%08" PRIX32, ADDR2LINE_CMD, s_raw_crash_data.pc);
488 append_addrs_to_hint(hint,
sizeof(hint),
pos, s_raw_crash_data.backtrace, s_raw_crash_data.backtrace_count,
489 s_raw_crash_data.reg_frame_count);
490 ESP_LOGE(TAG,
"%s", hint);
491#if SOC_CPU_CORES_NUM > 1
492 if (s_raw_crash_data.other_backtrace_count > 0) {
493 pos = snprintf(hint,
sizeof(hint),
"Other core: %s", ADDR2LINE_CMD);
494 append_addrs_to_hint(hint,
sizeof(hint),
pos, s_raw_crash_data.other_backtrace,
495 s_raw_crash_data.other_backtrace_count, s_raw_crash_data.other_reg_frame_count);
496 ESP_LOGE(TAG,
"%s", hint);
518 s_raw_crash_data.pc = (
uint32_t) info->addr;
519 s_raw_crash_data.backtrace_count = 0;
520 s_raw_crash_data.reg_frame_count = 0;
521 s_raw_crash_data.exception = (uint8_t) info->exception;
522 s_raw_crash_data.pseudo_excause = info->pseudo_excause ? 1 : 0;
523 s_raw_crash_data.crashed_core = (uint8_t) info->core;
529 bool is_twdt = &g_twdt_isr !=
nullptr && g_twdt_isr;
530 s_raw_crash_data.exception = (uint8_t) (is_twdt ? PANIC_EXCEPTION_TWDT : PANIC_EXCEPTION_ABORT);
533 s_raw_crash_data.cause = 0;
534 s_raw_crash_data.fault_addr = 0;
539 s_raw_crash_data.build_time = esphome::esp32::s_current_build_time;
540#if SOC_CPU_CORES_NUM > 1
541 s_raw_crash_data.other_backtrace_count = 0;
542 s_raw_crash_data.other_reg_frame_count = 0;
545#if CONFIG_IDF_TARGET_ARCH_XTENSA
547 if (info->frame !=
nullptr) {
548 auto *xt_frame = (XtExcFrame *) info->frame;
550 if (info->pseudo_excause)
551 s_raw_crash_data.pc = xt_frame->pc;
552 if (!g_panic_abort) {
555 s_raw_crash_data.cause = xt_frame->exccause;
556 s_raw_crash_data.fault_addr = xt_frame->excvaddr;
558 s_raw_crash_data.backtrace_count = walk_xtensa_backtrace(xt_frame, s_raw_crash_data.backtrace, MAX_BACKTRACE);
561#if SOC_CPU_CORES_NUM > 1
565 if (info->core >= 0 && info->core < SOC_CPU_CORES_NUM) {
566 int other_core = 1 - info->core;
567 auto *other_frame = (XtExcFrame *) g_exc_frames[other_core];
568 if (other_frame !=
nullptr) {
569 s_raw_crash_data.other_backtrace_count =
570 walk_xtensa_backtrace(other_frame, s_raw_crash_data.other_backtrace, MAX_BACKTRACE);
575#elif CONFIG_IDF_TARGET_ARCH_RISCV
577 if (info->frame !=
nullptr) {
578 auto *rv_frame = (RvExcFrame *) info->frame;
579 if (!g_panic_abort) {
581 s_raw_crash_data.cause = rv_frame->mcause;
582 s_raw_crash_data.fault_addr = rv_frame->mtval;
584 s_raw_crash_data.backtrace_count =
585 capture_riscv_backtrace(rv_frame, s_raw_crash_data.backtrace, MAX_BACKTRACE, &s_raw_crash_data.reg_frame_count);
588#if SOC_CPU_CORES_NUM > 1
590 if (info->core >= 0 && info->core < SOC_CPU_CORES_NUM) {
591 int other_core = 1 - info->core;
592 auto *other_frame = (RvExcFrame *) g_exc_frames[other_core];
593 if (other_frame !=
nullptr) {
594 s_raw_crash_data.other_backtrace_count = capture_riscv_backtrace(
595 other_frame, s_raw_crash_data.other_backtrace, MAX_BACKTRACE, &s_raw_crash_data.other_reg_frame_count);
602 s_raw_crash_data.version = CRASH_DATA_VERSION;
603 s_raw_crash_data.magic = CRASH_MAGIC;
struct @66::@67 __attribute__
Wake the main loop task from an ISR. ISR-safe.
char _instruction_reserved_start
void __real_esp_panic_handler(panic_info_t *info)
char _instruction_reserved_end
void IRAM_ATTR __wrap_esp_panic_handler(panic_info_t *info)
std::vector< uint8_t > & out
ESPHOME_LOG_TAG(TAG, "hw_timer_esp_idf")
bool crash_handler_has_data()
Returns true if crash data was found this boot, reading it first if needed.
void crash_handler_log()
Log crash data if a crash was detected on previous boot.
void crash_handler_clear()
Clear the magic marker and mark crash data as consumed.