mirror of
https://github.com/cesanta/mongoose.git
synced 2026-10-26 12:14:56 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
361a70517e | ||
|
|
278c4ca116 |
No files matched your search
@@ -637,6 +637,7 @@ jobs:
|
||||
example:
|
||||
- path: micropython/esp32
|
||||
- path: esp32/uart-bridge
|
||||
- path: esp32/minimal
|
||||
name: ${{ matrix.example.path }}
|
||||
env:
|
||||
GO: 0
|
||||
|
||||
@@ -186,6 +186,7 @@ jobs:
|
||||
matrix:
|
||||
example:
|
||||
- path: esp32/uart-bridge
|
||||
- path: esp32/minimal
|
||||
name: ${{ matrix.example.path }}
|
||||
env:
|
||||
GO: 0
|
||||
|
||||
+137
-37
@@ -3641,12 +3641,66 @@ struct mg_fs mg_fs_posix = {p_stat, p_list, p_open, p_close, p_read,
|
||||
|
||||
|
||||
|
||||
#if MG_ENABLE_HEALTH
|
||||
#if MG_ARCH == MG_ARCH_ESP32
|
||||
// reusable for other platforms, currently ESP32-only
|
||||
struct mg_health_coredump {
|
||||
const void *ptr;
|
||||
size_t poffset;
|
||||
size_t size;
|
||||
};
|
||||
|
||||
static struct mg_health_coredump s_coredump;
|
||||
#endif
|
||||
|
||||
bool mg_health_coredump_begin(size_t *size) {
|
||||
#if MG_ARCH == MG_ARCH_ESP32 && CONFIG_ESP_COREDUMP_ENABLE_TO_FLASH
|
||||
const esp_partition_t *p = esp_partition_find_first(
|
||||
ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_COREDUMP, NULL);
|
||||
size_t address = 0, len = 0;
|
||||
esp_err_t err = esp_core_dump_image_get(&address, &len);
|
||||
if (size == NULL || s_coredump.ptr != NULL || err != ESP_OK || p == NULL)
|
||||
return false;
|
||||
s_coredump.ptr = p;
|
||||
s_coredump.poffset = address - p->address;
|
||||
s_coredump.size = len;
|
||||
*size = len;
|
||||
return true;
|
||||
#else
|
||||
(void) size;
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
bool mg_health_coredump_read(size_t offset, void *buf, size_t *len) {
|
||||
#if MG_ARCH == MG_ARCH_ESP32 && CONFIG_ESP_COREDUMP_ENABLE_TO_FLASH
|
||||
const esp_partition_t *p = (const esp_partition_t *) s_coredump.ptr;
|
||||
if (p == NULL || offset > s_coredump.size)
|
||||
return false;
|
||||
if (*len > s_coredump.size - offset) *len = s_coredump.size - offset;
|
||||
return esp_partition_read(p, s_coredump.poffset + offset, buf, *len) == ESP_OK;
|
||||
#else
|
||||
(void) offset, (void) buf, (void) len;
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
bool mg_health_coredump_end(void) {
|
||||
#if MG_ARCH == MG_ARCH_ESP32 && CONFIG_ESP_COREDUMP_ENABLE_TO_FLASH
|
||||
bool ok = s_coredump.ptr != NULL;
|
||||
memset(&s_coredump, 0, sizeof(s_coredump));
|
||||
return ok;
|
||||
#else
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
// The one health record. Lives in RAM that survives a warm reset, see
|
||||
// MG_HEALTH_RAM and the .mg_health region in the linker script
|
||||
struct mg_health mg_health_record MG_HEALTH_RAM;
|
||||
|
||||
// STM32 H723 support. Needs the _estack symbol from the linker script
|
||||
#if MG_ENABLE_HEALTH && (defined(STM32H7_DEV_ID) || defined(STM32H5) || defined(STM32C5))
|
||||
// STM32 H7, H5, C5 support. Needs the _estack symbol from the linker script
|
||||
#if defined(STM32H7_DEV_ID) || defined(STM32H5) || defined(STM32C5)
|
||||
// Fault handler body. Runs in exception context: no printf, no malloc, no
|
||||
// blocking calls. Copies the unwind registers and raw stack into the health
|
||||
// record, then resets.
|
||||
@@ -3688,52 +3742,57 @@ void BusFault_Handler(void) __attribute__((alias("HardFault_Handler")));
|
||||
void UsageFault_Handler(void) __attribute__((alias("HardFault_Handler")));
|
||||
#endif
|
||||
|
||||
// Start a new boot: validate the record, count the boot, store the reset
|
||||
// reason. The current boot moves to prev_* slots first, so the previous
|
||||
// boot's uptime and reason stay readable. Called by MG_HEALTH_INIT()
|
||||
// Start a new boot: validate the record. Called by MG_HEALTH_INIT()
|
||||
void mg_health_init(void) {
|
||||
char magic[] = MG_HEALTH_MAGIC;
|
||||
// struct mg_health *h = mg_health_get();
|
||||
if (!mg_health_valid()) { // First boot, or RAM lost its contents
|
||||
memset(&mg_health_record, 0, sizeof(mg_health_record));
|
||||
memcpy(mg_health_record.magic, magic, sizeof(mg_health_record.magic));
|
||||
}
|
||||
|
||||
#if 0 && MG_ARCH == MG_ARCH_ESP32 && CONFIG_ESP_COREDUMP_ENABLE_TO_FLASH
|
||||
{
|
||||
#if MG_ARCH == MG_ARCH_ESP32
|
||||
#if CONFIG_IDF_TARGET_ARCH_XTENSA
|
||||
mg_health_record.arch = (uint16_t) MG_HEALTH_ARCH_XTENSA;
|
||||
#elif CONFIG_IDF_TARGET_ARCH_RISCV
|
||||
mg_health_record.arch = (uint16_t) MG_HEALTH_ARCH_RISCV;
|
||||
#endif
|
||||
esp_reset_reason_t rr = esp_reset_reason();
|
||||
mg_health_record.reset_reason = esp2mg(rr);
|
||||
#if CONFIG_ESP_COREDUMP_ENABLE_TO_FLASH == 0
|
||||
MG_ERROR(("MG_HEALTH is enabled but CONFIG_ESP_COREDUMP_ENABLE_TO_FLASH=n"));
|
||||
#else
|
||||
// After a crash, copy the backtrace from the core dump in flash: the same
|
||||
// addresses the panic handler prints as "Backtrace: PC:SP PC:SP ...".
|
||||
// The core dump stays in flash, so check the reset reason, otherwise an
|
||||
// old crash is reported on every boot
|
||||
static esp_core_dump_summary_t s; // Big on RISC-V, keep it off the stack
|
||||
esp_reset_reason_t rr = esp_reset_reason();
|
||||
esp_core_dump_summary_t *s = calloc(1, sizeof(*s));
|
||||
uint32_t *out = mg_health_record.data;
|
||||
size_t n = 0, max = MG_HEALTH_DATA_SIZE;
|
||||
if (rr != ESP_RST_PANIC && rr != ESP_RST_INT_WDT && rr != ESP_RST_TASK_WDT)
|
||||
return; // Not a crash that went through the panic handler
|
||||
if (max == 0 || esp_core_dump_get_summary(&s) != ESP_OK) return;
|
||||
|
||||
mg_health_record.cpuid = 42; // ESP32
|
||||
mg_health_record.reset_reason = MG_HEALTH_RESET_FAULT;
|
||||
|
||||
size_t n = 0;
|
||||
memset(out, 0, sizeof(mg_health_record.data));
|
||||
out[n++] = s.exc_pc;
|
||||
if (s == NULL) return;
|
||||
if ((rr != ESP_RST_PANIC && rr != ESP_RST_INT_WDT &&
|
||||
rr != ESP_RST_TASK_WDT) // Not a crash caught by the panic handler
|
||||
|| esp_core_dump_get_summary(s) != ESP_OK) {
|
||||
free(s);
|
||||
return;
|
||||
}
|
||||
out[n++] = s->exc_pc;
|
||||
memcpy(&out[n], s->exc_task, 16), n += 4;
|
||||
#if CONFIG_IDF_TARGET_ARCH_XTENSA
|
||||
for (uint32_t i = 0; i < s.exc_bt_info.depth && n < max; i++) {
|
||||
if (s.exc_bt_info.bt[i] != s.exc_pc) out[n++] = s.exc_bt_info.bt[i];
|
||||
}
|
||||
out[n++] = s->ex_info.exc_a[0];
|
||||
out[n++] = s->ex_info.exc_a[1];
|
||||
size_t len = s->exc_bt_info.depth;
|
||||
if (len > MG_HEALTH_DATA_SIZE - n) len = (MG_HEALTH_DATA_SIZE - n);
|
||||
memcpy(&out[n], s->exc_bt_info.bt, 4 * len); // sizeof(uint32_t)
|
||||
#elif CONFIG_IDF_TARGET_ARCH_RISCV
|
||||
// No unwinder: take the return address, then words on the stack that
|
||||
// point into executable memory (a heuristic, may include stale values)
|
||||
if (n < max) out[n++] = s.ex_info.ra;
|
||||
for (uint32_t i = 0; i + 4 <= s.exc_bt_info.dump_size && n < max; i += 4) {
|
||||
uint32_t w;
|
||||
memcpy(&w, &s.exc_bt_info.stackdump[i], sizeof(w)); // Byte array
|
||||
if (esp_ptr_executable((void *) (uintptr_t) w)) out[n++] = w;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
out[n++] = s->ex_info.exc_ra;
|
||||
out[n++] = s->ex_info.exc_sp;
|
||||
size_t len = s->exc_bt_info.dump_size / 4 ; // sizeof(uint32_t)
|
||||
if (len > MG_HEALTH_DATA_SIZE - n) len = (MG_HEALTH_DATA_SIZE - n);
|
||||
memcpy(&out[n], s->exc_bt_info.stackdump, 4 * len);
|
||||
#endif
|
||||
free(s);
|
||||
#endif // CONFIG_ESP_COREDUMP_ENABLE_TO_FLASH
|
||||
#endif // MG_ARCH_ESP32
|
||||
}
|
||||
|
||||
static size_t print_uint32(void (*fn)(char, void *), void *arg, va_list *ap) {
|
||||
@@ -3747,11 +3806,17 @@ static size_t print_uint32(void (*fn)(char, void *), void *arg, va_list *ap) {
|
||||
size_t mg_print_crash_record(void (*fn)(char, void *), void *arg, va_list *ap) {
|
||||
size_t len = 0;
|
||||
if (mg_health_valid() &&
|
||||
mg_health_record.reset_reason == MG_HEALTH_RESET_FAULT) {
|
||||
(mg_health_record.reset_reason == (uint16_t) MG_HEALTH_RESET_FAULT
|
||||
#if MG_ARCH == MG_ARCH_ESP32
|
||||
|| mg_health_record.reset_reason == (uint16_t) MG_HEALTH_RESET_WATCHDOG
|
||||
#endif
|
||||
)) {
|
||||
size_t n = sizeof(mg_health_record.data) / sizeof(mg_health_record.data[0]);
|
||||
len += mg_xprintf(fn, arg, "{%m:%hhu,%m:%u,%m:[%M]}", //
|
||||
MG_ESC("version"), mg_health_record.magic[3], //
|
||||
MG_ESC("cpuid"), mg_health_record.cpuid, //
|
||||
len += mg_xprintf(fn, arg, "{%m:%hhu,%m:%hu,%m:%hu,%m:%u,%m:[%M]}", //
|
||||
MG_ESC("version"), mg_health_record.magic[3], //
|
||||
MG_ESC("arch"), mg_health_record.arch, //
|
||||
MG_ESC("reason"), mg_health_record.reset_reason, //
|
||||
MG_ESC("cpuid"), mg_health_record.cpuid, //
|
||||
MG_ESC("data"), print_uint32, n, mg_health_record.data);
|
||||
} else {
|
||||
len += mg_xprintf(fn, arg, "{}");
|
||||
@@ -3759,6 +3824,7 @@ size_t mg_print_crash_record(void (*fn)(char, void *), void *arg, va_list *ap) {
|
||||
(void) ap;
|
||||
return len;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef MG_ENABLE_LINES
|
||||
#line 1 "src/http.c"
|
||||
@@ -7338,6 +7404,7 @@ static void mg_mdash_fn(struct mg_connection *c, int ev, void *ev_data) {
|
||||
}
|
||||
mg_iobuf_free(&io);
|
||||
} else if (ev == MG_EV_CLOSE) {
|
||||
mg_health_coredump_end();
|
||||
mg_mdash_conn = NULL;
|
||||
MG_INFO(("disconnected, ram %lu", 0));
|
||||
}
|
||||
@@ -7395,6 +7462,35 @@ static void mg_mdash_rpc_ota_end(struct mg_rpc_req *r) {
|
||||
// MG_ESC("percent"));
|
||||
}
|
||||
|
||||
static void rpc_coredump_begin(struct mg_rpc_req *r) {
|
||||
size_t size = 0;
|
||||
if (mg_health_coredump_begin(&size)) {
|
||||
mg_rpc_ok(r, "{%m:%llu}", MG_ESC("size"), (uint64_t) size);
|
||||
} else {
|
||||
mg_rpc_err(r, 500, "%m", MG_ESC("failed"));
|
||||
}
|
||||
}
|
||||
|
||||
static void rpc_coredump_read(struct mg_rpc_req *r) {
|
||||
uint8_t buf[768];
|
||||
size_t len = sizeof(buf);
|
||||
size_t offset = (size_t) mg_json_get_long(r->frame, "$.params.offset", -1);
|
||||
if (mg_health_coredump_read(offset, buf, &len)) {
|
||||
mg_rpc_ok(r, "{%m:\"%M\"}", MG_ESC("data"), mg_print_base64,
|
||||
(int) len, buf);
|
||||
} else {
|
||||
mg_rpc_err(r, 500, "%m", MG_ESC("failed"));
|
||||
}
|
||||
}
|
||||
|
||||
static void rpc_coredump_end(struct mg_rpc_req *r) {
|
||||
if (mg_health_coredump_end()) {
|
||||
mg_rpc_ok(r, "true");
|
||||
} else {
|
||||
mg_rpc_err(r, 500, "%m", MG_ESC("failed"));
|
||||
}
|
||||
}
|
||||
|
||||
void mg_mdash_init(struct mg_mgr *mgr) {
|
||||
MG_INFO(("Initialising mdash agent, key [%s]", MG_MDASH_KEY));
|
||||
mg_rpc_add(&mgr->rpcs, mg_str("RPC.List"), mg_rpc_list, NULL);
|
||||
@@ -7402,6 +7498,9 @@ void mg_mdash_init(struct mg_mgr *mgr) {
|
||||
mg_rpc_add(&mgr->rpcs, mg_str("OTA.Begin"), mg_mdash_rpc_ota_begin, NULL);
|
||||
mg_rpc_add(&mgr->rpcs, mg_str("OTA.Write"), mg_mdash_rpc_ota_write, NULL);
|
||||
mg_rpc_add(&mgr->rpcs, mg_str("OTA.End"), mg_mdash_rpc_ota_end, NULL);
|
||||
mg_rpc_add(&mgr->rpcs, mg_str("CoreDump.Begin"), rpc_coredump_begin, NULL);
|
||||
mg_rpc_add(&mgr->rpcs, mg_str("CoreDump.Read"), rpc_coredump_read, NULL);
|
||||
mg_rpc_add(&mgr->rpcs, mg_str("CoreDump.End"), rpc_coredump_end, NULL);
|
||||
}
|
||||
|
||||
void mg_mdash_poll(struct mg_mgr *mgr) {
|
||||
@@ -34937,6 +35036,7 @@ struct mg_tcpip_driver mg_tcpip_driver_stm32f = {
|
||||
|
||||
#if MG_ENABLE_TCPIP && (MG_ENABLE_DRIVER_STM32H || MG_ENABLE_DRIVER_MCXN || \
|
||||
MG_ENABLE_DRIVER_STM32N)
|
||||
// STM32C: vendor modded single-queue Synopsys v5.4, same settings as STM32H
|
||||
// STM32H: vendor modded single-queue Synopsys v4.2
|
||||
// STM32N: dual-queue GbE Synopsys v5.2 with no hash table option, 64-bit AXI
|
||||
// MCXNx4x: dual-queue Synopsys v5.2 with no hash table option
|
||||
|
||||
+85
-7
@@ -253,8 +253,11 @@ extern "C" {
|
||||
#include <esp_system.h>
|
||||
#include <esp_mac.h>
|
||||
#include <esp_idf_version.h>
|
||||
// #include <esp_memory_utils.h> // For health record coredumps
|
||||
// #include <esp_core_dump.h> // For health record coredumps
|
||||
#if MG_ENABLE_HEALTH
|
||||
#include <esp_memory_utils.h>
|
||||
#include <esp_core_dump.h>
|
||||
#include <esp_partition.h>
|
||||
#endif
|
||||
|
||||
#define MG_PATH_MAX 128
|
||||
|
||||
@@ -4371,6 +4374,8 @@ void mg_ota_poll(struct mg_mgr *);
|
||||
|
||||
|
||||
|
||||
#if MG_ENABLE_HEALTH
|
||||
|
||||
#ifndef MG_HEALTH_MAGIC
|
||||
#define MG_HEALTH_MAGIC {'M', 'G', 'H', '3'} // '3' is the layout version
|
||||
#endif
|
||||
@@ -4379,13 +4384,15 @@ void mg_ota_poll(struct mg_mgr *);
|
||||
#define MG_HEALTH_RAM __attribute__((section(".mg_health")))
|
||||
#endif
|
||||
|
||||
#if !defined(MG_HEALTH_DATA_SIZE) && (defined(STM32H7_DEV_ID) || defined(STM32H5) || defined(STM32C5))
|
||||
#define MG_HEALTH_DATA_SIZE 250
|
||||
#endif
|
||||
|
||||
#ifndef MG_HEALTH_DATA_SIZE
|
||||
#if (defined(STM32H7_DEV_ID) || defined(STM32H5)) || defined(STM32C5)
|
||||
#define MG_HEALTH_DATA_SIZE 250
|
||||
#elif MG_ARCH == MG_ARCH_ESP32
|
||||
#define MG_HEALTH_DATA_SIZE 23
|
||||
#else
|
||||
#define MG_HEALTH_DATA_SIZE 10
|
||||
#endif
|
||||
#endif // MG_HEALTH_DATA_SIZE
|
||||
|
||||
// Places the record in RAM that survives a reset. Define in mongoose_config.h
|
||||
// to match the linker script, e.g. __attribute__((section(".mg_health")))
|
||||
@@ -4393,6 +4400,13 @@ void mg_ota_poll(struct mg_mgr *);
|
||||
#define MG_HEALTH_RAM
|
||||
#endif
|
||||
|
||||
// CPU core architecture
|
||||
enum mg_health_arch {
|
||||
MG_HEALTH_ARCH_ARM = 0,
|
||||
MG_HEALTH_ARCH_XTENSA,
|
||||
MG_HEALTH_ARCH_RISCV
|
||||
};
|
||||
|
||||
// Why the device restarted. Hardware usually reports several flags at once,
|
||||
// so these are ordered by how much they tell you: the most specific cause a
|
||||
// device reports wins
|
||||
@@ -4409,7 +4423,8 @@ enum mg_health_reason {
|
||||
|
||||
struct mg_health {
|
||||
char magic[4]; // MG_HEALTH_MAGIC when the record holds valid data
|
||||
uint32_t reset_reason; // enum mg_health_reason that started this boot
|
||||
uint16_t reset_reason; // enum mg_health_reason that started this boot
|
||||
uint16_t arch; // enum mg_health_arch, core architecture (ARM, etc)
|
||||
uint32_t cpuid; // ARM CPU ID
|
||||
uint32_t data[MG_HEALTH_DATA_SIZE];
|
||||
};
|
||||
@@ -4442,9 +4457,72 @@ static inline const char *mg_health_reason_str(int reason) {
|
||||
}
|
||||
}
|
||||
|
||||
#if MG_ARCH == MG_ARCH_ESP32
|
||||
#ifndef CONFIG_ESP_COREDUMP_ENABLE_TO_FLASH
|
||||
#define CONFIG_ESP_COREDUMP_ENABLE_TO_FLASH 0
|
||||
#endif
|
||||
#ifndef ESP_PARTITION_SUBTYPE_DATA_COREDUMP
|
||||
#define ESP_PARTITION_SUBTYPE_DATA_COREDUMP 0x03
|
||||
#endif
|
||||
// clang-format off
|
||||
static inline uint16_t esp2mg(esp_reset_reason_t rr) {
|
||||
switch(rr) {
|
||||
case ESP_RST_POWERON: return (uint16_t) MG_HEALTH_RESET_POWER;
|
||||
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 2, 2)
|
||||
case ESP_RST_PWR_GLITCH:
|
||||
#endif
|
||||
case ESP_RST_BROWNOUT: return (uint16_t) MG_HEALTH_RESET_BROWNOUT;
|
||||
case ESP_RST_EXT: return (uint16_t) MG_HEALTH_RESET_PIN;
|
||||
case ESP_RST_SW: return (uint16_t) MG_HEALTH_RESET_SOFTWARE;
|
||||
case ESP_RST_DEEPSLEEP: return (uint16_t) MG_HEALTH_RESET_LOWPOWER;
|
||||
case ESP_RST_INT_WDT:
|
||||
case ESP_RST_TASK_WDT:
|
||||
case ESP_RST_WDT: return (uint16_t) MG_HEALTH_RESET_WATCHDOG;
|
||||
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 2, 2)
|
||||
case ESP_RST_CPU_LOCKUP:
|
||||
#endif
|
||||
case ESP_RST_PANIC: return (uint16_t) MG_HEALTH_RESET_FAULT;
|
||||
default: break;
|
||||
}
|
||||
return (uint16_t) MG_HEALTH_RESET_UNKNOWN;
|
||||
}
|
||||
// clang-format on
|
||||
#endif
|
||||
|
||||
#define MG_HEALTH_INIT() mg_health_init()
|
||||
extern size_t mg_print_crash_record(mg_pfn_t, void *, va_list *);
|
||||
extern void mg_health_init(void);
|
||||
extern bool mg_health_coredump_begin(size_t *size);
|
||||
extern bool mg_health_coredump_read(size_t offset, void *buf, size_t *len);
|
||||
extern bool mg_health_coredump_end(void);
|
||||
#else
|
||||
#define MG_HEALTH_INIT()
|
||||
static inline int mg_health_reason(void) {
|
||||
return 0;
|
||||
}
|
||||
static inline const char *mg_health_reason_str(int reason) {
|
||||
(void) reason;
|
||||
return "unknown";
|
||||
}
|
||||
static inline size_t mg_print_crash_record(mg_pfn_t fn, void *arg,
|
||||
va_list *ap) {
|
||||
(void) ap;
|
||||
return mg_xprintf(fn, arg, "{}");
|
||||
}
|
||||
static inline bool mg_health_coredump_begin(size_t *size) {
|
||||
(void) size;
|
||||
return false;
|
||||
}
|
||||
static inline bool mg_health_coredump_read(size_t offset, void *buf,
|
||||
size_t *len) {
|
||||
(void) offset, (void) buf, (void) len;
|
||||
return false;
|
||||
}
|
||||
static inline bool mg_health_coredump_end(void) {
|
||||
return false;
|
||||
}
|
||||
|
||||
#endif
|
||||
// Cloud connector API to https://mdash.net - device management cloud
|
||||
|
||||
|
||||
|
||||
+5
-2
@@ -22,8 +22,11 @@
|
||||
#include <esp_system.h>
|
||||
#include <esp_mac.h>
|
||||
#include <esp_idf_version.h>
|
||||
// #include <esp_memory_utils.h> // For health record coredumps
|
||||
// #include <esp_core_dump.h> // For health record coredumps
|
||||
#if MG_ENABLE_HEALTH
|
||||
#include <esp_memory_utils.h>
|
||||
#include <esp_core_dump.h>
|
||||
#include <esp_partition.h>
|
||||
#endif
|
||||
|
||||
#define MG_PATH_MAX 128
|
||||
|
||||
|
||||
+103
-37
@@ -1,12 +1,66 @@
|
||||
#include "config.h"
|
||||
#include "health.h"
|
||||
|
||||
#if MG_ENABLE_HEALTH
|
||||
#if MG_ARCH == MG_ARCH_ESP32
|
||||
// reusable for other platforms, currently ESP32-only
|
||||
struct mg_health_coredump {
|
||||
const void *ptr;
|
||||
size_t poffset;
|
||||
size_t size;
|
||||
};
|
||||
|
||||
static struct mg_health_coredump s_coredump;
|
||||
#endif
|
||||
|
||||
bool mg_health_coredump_begin(size_t *size) {
|
||||
#if MG_ARCH == MG_ARCH_ESP32 && CONFIG_ESP_COREDUMP_ENABLE_TO_FLASH
|
||||
const esp_partition_t *p = esp_partition_find_first(
|
||||
ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_COREDUMP, NULL);
|
||||
size_t address = 0, len = 0;
|
||||
esp_err_t err = esp_core_dump_image_get(&address, &len);
|
||||
if (size == NULL || s_coredump.ptr != NULL || err != ESP_OK || p == NULL)
|
||||
return false;
|
||||
s_coredump.ptr = p;
|
||||
s_coredump.poffset = address - p->address;
|
||||
s_coredump.size = len;
|
||||
*size = len;
|
||||
return true;
|
||||
#else
|
||||
(void) size;
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
bool mg_health_coredump_read(size_t offset, void *buf, size_t *len) {
|
||||
#if MG_ARCH == MG_ARCH_ESP32 && CONFIG_ESP_COREDUMP_ENABLE_TO_FLASH
|
||||
const esp_partition_t *p = (const esp_partition_t *) s_coredump.ptr;
|
||||
if (p == NULL || offset > s_coredump.size)
|
||||
return false;
|
||||
if (*len > s_coredump.size - offset) *len = s_coredump.size - offset;
|
||||
return esp_partition_read(p, s_coredump.poffset + offset, buf, *len) == ESP_OK;
|
||||
#else
|
||||
(void) offset, (void) buf, (void) len;
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
bool mg_health_coredump_end(void) {
|
||||
#if MG_ARCH == MG_ARCH_ESP32 && CONFIG_ESP_COREDUMP_ENABLE_TO_FLASH
|
||||
bool ok = s_coredump.ptr != NULL;
|
||||
memset(&s_coredump, 0, sizeof(s_coredump));
|
||||
return ok;
|
||||
#else
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
// The one health record. Lives in RAM that survives a warm reset, see
|
||||
// MG_HEALTH_RAM and the .mg_health region in the linker script
|
||||
struct mg_health mg_health_record MG_HEALTH_RAM;
|
||||
|
||||
// STM32 H723 support. Needs the _estack symbol from the linker script
|
||||
#if MG_ENABLE_HEALTH && (defined(STM32H7_DEV_ID) || defined(STM32H5) || defined(STM32C5))
|
||||
// STM32 H7, H5, C5 support. Needs the _estack symbol from the linker script
|
||||
#if defined(STM32H7_DEV_ID) || defined(STM32H5) || defined(STM32C5)
|
||||
// Fault handler body. Runs in exception context: no printf, no malloc, no
|
||||
// blocking calls. Copies the unwind registers and raw stack into the health
|
||||
// record, then resets.
|
||||
@@ -48,52 +102,57 @@ void BusFault_Handler(void) __attribute__((alias("HardFault_Handler")));
|
||||
void UsageFault_Handler(void) __attribute__((alias("HardFault_Handler")));
|
||||
#endif
|
||||
|
||||
// Start a new boot: validate the record, count the boot, store the reset
|
||||
// reason. The current boot moves to prev_* slots first, so the previous
|
||||
// boot's uptime and reason stay readable. Called by MG_HEALTH_INIT()
|
||||
// Start a new boot: validate the record. Called by MG_HEALTH_INIT()
|
||||
void mg_health_init(void) {
|
||||
char magic[] = MG_HEALTH_MAGIC;
|
||||
// struct mg_health *h = mg_health_get();
|
||||
if (!mg_health_valid()) { // First boot, or RAM lost its contents
|
||||
memset(&mg_health_record, 0, sizeof(mg_health_record));
|
||||
memcpy(mg_health_record.magic, magic, sizeof(mg_health_record.magic));
|
||||
}
|
||||
|
||||
#if 0 && MG_ARCH == MG_ARCH_ESP32 && CONFIG_ESP_COREDUMP_ENABLE_TO_FLASH
|
||||
{
|
||||
#if MG_ARCH == MG_ARCH_ESP32
|
||||
#if CONFIG_IDF_TARGET_ARCH_XTENSA
|
||||
mg_health_record.arch = (uint16_t) MG_HEALTH_ARCH_XTENSA;
|
||||
#elif CONFIG_IDF_TARGET_ARCH_RISCV
|
||||
mg_health_record.arch = (uint16_t) MG_HEALTH_ARCH_RISCV;
|
||||
#endif
|
||||
esp_reset_reason_t rr = esp_reset_reason();
|
||||
mg_health_record.reset_reason = esp2mg(rr);
|
||||
#if CONFIG_ESP_COREDUMP_ENABLE_TO_FLASH == 0
|
||||
MG_ERROR(("MG_HEALTH is enabled but CONFIG_ESP_COREDUMP_ENABLE_TO_FLASH=n"));
|
||||
#else
|
||||
// After a crash, copy the backtrace from the core dump in flash: the same
|
||||
// addresses the panic handler prints as "Backtrace: PC:SP PC:SP ...".
|
||||
// The core dump stays in flash, so check the reset reason, otherwise an
|
||||
// old crash is reported on every boot
|
||||
static esp_core_dump_summary_t s; // Big on RISC-V, keep it off the stack
|
||||
esp_reset_reason_t rr = esp_reset_reason();
|
||||
esp_core_dump_summary_t *s = calloc(1, sizeof(*s));
|
||||
uint32_t *out = mg_health_record.data;
|
||||
size_t n = 0, max = MG_HEALTH_DATA_SIZE;
|
||||
if (rr != ESP_RST_PANIC && rr != ESP_RST_INT_WDT && rr != ESP_RST_TASK_WDT)
|
||||
return; // Not a crash that went through the panic handler
|
||||
if (max == 0 || esp_core_dump_get_summary(&s) != ESP_OK) return;
|
||||
|
||||
mg_health_record.cpuid = 42; // ESP32
|
||||
mg_health_record.reset_reason = MG_HEALTH_RESET_FAULT;
|
||||
|
||||
size_t n = 0;
|
||||
memset(out, 0, sizeof(mg_health_record.data));
|
||||
out[n++] = s.exc_pc;
|
||||
if (s == NULL) return;
|
||||
if ((rr != ESP_RST_PANIC && rr != ESP_RST_INT_WDT &&
|
||||
rr != ESP_RST_TASK_WDT) // Not a crash caught by the panic handler
|
||||
|| esp_core_dump_get_summary(s) != ESP_OK) {
|
||||
free(s);
|
||||
return;
|
||||
}
|
||||
out[n++] = s->exc_pc;
|
||||
memcpy(&out[n], s->exc_task, 16), n += 4;
|
||||
#if CONFIG_IDF_TARGET_ARCH_XTENSA
|
||||
for (uint32_t i = 0; i < s.exc_bt_info.depth && n < max; i++) {
|
||||
if (s.exc_bt_info.bt[i] != s.exc_pc) out[n++] = s.exc_bt_info.bt[i];
|
||||
}
|
||||
out[n++] = s->ex_info.exc_a[0];
|
||||
out[n++] = s->ex_info.exc_a[1];
|
||||
size_t len = s->exc_bt_info.depth;
|
||||
if (len > MG_HEALTH_DATA_SIZE - n) len = (MG_HEALTH_DATA_SIZE - n);
|
||||
memcpy(&out[n], s->exc_bt_info.bt, 4 * len); // sizeof(uint32_t)
|
||||
#elif CONFIG_IDF_TARGET_ARCH_RISCV
|
||||
// No unwinder: take the return address, then words on the stack that
|
||||
// point into executable memory (a heuristic, may include stale values)
|
||||
if (n < max) out[n++] = s.ex_info.ra;
|
||||
for (uint32_t i = 0; i + 4 <= s.exc_bt_info.dump_size && n < max; i += 4) {
|
||||
uint32_t w;
|
||||
memcpy(&w, &s.exc_bt_info.stackdump[i], sizeof(w)); // Byte array
|
||||
if (esp_ptr_executable((void *) (uintptr_t) w)) out[n++] = w;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
out[n++] = s->ex_info.exc_ra;
|
||||
out[n++] = s->ex_info.exc_sp;
|
||||
size_t len = s->exc_bt_info.dump_size / 4 ; // sizeof(uint32_t)
|
||||
if (len > MG_HEALTH_DATA_SIZE - n) len = (MG_HEALTH_DATA_SIZE - n);
|
||||
memcpy(&out[n], s->exc_bt_info.stackdump, 4 * len);
|
||||
#endif
|
||||
free(s);
|
||||
#endif // CONFIG_ESP_COREDUMP_ENABLE_TO_FLASH
|
||||
#endif // MG_ARCH_ESP32
|
||||
}
|
||||
|
||||
static size_t print_uint32(void (*fn)(char, void *), void *arg, va_list *ap) {
|
||||
@@ -107,11 +166,17 @@ static size_t print_uint32(void (*fn)(char, void *), void *arg, va_list *ap) {
|
||||
size_t mg_print_crash_record(void (*fn)(char, void *), void *arg, va_list *ap) {
|
||||
size_t len = 0;
|
||||
if (mg_health_valid() &&
|
||||
mg_health_record.reset_reason == MG_HEALTH_RESET_FAULT) {
|
||||
(mg_health_record.reset_reason == (uint16_t) MG_HEALTH_RESET_FAULT
|
||||
#if MG_ARCH == MG_ARCH_ESP32
|
||||
|| mg_health_record.reset_reason == (uint16_t) MG_HEALTH_RESET_WATCHDOG
|
||||
#endif
|
||||
)) {
|
||||
size_t n = sizeof(mg_health_record.data) / sizeof(mg_health_record.data[0]);
|
||||
len += mg_xprintf(fn, arg, "{%m:%hhu,%m:%u,%m:[%M]}", //
|
||||
MG_ESC("version"), mg_health_record.magic[3], //
|
||||
MG_ESC("cpuid"), mg_health_record.cpuid, //
|
||||
len += mg_xprintf(fn, arg, "{%m:%hhu,%m:%hu,%m:%hu,%m:%u,%m:[%M]}", //
|
||||
MG_ESC("version"), mg_health_record.magic[3], //
|
||||
MG_ESC("arch"), mg_health_record.arch, //
|
||||
MG_ESC("reason"), mg_health_record.reset_reason, //
|
||||
MG_ESC("cpuid"), mg_health_record.cpuid, //
|
||||
MG_ESC("data"), print_uint32, n, mg_health_record.data);
|
||||
} else {
|
||||
len += mg_xprintf(fn, arg, "{}");
|
||||
@@ -119,3 +184,4 @@ size_t mg_print_crash_record(void (*fn)(char, void *), void *arg, va_list *ap) {
|
||||
(void) ap;
|
||||
return len;
|
||||
}
|
||||
#endif
|
||||
+80
-5
@@ -5,6 +5,8 @@
|
||||
#include "arch.h"
|
||||
#include "printf.h"
|
||||
|
||||
#if MG_ENABLE_HEALTH
|
||||
|
||||
#ifndef MG_HEALTH_MAGIC
|
||||
#define MG_HEALTH_MAGIC {'M', 'G', 'H', '3'} // '3' is the layout version
|
||||
#endif
|
||||
@@ -13,13 +15,15 @@
|
||||
#define MG_HEALTH_RAM __attribute__((section(".mg_health")))
|
||||
#endif
|
||||
|
||||
#if !defined(MG_HEALTH_DATA_SIZE) && (defined(STM32H7_DEV_ID) || defined(STM32H5) || defined(STM32C5))
|
||||
#define MG_HEALTH_DATA_SIZE 250
|
||||
#endif
|
||||
|
||||
#ifndef MG_HEALTH_DATA_SIZE
|
||||
#if (defined(STM32H7_DEV_ID) || defined(STM32H5)) || defined(STM32C5)
|
||||
#define MG_HEALTH_DATA_SIZE 250
|
||||
#elif MG_ARCH == MG_ARCH_ESP32
|
||||
#define MG_HEALTH_DATA_SIZE 23
|
||||
#else
|
||||
#define MG_HEALTH_DATA_SIZE 10
|
||||
#endif
|
||||
#endif // MG_HEALTH_DATA_SIZE
|
||||
|
||||
// Places the record in RAM that survives a reset. Define in mongoose_config.h
|
||||
// to match the linker script, e.g. __attribute__((section(".mg_health")))
|
||||
@@ -27,6 +31,13 @@
|
||||
#define MG_HEALTH_RAM
|
||||
#endif
|
||||
|
||||
// CPU core architecture
|
||||
enum mg_health_arch {
|
||||
MG_HEALTH_ARCH_ARM = 0,
|
||||
MG_HEALTH_ARCH_XTENSA,
|
||||
MG_HEALTH_ARCH_RISCV
|
||||
};
|
||||
|
||||
// Why the device restarted. Hardware usually reports several flags at once,
|
||||
// so these are ordered by how much they tell you: the most specific cause a
|
||||
// device reports wins
|
||||
@@ -43,7 +54,8 @@ enum mg_health_reason {
|
||||
|
||||
struct mg_health {
|
||||
char magic[4]; // MG_HEALTH_MAGIC when the record holds valid data
|
||||
uint32_t reset_reason; // enum mg_health_reason that started this boot
|
||||
uint16_t reset_reason; // enum mg_health_reason that started this boot
|
||||
uint16_t arch; // enum mg_health_arch, core architecture (ARM, etc)
|
||||
uint32_t cpuid; // ARM CPU ID
|
||||
uint32_t data[MG_HEALTH_DATA_SIZE];
|
||||
};
|
||||
@@ -76,6 +88,69 @@ static inline const char *mg_health_reason_str(int reason) {
|
||||
}
|
||||
}
|
||||
|
||||
#if MG_ARCH == MG_ARCH_ESP32
|
||||
#ifndef CONFIG_ESP_COREDUMP_ENABLE_TO_FLASH
|
||||
#define CONFIG_ESP_COREDUMP_ENABLE_TO_FLASH 0
|
||||
#endif
|
||||
#ifndef ESP_PARTITION_SUBTYPE_DATA_COREDUMP
|
||||
#define ESP_PARTITION_SUBTYPE_DATA_COREDUMP 0x03
|
||||
#endif
|
||||
// clang-format off
|
||||
static inline uint16_t esp2mg(esp_reset_reason_t rr) {
|
||||
switch(rr) {
|
||||
case ESP_RST_POWERON: return (uint16_t) MG_HEALTH_RESET_POWER;
|
||||
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 2, 2)
|
||||
case ESP_RST_PWR_GLITCH:
|
||||
#endif
|
||||
case ESP_RST_BROWNOUT: return (uint16_t) MG_HEALTH_RESET_BROWNOUT;
|
||||
case ESP_RST_EXT: return (uint16_t) MG_HEALTH_RESET_PIN;
|
||||
case ESP_RST_SW: return (uint16_t) MG_HEALTH_RESET_SOFTWARE;
|
||||
case ESP_RST_DEEPSLEEP: return (uint16_t) MG_HEALTH_RESET_LOWPOWER;
|
||||
case ESP_RST_INT_WDT:
|
||||
case ESP_RST_TASK_WDT:
|
||||
case ESP_RST_WDT: return (uint16_t) MG_HEALTH_RESET_WATCHDOG;
|
||||
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 2, 2)
|
||||
case ESP_RST_CPU_LOCKUP:
|
||||
#endif
|
||||
case ESP_RST_PANIC: return (uint16_t) MG_HEALTH_RESET_FAULT;
|
||||
default: break;
|
||||
}
|
||||
return (uint16_t) MG_HEALTH_RESET_UNKNOWN;
|
||||
}
|
||||
// clang-format on
|
||||
#endif
|
||||
|
||||
#define MG_HEALTH_INIT() mg_health_init()
|
||||
extern size_t mg_print_crash_record(mg_pfn_t, void *, va_list *);
|
||||
extern void mg_health_init(void);
|
||||
extern bool mg_health_coredump_begin(size_t *size);
|
||||
extern bool mg_health_coredump_read(size_t offset, void *buf, size_t *len);
|
||||
extern bool mg_health_coredump_end(void);
|
||||
#else
|
||||
#define MG_HEALTH_INIT()
|
||||
static inline int mg_health_reason(void) {
|
||||
return 0;
|
||||
}
|
||||
static inline const char *mg_health_reason_str(int reason) {
|
||||
(void) reason;
|
||||
return "unknown";
|
||||
}
|
||||
static inline size_t mg_print_crash_record(mg_pfn_t fn, void *arg,
|
||||
va_list *ap) {
|
||||
(void) ap;
|
||||
return mg_xprintf(fn, arg, "{}");
|
||||
}
|
||||
static inline bool mg_health_coredump_begin(size_t *size) {
|
||||
(void) size;
|
||||
return false;
|
||||
}
|
||||
static inline bool mg_health_coredump_read(size_t offset, void *buf,
|
||||
size_t *len) {
|
||||
(void) offset, (void) buf, (void) len;
|
||||
return false;
|
||||
}
|
||||
static inline bool mg_health_coredump_end(void) {
|
||||
return false;
|
||||
}
|
||||
|
||||
#endif
|
||||
+33
@@ -89,6 +89,7 @@ static void mg_mdash_fn(struct mg_connection *c, int ev, void *ev_data) {
|
||||
}
|
||||
mg_iobuf_free(&io);
|
||||
} else if (ev == MG_EV_CLOSE) {
|
||||
mg_health_coredump_end();
|
||||
mg_mdash_conn = NULL;
|
||||
MG_INFO(("disconnected, ram %lu", 0));
|
||||
}
|
||||
@@ -146,6 +147,35 @@ static void mg_mdash_rpc_ota_end(struct mg_rpc_req *r) {
|
||||
// MG_ESC("percent"));
|
||||
}
|
||||
|
||||
static void rpc_coredump_begin(struct mg_rpc_req *r) {
|
||||
size_t size = 0;
|
||||
if (mg_health_coredump_begin(&size)) {
|
||||
mg_rpc_ok(r, "{%m:%llu}", MG_ESC("size"), (uint64_t) size);
|
||||
} else {
|
||||
mg_rpc_err(r, 500, "%m", MG_ESC("failed"));
|
||||
}
|
||||
}
|
||||
|
||||
static void rpc_coredump_read(struct mg_rpc_req *r) {
|
||||
uint8_t buf[768];
|
||||
size_t len = sizeof(buf);
|
||||
size_t offset = (size_t) mg_json_get_long(r->frame, "$.params.offset", -1);
|
||||
if (mg_health_coredump_read(offset, buf, &len)) {
|
||||
mg_rpc_ok(r, "{%m:\"%M\"}", MG_ESC("data"), mg_print_base64,
|
||||
(int) len, buf);
|
||||
} else {
|
||||
mg_rpc_err(r, 500, "%m", MG_ESC("failed"));
|
||||
}
|
||||
}
|
||||
|
||||
static void rpc_coredump_end(struct mg_rpc_req *r) {
|
||||
if (mg_health_coredump_end()) {
|
||||
mg_rpc_ok(r, "true");
|
||||
} else {
|
||||
mg_rpc_err(r, 500, "%m", MG_ESC("failed"));
|
||||
}
|
||||
}
|
||||
|
||||
void mg_mdash_init(struct mg_mgr *mgr) {
|
||||
MG_INFO(("Initialising mdash agent, key [%s]", MG_MDASH_KEY));
|
||||
mg_rpc_add(&mgr->rpcs, mg_str("RPC.List"), mg_rpc_list, NULL);
|
||||
@@ -153,6 +183,9 @@ void mg_mdash_init(struct mg_mgr *mgr) {
|
||||
mg_rpc_add(&mgr->rpcs, mg_str("OTA.Begin"), mg_mdash_rpc_ota_begin, NULL);
|
||||
mg_rpc_add(&mgr->rpcs, mg_str("OTA.Write"), mg_mdash_rpc_ota_write, NULL);
|
||||
mg_rpc_add(&mgr->rpcs, mg_str("OTA.End"), mg_mdash_rpc_ota_end, NULL);
|
||||
mg_rpc_add(&mgr->rpcs, mg_str("CoreDump.Begin"), rpc_coredump_begin, NULL);
|
||||
mg_rpc_add(&mgr->rpcs, mg_str("CoreDump.Read"), rpc_coredump_read, NULL);
|
||||
mg_rpc_add(&mgr->rpcs, mg_str("CoreDump.End"), rpc_coredump_end, NULL);
|
||||
}
|
||||
|
||||
void mg_mdash_poll(struct mg_mgr *mgr) {
|
||||
|
||||
@@ -65,8 +65,10 @@ static void http_ev_handler(struct mg_connection *c, int ev, void *ev_data) {
|
||||
}
|
||||
}
|
||||
|
||||
extern void wifi_init(const char *ssid, const char *pass);
|
||||
|
||||
void app_main(void) {
|
||||
void wifi_init(const char *ssid, const char *pass);
|
||||
MG_HEALTH_INIT();
|
||||
wifi_init(WIFI_SSID, WIFI_PASS);
|
||||
|
||||
struct mg_mgr mgr;
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
#define MG_ARCH MG_ARCH_ESP32
|
||||
|
||||
#define MG_ENABLE_HEALTH 1
|
||||
|
||||
#define MG_TLS MG_TLS_BUILTIN
|
||||
//#define MG_TLS MG_TLS_MBED
|
||||
Reference in new issue
Block a user