From 8bf1e98f24f03a1bc73fe8243bfc0aa01b688e5b Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Sat, 21 Nov 2015 20:04:38 +0300 Subject: [PATCH] Improve debug output on critical errors --- cores/esp8266/abi.cpp | 15 ++++---- cores/esp8266/core_esp8266_main.cpp | 14 ++------ cores/esp8266/core_esp8266_postmortem.c | 36 ++++++++++++++++++- cores/esp8266/debug.h | 16 ++++++++- .../ESP8266WiFi/src/WiFiClientSecure.cpp | 7 ++-- 5 files changed, 64 insertions(+), 24 deletions(-) diff --git a/cores/esp8266/abi.cpp b/cores/esp8266/abi.cpp index 91e999a0c..b38f16453 100644 --- a/cores/esp8266/abi.cpp +++ b/cores/esp8266/abi.cpp @@ -17,14 +17,16 @@ */ #include +#include +#include extern "C" { #include "ets_sys.h" #include "os_type.h" #include "osapi.h" #include "mem.h" -#include "user_interface.h" } + void *operator new(size_t size) { size = ((size + 3) & ~((size_t)0x3)); return os_malloc(size); @@ -47,27 +49,26 @@ extern "C" void __cxa_pure_virtual(void) __attribute__ ((__noreturn__)); extern "C" void __cxa_deleted_virtual(void) __attribute__ ((__noreturn__)); void __cxa_pure_virtual(void) { - abort(); + panic(); } void __cxa_deleted_virtual(void) { - abort(); + panic(); } namespace std { void __throw_bad_function_call() { - abort(); + panic(); } void __throw_length_error(char const*) { - abort(); + panic(); } void __throw_bad_alloc() { - abort(); + panic(); } } // TODO: rebuild windows toolchain to make this unnecessary: void* __dso_handle; - diff --git a/cores/esp8266/core_esp8266_main.cpp b/cores/esp8266/core_esp8266_main.cpp index 5ea7a1be7..92c48312d 100644 --- a/cores/esp8266/core_esp8266_main.cpp +++ b/cores/esp8266/core_esp8266_main.cpp @@ -66,13 +66,6 @@ static os_event_t g_loop_queue[LOOP_QUEUE_SIZE]; static uint32_t g_micros_at_task_start; - -extern "C" void abort() { - do { - *((int*)0) = 0; - } while(true); -} - extern "C" void esp_yield() { if (cont_can_yield(&g_cont)) { cont_yield(&g_cont); @@ -89,7 +82,7 @@ extern "C" void __yield() { esp_yield(); } else { - abort(); + panic(); } } @@ -117,9 +110,8 @@ static void loop_wrapper() { static void loop_task(os_event_t *events) { g_micros_at_task_start = system_get_time(); cont_run(&g_cont, &loop_wrapper); - if(cont_check(&g_cont) != 0) { - ets_printf("\r\nsketch stack overflow detected\r\n"); - abort(); + if (cont_check(&g_cont) != 0) { + panic(); } } diff --git a/cores/esp8266/core_esp8266_postmortem.c b/cores/esp8266/core_esp8266_postmortem.c index 1fd929e85..1af8f57ca 100644 --- a/cores/esp8266/core_esp8266_postmortem.c +++ b/cores/esp8266/core_esp8266_postmortem.c @@ -22,6 +22,7 @@ #include #include #include +#include "debug.h" #include "ets_sys.h" #include "user_interface.h" #include "esp8266_peri.h" @@ -30,6 +31,11 @@ extern void __real_system_restart_local(); extern cont_t g_cont; +static const char* s_panic_file = 0; +static int s_panic_line = 0; +static const char* s_panic_func = 0; + +static bool s_abort_called = false; void uart_write_char_d(char c); static void uart0_write_char_d(char c); @@ -56,7 +62,13 @@ void __wrap_system_restart_local() { ets_install_putc1(&uart_write_char_d); - if (rst_info.reason == REASON_EXCEPTION_RST) { + if (s_panic_line) { + ets_printf("\nPanic %s:%d %s\n", s_panic_file, s_panic_line, s_panic_func); + } + else if (s_abort_called) { + ets_printf("Abort called\n"); + } + else if (rst_info.reason == REASON_EXCEPTION_RST) { ets_printf("\nException (%d):\nepc1=0x%08x epc2=0x%08x epc3=0x%08x excvaddr=0x%08x depc=0x%08x\n", rst_info.exccause, rst_info.epc1, rst_info.epc2, rst_info.epc3, rst_info.excvaddr, rst_info.depc); } @@ -158,3 +170,25 @@ static void uart1_write_char_d(char c) { } USF(1) = c; } +void abort() __attribute__((noreturn)); + +void abort(){ + // cause exception + s_abort_called = true; + do { + *((int*)0) = 0; + } while(true); +} + +void __assert_func(const char *file, int line, const char *func, const char *what) { + s_panic_file = file; + s_panic_line = line; + s_panic_func = func; +} + +void __panic_func(const char* file, int line, const char* func) { + s_panic_file = file; + s_panic_line = line; + s_panic_func = func; + abort(); +} diff --git a/cores/esp8266/debug.h b/cores/esp8266/debug.h index fe389bb2d..9cf5980ef 100644 --- a/cores/esp8266/debug.h +++ b/cores/esp8266/debug.h @@ -2,7 +2,9 @@ #define ARD_DEBUG_H #include -//#define DEBUGV(...) ets_printf(__VA_ARGS__) +#include + +#define DEBUGV(...) ets_printf(__VA_ARGS__) #ifndef DEBUGV #define DEBUGV(...) @@ -14,4 +16,16 @@ void hexdump(uint8_t *mem, uint32_t len, uint8_t cols = 16); void hexdump(uint8_t *mem, uint32_t len, uint8_t cols); #endif +#ifdef __cplusplus +extern "C" { +#endif + +void __panic_func(const char* file, int line, const char* func) __attribute__((noreturn)); +#define panic() __panic_func(__FILE__, __LINE__, __func__) + +#ifdef __cplusplus +} +#endif + + #endif//ARD_DEBUG_H diff --git a/libraries/ESP8266WiFi/src/WiFiClientSecure.cpp b/libraries/ESP8266WiFi/src/WiFiClientSecure.cpp index 3369c3448..ef737c6d4 100644 --- a/libraries/ESP8266WiFi/src/WiFiClientSecure.cpp +++ b/libraries/ESP8266WiFi/src/WiFiClientSecure.cpp @@ -273,7 +273,7 @@ int WiFiClientSecure::available() { uint8_t WiFiClientSecure::connected() { if (!_client) return 0; - + if (_client->state() == ESTABLISHED) return 1; @@ -384,8 +384,7 @@ extern "C" void* ax_port_malloc(size_t size, const char* file, int line) { if (result == nullptr) { DEBUG_TLS_MEM_PRINT("%s:%d malloc %d failed, left %d\r\n", file, line, size, ESP.getFreeHeap()); - - while(true){} + panic(); } if (size >= 1024) DEBUG_TLS_MEM_PRINT("%s:%d malloc %d, left %d\r\n", file, line, size, ESP.getFreeHeap()); @@ -402,7 +401,7 @@ extern "C" void* ax_port_realloc(void* ptr, size_t size, const char* file, int l void* result = realloc(ptr, size); if (result == nullptr) { DEBUG_TLS_MEM_PRINT("%s:%d realloc %d failed, left %d\r\n", file, line, size, ESP.getFreeHeap()); - while(true){} + panic(); } if (size >= 1024) DEBUG_TLS_MEM_PRINT("%s:%d realloc %d, left %d\r\n", file, line, size, ESP.getFreeHeap());