feat(Telemetry): backtrace, abort reason and pre-crash snapshot in crash reports (F-070)
Crash reports only carried the coredump's PC/cause/vaddr, which for every abort() is panic_abort()'s own write to 0x0 - so all aborts looked the same. - Read the coredump backtrace (up to 16 PCs) and crashing ELF SHA prefix - Wrap esp_system_abort (-Wl,--wrap) to keep the abort/assert/stack-overflow message in RTC_NOINIT memory across the reboot - TelemetryTask snapshots uptime/free heap/largest block/min free heap into the same RTC block every second - Reported in MQTT boot_report and SD boot log as crash.backtrace, crash.elf_sha256 and a new pre_crash object (additive) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -583,12 +583,9 @@ void CommunicationRouter::reportBootOnce() {
|
||||
payload["free_heap"] = (uint32_t)ESP.getFreeHeap();
|
||||
|
||||
const CrashReport& crash = _telemetry->getCrashReport();
|
||||
if (crash.available) {
|
||||
JsonObject crashObj = payload["crash"].to<JsonObject>();
|
||||
crashObj["task"] = crash.taskName;
|
||||
crashObj["pc"] = crash.pc;
|
||||
crashObj["exc_cause"] = crash.excCause;
|
||||
crashObj["exc_vaddr"] = crash.excVaddr;
|
||||
if (crash.available) _telemetry->writeCrashJson(payload["crash"].to<JsonObject>());
|
||||
if (_telemetry->getPreCrashSnapshot().available) {
|
||||
_telemetry->writePreCrashJson(payload["pre_crash"].to<JsonObject>());
|
||||
}
|
||||
|
||||
publishInfo("boot_report", payload);
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
#include "CrashBreadcrumbs.hpp"
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <esp_attr.h>
|
||||
#include <esp_heap_caps.h>
|
||||
#include <esp_system.h>
|
||||
|
||||
namespace {
|
||||
constexpr uint32_t SNAPSHOT_MAGIC = 0x56534E50; // "VSNP"
|
||||
constexpr uint32_t ABORT_MAGIC = 0x56414252; // "VABR"
|
||||
|
||||
struct RtcBreadcrumbs {
|
||||
uint32_t snapshotMagic;
|
||||
uint32_t uptimeMs;
|
||||
uint32_t freeHeap;
|
||||
uint32_t largestBlock;
|
||||
uint32_t minFreeHeap;
|
||||
uint32_t abortMagic;
|
||||
char abortMsg[sizeof(PreCrashSnapshot::abortMsg)];
|
||||
};
|
||||
|
||||
// Not zeroed on boot — that's the point. Validated by the magic words.
|
||||
RTC_NOINIT_ATTR RtcBreadcrumbs s_rtc;
|
||||
}
|
||||
|
||||
// Link-time wrapper: every esp_system_abort() call in the SDK (abort, assert,
|
||||
// stack-overflow hook) lands here first. Runs in whatever context aborted —
|
||||
// possibly with the heap lock held or the flash cache disabled — so it only
|
||||
// copies bytes into RTC memory: no allocation, no locks, no libc, IRAM only.
|
||||
extern "C" void __real_esp_system_abort(const char* details) __attribute__((noreturn));
|
||||
|
||||
extern "C" void IRAM_ATTR __attribute__((noreturn)) __wrap_esp_system_abort(const char* details) {
|
||||
size_t i = 0;
|
||||
if (details) {
|
||||
for (; i < sizeof(s_rtc.abortMsg) - 1 && details[i] != '\0'; i++) {
|
||||
s_rtc.abortMsg[i] = details[i];
|
||||
}
|
||||
}
|
||||
s_rtc.abortMsg[i] = '\0';
|
||||
s_rtc.abortMagic = ABORT_MAGIC;
|
||||
__real_esp_system_abort(details);
|
||||
}
|
||||
|
||||
namespace CrashBreadcrumbs {
|
||||
|
||||
void updateSnapshot() {
|
||||
s_rtc.uptimeMs = millis();
|
||||
s_rtc.freeHeap = heap_caps_get_free_size(MALLOC_CAP_8BIT);
|
||||
s_rtc.largestBlock = heap_caps_get_largest_free_block(MALLOC_CAP_8BIT);
|
||||
s_rtc.minFreeHeap = heap_caps_get_minimum_free_size(MALLOC_CAP_8BIT);
|
||||
s_rtc.snapshotMagic = SNAPSHOT_MAGIC;
|
||||
}
|
||||
|
||||
PreCrashSnapshot consume() {
|
||||
PreCrashSnapshot out;
|
||||
if (s_rtc.snapshotMagic == SNAPSHOT_MAGIC) {
|
||||
out.available = true;
|
||||
out.uptimeMs = s_rtc.uptimeMs;
|
||||
out.freeHeap = s_rtc.freeHeap;
|
||||
out.largestBlock = s_rtc.largestBlock;
|
||||
out.minFreeHeap = s_rtc.minFreeHeap;
|
||||
}
|
||||
if (s_rtc.abortMagic == ABORT_MAGIC) {
|
||||
out.available = true;
|
||||
memcpy(out.abortMsg, s_rtc.abortMsg, sizeof(out.abortMsg));
|
||||
out.abortMsg[sizeof(out.abortMsg) - 1] = '\0';
|
||||
}
|
||||
s_rtc.snapshotMagic = 0;
|
||||
s_rtc.abortMagic = 0;
|
||||
return out;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
#pragma once
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
// CRASH BREADCRUMBS — context that survives a panic reboot (F-070)
|
||||
// ════════════════════════════════════════════════════════════════════════════
|
||||
// The coredump summary (see Telemetry::captureCrashReport) only knows the CPU
|
||||
// state at the moment the chip went down. For an abort() — unhandled C++
|
||||
// exception, failed assert, FreeRTOS stack-overflow check — that state is
|
||||
// panic_abort()'s own deliberate write to address 0, so PC/cause/vaddr say
|
||||
// nothing about *why* (see F-055). Two things fill that gap, both kept in
|
||||
// RTC_NOINIT memory, which survives software/panic/watchdog resets but NOT
|
||||
// power-on or brownout:
|
||||
//
|
||||
// 1. The abort reason string — captured by wrapping esp_system_abort() at
|
||||
// link time (-Wl,--wrap=esp_system_abort in platformio.ini). Every abort
|
||||
// path in this SDK funnels through it: abort() ("abort() was called at PC
|
||||
// 0x... on core N" — that PC is abort()'s caller), assert ("assert failed:
|
||||
// func file:line (expr)"), and the FreeRTOS stack-overflow hook.
|
||||
// 2. A heap/uptime snapshot refreshed every second by TelemetryTask — the
|
||||
// last known state right before the reset (fragmentation killed us once).
|
||||
//
|
||||
// Both are consumed once at boot and cleared, so they're reported exactly once.
|
||||
|
||||
struct PreCrashSnapshot {
|
||||
bool available = false;
|
||||
uint32_t uptimeMs = 0; // millis() at the last snapshot before the reset
|
||||
uint32_t freeHeap = 0; // free heap at that snapshot
|
||||
uint32_t largestBlock = 0; // largest allocatable block at that snapshot
|
||||
uint32_t minFreeHeap = 0; // lowest free heap ever seen during that boot
|
||||
char abortMsg[160] = {0}; // empty unless the reset came from esp_system_abort()
|
||||
};
|
||||
|
||||
namespace CrashBreadcrumbs {
|
||||
// Refresh the heap/uptime snapshot. Cheap; called from TelemetryTask every 1s.
|
||||
void updateSnapshot();
|
||||
|
||||
// Read whatever the previous boot left behind, then clear it. Call once,
|
||||
// early in boot, and only trust the result on a fault-class reset — RTC
|
||||
// memory is garbage after a power-on, which the magic check guards against.
|
||||
PreCrashSnapshot consume();
|
||||
}
|
||||
Binary file not shown.
@@ -66,6 +66,7 @@
|
||||
// panic handler automatically on every crash (already enabled in this project's SDK
|
||||
// build; no custom panic hook needed). See CrashReport below.
|
||||
#include <esp_core_dump.h>
|
||||
#include "CrashBreadcrumbs.hpp"
|
||||
|
||||
// Most recent crash's coredump summary, captured once at begin() and erased from
|
||||
// flash immediately after reading so it's reported exactly once. Empty/zeroed
|
||||
@@ -77,6 +78,13 @@ struct CrashReport {
|
||||
uint32_t pc = 0; // Program counter at exception
|
||||
uint32_t excCause = 0; // Xtensa exception cause code
|
||||
uint32_t excVaddr = 0; // Faulting virtual address
|
||||
// F-070: backtrace of the faulting task (decode with addr2line against the
|
||||
// ELF whose SHA matches elfSha). For an abort() the first frames are the
|
||||
// abort machinery; the real call site follows.
|
||||
uint32_t backtrace[16] = {0};
|
||||
uint8_t backtraceDepth = 0;
|
||||
bool backtraceCorrupted = false;
|
||||
char elfSha[APP_ELF_SHA256_SZ] = {0}; // first 16 hex chars of the crashing app's ELF SHA-256
|
||||
};
|
||||
|
||||
// Maximum number of boot log entries retained on SD (rotating)
|
||||
@@ -154,6 +162,10 @@ private:
|
||||
// Most recent crash's coredump summary, if any — read once at begin().
|
||||
CrashReport crashReport;
|
||||
|
||||
// Abort reason + last heap/uptime snapshot from before a fault reset, if
|
||||
// the previous boot left them in RTC memory (F-070). See CrashBreadcrumbs.hpp.
|
||||
PreCrashSnapshot preCrash;
|
||||
|
||||
// Diagnostics — CPU temperature sampled locally every 15s, reduced to
|
||||
// min/max/avg between 5-minute batch reports. See RunningStats above for
|
||||
// why only temperature needs local sampling (stack high-water and the
|
||||
@@ -235,6 +247,12 @@ public:
|
||||
// if one was captured. available == false when the last reset wasn't a crash,
|
||||
// or no coredump was found for one that was.
|
||||
const CrashReport& getCrashReport() const { return crashReport; }
|
||||
const PreCrashSnapshot& getPreCrashSnapshot() const { return preCrash; }
|
||||
|
||||
// Serialize the above into the shared JSON shape used by the MQTT
|
||||
// boot_report and the SD boot log. Callers check .available first.
|
||||
void writeCrashJson(JsonObject out) const;
|
||||
void writePreCrashJson(JsonObject out) const;
|
||||
|
||||
// Reset reason (captured once at begin(), before anything else can crash again)
|
||||
esp_reset_reason_t getResetReason() const { return resetReason; }
|
||||
|
||||
Reference in New Issue
Block a user