From 2b6423edccfb9edd696a573af9698547a66b2dd1 Mon Sep 17 00:00:00 2001 From: david gauchard Date: Mon, 24 Aug 2020 22:15:33 +0200 Subject: [PATCH] new w/ OOM raises an exception, shows the caller address for decoders --- cores/esp8266/abi.cpp | 14 ++++++++++++-- cores/esp8266/core_esp8266_postmortem.cpp | 4 ++++ cores/esp8266/debug.h | 1 + 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/cores/esp8266/abi.cpp b/cores/esp8266/abi.cpp index fe6335410..feacc5eb8 100644 --- a/cores/esp8266/abi.cpp +++ b/cores/esp8266/abi.cpp @@ -32,13 +32,19 @@ extern "C" void __cxa_pure_virtual(void) __attribute__ ((__noreturn__)); extern "C" void __cxa_deleted_virtual(void) __attribute__ ((__noreturn__)); -#if !defined(__cpp_exceptions) && !defined(NEW_OOM_ABORT) +#if !defined(__cpp_exceptions) + +// overwrite weak operators new/new[] definitions + void *operator new(size_t size) { void *ret = malloc(size); if (0 != size && 0 == ret) { umm_last_fail_alloc_addr = __builtin_return_address(0); umm_last_fail_alloc_size = size; +#if defined(NEW_OOM_ABORT) + __unhandled_exception(PSTR("OOM")); +#endif } return ret; } @@ -49,10 +55,14 @@ void *operator new[](size_t size) if (0 != size && 0 == ret) { umm_last_fail_alloc_addr = __builtin_return_address(0); umm_last_fail_alloc_size = size; +#if defined(NEW_OOM_ABORT) + __unhandled_exception(PSTR("OOM")); +#endif } return ret; } -#endif // arduino's std::new legacy + +#endif // !defined(__cpp_exceptions) void __cxa_pure_virtual(void) { diff --git a/cores/esp8266/core_esp8266_postmortem.cpp b/cores/esp8266/core_esp8266_postmortem.cpp index f79ebace8..8e10a0edd 100644 --- a/cores/esp8266/core_esp8266_postmortem.cpp +++ b/cores/esp8266/core_esp8266_postmortem.cpp @@ -220,6 +220,10 @@ void __wrap_system_restart_local() { cut_here(); + // now outside from the "cut-here" zone, print correctly the malloc address, + // idf-monitor.py will be able to decode this one and show exact location in sources + ets_printf_P(PSTR("\nlast failed alloc call: 0x%08x\n"), (uint32_t)umm_last_fail_alloc_addr); + custom_crash_callback( &rst_info, sp_dump + offset, stack_end ); ets_delay_us(10000); diff --git a/cores/esp8266/debug.h b/cores/esp8266/debug.h index fab128253..c48b79a14 100644 --- a/cores/esp8266/debug.h +++ b/cores/esp8266/debug.h @@ -22,6 +22,7 @@ void hexdump(const void *mem, uint32_t len, uint8_t cols); extern "C" { #endif +void __unhandled_exception(const char *str) __attribute__((noreturn)); void __panic_func(const char* file, int line, const char* func) __attribute__((noreturn)); #define panic() __panic_func(PSTR(__FILE__), __LINE__, __func__)