4#ifdef USE_ESP32_CRASH_HANDLER
14#include <esp_private/panic_internal.h>
17#if CONFIG_IDF_TARGET_ARCH_XTENSA
18#include <esp_cpu_utils.h>
19#include <esp_debug_helpers.h>
20#include <xtensa_context.h>
21#elif CONFIG_IDF_TARGET_ARCH_RISCV
22#include <riscv/rvruntime-frames.h>
25static constexpr uint32_t CRASH_MAGIC = 0xDEADBEEF;
26static constexpr size_t MAX_BACKTRACE = 16;
30static inline bool IRAM_ATTR is_code_addr(
uint32_t addr) {
31 return (addr >= SOC_IROM_LOW && addr < SOC_IROM_HIGH) || (addr >= SOC_IRAM_LOW && addr < SOC_IRAM_HIGH);
34#if CONFIG_IDF_TARGET_ARCH_RISCV
45 return (start >= irom_start &&
end <= irom_end) || (start >= SOC_IRAM_LOW &&
end <= SOC_IRAM_HIGH);
52static inline bool is_return_addr(
uint32_t addr) {
53 if (!is_code_addr(addr) || !is_readable_code(addr - 4, addr))
61 memcpy(&inst, (
const void *) (addr - 4),
sizeof(inst));
66 if ((opcode == 0x6f || opcode == 0x67) && rd == 0x80)
72 uint16_t c_inst = *(uint16_t *) (addr - 2);
73 if ((c_inst & 0xf07f) == 0x9002 && (c_inst & 0x0f80) != 0)
83#if CONFIG_IDF_TARGET_ARCH_XTENSA
86static uint8_t IRAM_ATTR walk_xtensa_backtrace(XtExcFrame *frame,
uint32_t *
out, uint8_t max) {
87 esp_backtrace_frame_t bt_frame = {
94 uint32_t first_pc = esp_cpu_process_stack_pc(bt_frame.pc);
95 if (is_code_addr(first_pc)) {
98 while (
count < max && bt_frame.next_pc != 0) {
99 if (!esp_backtrace_get_next_frame(&bt_frame))
101 uint32_t pc = esp_cpu_process_stack_pc(bt_frame.pc);
102 if (is_code_addr(pc)) {
110#if CONFIG_IDF_TARGET_ARCH_RISCV
113static uint8_t IRAM_ATTR capture_riscv_backtrace(RvExcFrame *frame,
uint32_t *
out, uint8_t max, uint8_t *reg_count) {
115 if (is_code_addr(frame->mepc)) {
118 if (is_code_addr(frame->ra) && frame->ra != frame->mepc) {
126 if (is_code_addr(
val) &&
val != frame->mepc &&
val != frame->ra) {
141static constexpr uint32_t CRASH_DATA_VERSION = 4;
142#if CONFIG_IDF_TARGET_ARCH_XTENSA
145static constexpr uint32_t XTENSA_EXCCAUSE_COUNT = XCHAL_EXCCAUSE_NUM;
146#elif CONFIG_IDF_TARGET_ARCH_RISCV
149static constexpr uint32_t RISCV_EXCEPTION_CAUSE_COUNT = 32;
155 uint8_t backtrace_count;
156 uint8_t reg_frame_count;
158 uint8_t pseudo_excause;
163 uint8_t crashed_core;
164#if SOC_CPU_CORES_NUM > 1
165 static_assert(SOC_CPU_CORES_NUM == 2,
"Dual-core logic assumes exactly 2 cores");
166 uint8_t other_backtrace_count;
167 uint8_t other_reg_frame_count;
168 uint32_t other_backtrace[MAX_BACKTRACE];
175static
bool s_crash_data_valid = false;
189static uint32_t s_current_build_time =
static_cast<uint32_t>(ESPHOME_BUILD_TIME);
195static void read_crash_data() {
196 if (s_raw_crash_data.magic == CRASH_MAGIC && s_raw_crash_data.version == CRASH_DATA_VERSION) {
197 s_crash_data_valid =
true;
199 if (s_raw_crash_data.backtrace_count > MAX_BACKTRACE)
200 s_raw_crash_data.backtrace_count = MAX_BACKTRACE;
201 if (s_raw_crash_data.reg_frame_count > s_raw_crash_data.backtrace_count)
202 s_raw_crash_data.reg_frame_count = s_raw_crash_data.backtrace_count;
203 if (s_raw_crash_data.exception > 4)
204 s_raw_crash_data.exception = 4;
205 if (s_raw_crash_data.pseudo_excause > 1)
206 s_raw_crash_data.pseudo_excause = 0;
207 if (s_raw_crash_data.crashed_core >= SOC_CPU_CORES_NUM)
208 s_raw_crash_data.crashed_core = 0;
209#if SOC_CPU_CORES_NUM > 1
210 if (s_raw_crash_data.other_backtrace_count > MAX_BACKTRACE)
211 s_raw_crash_data.other_backtrace_count = MAX_BACKTRACE;
212 if (s_raw_crash_data.other_reg_frame_count > s_raw_crash_data.other_backtrace_count)
213 s_raw_crash_data.other_reg_frame_count = s_raw_crash_data.other_backtrace_count;
220 return s_crash_data_valid;
227 s_raw_crash_data.magic = 0;
231static bool cause_slot_was_written() {
232#if CONFIG_IDF_TARGET_ARCH_XTENSA
233 return s_raw_crash_data.cause < XTENSA_EXCCAUSE_COUNT;
235 return s_raw_crash_data.cause < RISCV_EXCEPTION_CAUSE_COUNT;
242static const char *get_exception_reason() {
243 uint8_t exception = s_raw_crash_data.exception;
244 if (exception == PANIC_EXCEPTION_ABORT || exception == PANIC_EXCEPTION_TWDT) {
248 if (!cause_slot_was_written()) {
252#if CONFIG_IDF_TARGET_ARCH_XTENSA
253 if (s_raw_crash_data.pseudo_excause) {
256 static const char *
const PSEUDO_REASON[] = {
258 "Unhandled debug exception",
260 "Unhandled kernel exception",
261 "Coprocessor exception",
262 "Interrupt wdt timeout on CPU0",
263 "Interrupt wdt timeout on CPU1",
266 uint32_t cause = s_raw_crash_data.cause;
267 if (cause <
sizeof(PSEUDO_REASON) /
sizeof(PSEUDO_REASON[0]))
268 return PSEUDO_REASON[cause];
269 return PSEUDO_REASON[0];
272 static const char *
const REASON[] = {
273 "IllegalInstruction",
275 "InstructionFetchError",
279 "IntegerDivideByZero",
282 "LoadStoreAlignment",
286 "LoadStorePIFDataError",
288 "LoadStorePIFAddrError",
291 "InstFetchPrivilege",
293 "InstrFetchProhibited",
298 "LoadStoreTLBMultihit",
299 "LoadStorePrivilege",
314 uint32_t cause = s_raw_crash_data.cause;
315 if (cause <
sizeof(REASON) /
sizeof(REASON[0]) && REASON[cause] !=
nullptr)
316 return REASON[cause];
317#elif CONFIG_IDF_TARGET_ARCH_RISCV
321 if (s_raw_crash_data.pseudo_excause)
323 static const char *
const REASON[] = {
324 "Instruction address misaligned",
325 "Instruction access fault",
326 "Illegal instruction",
328 "Load address misaligned",
330 "Store address misaligned",
331 "Store access fault",
332 "Environment call from U-mode",
333 "Environment call from S-mode",
335 "Environment call from M-mode",
336 "Instruction page fault",
341 uint32_t cause = s_raw_crash_data.cause;
342 if (cause <
sizeof(REASON) /
sizeof(REASON[0]) && REASON[cause] !=
nullptr)
343 return REASON[cause];
349static const char *get_exception_type() {
350 static const char *
const TYPES[] = {
357 uint8_t exc = s_raw_crash_data.exception;
358 if (exc <
sizeof(TYPES) /
sizeof(TYPES[0]))
364static void log_backtrace(
const uint32_t *addrs, uint8_t
count, uint8_t reg_frame_count) {
366 for (uint8_t i = 0; i <
count; i++) {
368#if CONFIG_IDF_TARGET_ARCH_RISCV
369 if (i >= reg_frame_count && !is_return_addr(addr))
371 const char *source = (i < reg_frame_count) ?
"backtrace" :
"stack scan";
373 const char *source =
"backtrace";
375 ESP_LOGE(TAG,
" BT%d: 0x%08" PRIX32
" (%s)", bt_num++, addr, source);
380static int append_addrs_to_hint(
char *buf,
int size,
int pos,
const uint32_t *addrs, uint8_t
count,
381 uint8_t reg_frame_count) {
382 for (uint8_t i = 0; i <
count &&
pos <
size - 12; i++) {
384#if CONFIG_IDF_TARGET_ARCH_RISCV
385 if (i >= reg_frame_count && !is_return_addr(addr))
388 pos += snprintf(buf + pos, size - pos,
" 0x%08" PRIX32, addr);
396#if CONFIG_IDF_TARGET_ARCH_XTENSA
397static const char *
const FAULT_ADDR_REG =
"EXCVADDR";
398static const char *
const FAULT_ADDR_REG_LOWER =
"excvaddr";
399#elif CONFIG_IDF_TARGET_ARCH_RISCV
400static const char *
const FAULT_ADDR_REG =
"MTVAL";
401static const char *
const FAULT_ADDR_REG_LOWER =
"mtval";
406static bool has_fault_addr() {
407 return s_raw_crash_data.exception == PANIC_EXCEPTION_FAULT && !s_raw_crash_data.pseudo_excause &&
408 cause_slot_was_written();
418static uint8_t log_foreign_backtrace(
const uint32_t *addrs, uint8_t
count, uint8_t bt_num) {
419 for (uint8_t i = 0; i <
count; i++) {
420 ESP_LOGE(TAG,
" bt%d: 0x%08" PRIX32, bt_num++, addrs[i]);
425static void log_foreign_addresses() {
426 ESP_LOGE(TAG,
" Captured by a different firmware build; addresses belong to that build's ELF");
427 ESP_LOGE(TAG,
" pc: 0x%08" PRIX32, s_raw_crash_data.pc);
428 if (has_fault_addr()) {
429 ESP_LOGE(TAG,
" %s: 0x%08" PRIX32, FAULT_ADDR_REG_LOWER, s_raw_crash_data.fault_addr);
431 uint8_t bt_num = log_foreign_backtrace(s_raw_crash_data.backtrace, s_raw_crash_data.backtrace_count, 0);
432#if SOC_CPU_CORES_NUM > 1
433 if (s_raw_crash_data.other_backtrace_count > 0) {
435 ESP_LOGE(TAG,
" other core (%d):", 1 - s_raw_crash_data.crashed_core);
436 log_foreign_backtrace(s_raw_crash_data.other_backtrace, s_raw_crash_data.other_backtrace_count, bt_num);
452 ESP_LOGE(TAG,
"*** CRASH DETECTED ON PREVIOUS BOOT ***");
453 const char *reason = get_exception_reason();
454 if (reason !=
nullptr) {
455 ESP_LOGE(TAG,
" Reason: %s - %s (cause %" PRIu32
")", get_exception_type(), reason, s_raw_crash_data.cause);
457 ESP_LOGE(TAG,
" Reason: %s", get_exception_type());
459 ESP_LOGE(TAG,
" Crashed core: %d", s_raw_crash_data.crashed_core);
460 if (s_raw_crash_data.build_time != s_current_build_time) {
463 log_foreign_addresses();
466 ESP_LOGE(TAG,
" PC: 0x%08" PRIX32
" (fault location)", s_raw_crash_data.pc);
469 if (has_fault_addr()) {
470 ESP_LOGE(TAG,
" %s: 0x%08" PRIX32
" (faulting address)", FAULT_ADDR_REG, s_raw_crash_data.fault_addr);
472 log_backtrace(s_raw_crash_data.backtrace, s_raw_crash_data.backtrace_count, s_raw_crash_data.reg_frame_count);
474#if SOC_CPU_CORES_NUM > 1
475 if (s_raw_crash_data.other_backtrace_count > 0) {
476 int other_core = 1 - s_raw_crash_data.crashed_core;
477 ESP_LOGE(TAG,
" Other core (%d) backtrace:", other_core);
478 log_backtrace(s_raw_crash_data.other_backtrace, s_raw_crash_data.other_backtrace_count,
479 s_raw_crash_data.other_reg_frame_count);
486 static const char *
const ADDR2LINE_CMD =
"addr2line -pfiaC -e firmware.elf";
488 int pos = snprintf(hint,
sizeof(hint),
"Use: %s 0x%08" PRIX32, ADDR2LINE_CMD, s_raw_crash_data.pc);
489 append_addrs_to_hint(hint,
sizeof(hint),
pos, s_raw_crash_data.backtrace, s_raw_crash_data.backtrace_count,
490 s_raw_crash_data.reg_frame_count);
491 ESP_LOGE(TAG,
"%s", hint);
492#if SOC_CPU_CORES_NUM > 1
493 if (s_raw_crash_data.other_backtrace_count > 0) {
494 pos = snprintf(hint,
sizeof(hint),
"Other core: %s", ADDR2LINE_CMD);
495 append_addrs_to_hint(hint,
sizeof(hint),
pos, s_raw_crash_data.other_backtrace,
496 s_raw_crash_data.other_backtrace_count, s_raw_crash_data.other_reg_frame_count);
497 ESP_LOGE(TAG,
"%s", hint);
506static_assert(MAX_BACKTRACE <= CRASH_REPORT_MAX_BACKTRACE,
"Crash report backtrace too small for the record");
509static bool s_report_read =
false;
512static uint8_t copy_backtrace(
const uint32_t *addrs, uint8_t
count, [[maybe_unused]] uint8_t reg_frame_count,
514 uint8_t out_count = 0;
515 for (uint8_t i = 0; i <
count; i++) {
516#if CONFIG_IDF_TARGET_ARCH_RISCV
517 if (filter && i >= reg_frame_count && !is_return_addr(addrs[i]))
520 out[out_count++] = addrs[i];
526 switch (s_raw_crash_data.exception) {
527 case PANIC_EXCEPTION_ABORT:
529 case PANIC_EXCEPTION_TWDT:
531 case PANIC_EXCEPTION_IWDT:
543 const bool build_matches = s_raw_crash_data.build_time == esp32::s_current_build_time;
546 out.reason = get_crash_reason();
547 out.crashed_core = s_raw_crash_data.crashed_core;
548 out.pc = s_raw_crash_data.pc;
549 out.build_time = s_raw_crash_data.build_time;
551 if (s_raw_crash_data.exception != PANIC_EXCEPTION_ABORT && s_raw_crash_data.exception != PANIC_EXCEPTION_TWDT &&
552 !s_raw_crash_data.pseudo_excause && esp32::cause_slot_was_written()) {
554 out.cause = s_raw_crash_data.cause;
556 if (esp32::has_fault_addr()) {
558 out.fault_address = s_raw_crash_data.fault_addr;
560 auto &crashed =
out.cores[
out.core_count++];
561 crashed.core = s_raw_crash_data.crashed_core;
562 crashed.count = copy_backtrace(s_raw_crash_data.backtrace, s_raw_crash_data.backtrace_count,
563 s_raw_crash_data.reg_frame_count, build_matches, crashed.addresses.data());
564#if SOC_CPU_CORES_NUM > 1
565 if (s_raw_crash_data.other_backtrace_count > 0) {
566 auto &other =
out.cores[
out.core_count++];
567 other.core = 1 - s_raw_crash_data.crashed_core;
568 other.count = copy_backtrace(s_raw_crash_data.other_backtrace, s_raw_crash_data.other_backtrace_count,
569 s_raw_crash_data.other_reg_frame_count, build_matches, other.addresses.data());
576 s_report_read =
true;
597 s_raw_crash_data.pc = (
uint32_t) info->addr;
598 s_raw_crash_data.backtrace_count = 0;
599 s_raw_crash_data.reg_frame_count = 0;
600 s_raw_crash_data.exception = (uint8_t) info->exception;
601 s_raw_crash_data.pseudo_excause = info->pseudo_excause ? 1 : 0;
602 s_raw_crash_data.crashed_core = (uint8_t) info->core;
608 bool is_twdt = &g_twdt_isr !=
nullptr && g_twdt_isr;
609 s_raw_crash_data.exception = (uint8_t) (is_twdt ? PANIC_EXCEPTION_TWDT : PANIC_EXCEPTION_ABORT);
612 s_raw_crash_data.cause = 0;
613 s_raw_crash_data.fault_addr = 0;
618 s_raw_crash_data.build_time = esphome::esp32::s_current_build_time;
619#if SOC_CPU_CORES_NUM > 1
620 s_raw_crash_data.other_backtrace_count = 0;
621 s_raw_crash_data.other_reg_frame_count = 0;
624#if CONFIG_IDF_TARGET_ARCH_XTENSA
626 if (info->frame !=
nullptr) {
627 auto *xt_frame = (XtExcFrame *) info->frame;
629 if (info->pseudo_excause)
630 s_raw_crash_data.pc = xt_frame->pc;
631 if (!g_panic_abort) {
634 s_raw_crash_data.cause = xt_frame->exccause;
635 s_raw_crash_data.fault_addr = xt_frame->excvaddr;
637 s_raw_crash_data.backtrace_count = walk_xtensa_backtrace(xt_frame, s_raw_crash_data.backtrace, MAX_BACKTRACE);
640#if SOC_CPU_CORES_NUM > 1
644 if (info->core >= 0 && info->core < SOC_CPU_CORES_NUM) {
645 int other_core = 1 - info->core;
646 auto *other_frame = (XtExcFrame *) g_exc_frames[other_core];
647 if (other_frame !=
nullptr) {
648 s_raw_crash_data.other_backtrace_count =
649 walk_xtensa_backtrace(other_frame, s_raw_crash_data.other_backtrace, MAX_BACKTRACE);
654#elif CONFIG_IDF_TARGET_ARCH_RISCV
656 if (info->frame !=
nullptr) {
657 auto *rv_frame = (RvExcFrame *) info->frame;
658 if (!g_panic_abort) {
660 s_raw_crash_data.cause = rv_frame->mcause;
661 s_raw_crash_data.fault_addr = rv_frame->mtval;
663 s_raw_crash_data.backtrace_count =
664 capture_riscv_backtrace(rv_frame, s_raw_crash_data.backtrace, MAX_BACKTRACE, &s_raw_crash_data.reg_frame_count);
667#if SOC_CPU_CORES_NUM > 1
669 if (info->core >= 0 && info->core < SOC_CPU_CORES_NUM) {
670 int other_core = 1 - info->core;
671 auto *other_frame = (RvExcFrame *) g_exc_frames[other_core];
672 if (other_frame !=
nullptr) {
673 s_raw_crash_data.other_backtrace_count = capture_riscv_backtrace(
674 other_frame, s_raw_crash_data.other_backtrace, MAX_BACKTRACE, &s_raw_crash_data.other_reg_frame_count);
681 s_raw_crash_data.version = CRASH_DATA_VERSION;
682 s_raw_crash_data.magic = CRASH_MAGIC;
struct @67::@68 __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.
void crash_report_clear()
Mark the report read; it also no longer survives a reboot.
CrashReason
Sent as is over the native API.
@ CRASH_REASON_TASK_WATCHDOG
@ CRASH_REASON_INTERRUPT_WATCHDOG
@ CRASH_REPORT_FLAG_BUILD_MATCHES
@ CRASH_REPORT_FLAG_PRESENT
@ CRASH_REPORT_FLAG_CAUSE
@ CRASH_REPORT_FLAG_FAULT_ADDRESS
bool crash_report_get(CrashReport &out)
The previous boot's crash; false when there is none or it was read. Implemented per platform.