From 81de98ef836bcd239af5d9a6033e75e08dec16c5 Mon Sep 17 00:00:00 2001 From: David Gauchard Date: Thu, 11 Jul 2019 18:53:14 +0200 Subject: [PATCH 01/10] workaround when an exceptin occurs while in an ISR --- cores/esp8266/core_esp8266_postmortem.cpp | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/cores/esp8266/core_esp8266_postmortem.cpp b/cores/esp8266/core_esp8266_postmortem.cpp index f18dcfe53b..42969e4038 100644 --- a/cores/esp8266/core_esp8266_postmortem.cpp +++ b/cores/esp8266/core_esp8266_postmortem.cpp @@ -82,6 +82,7 @@ static void ets_printf_P(const char *str, ...) { } } +static int workaround = 0; void __wrap_system_restart_local() { register uint32_t sp asm("a1"); uint32_t sp_dump = sp; @@ -97,13 +98,19 @@ void __wrap_system_restart_local() { struct rst_info rst_info; memset(&rst_info, 0, sizeof(rst_info)); - system_rtc_mem_read(0, &rst_info, sizeof(rst_info)); - if (rst_info.reason != REASON_SOFT_WDT_RST && - rst_info.reason != REASON_EXCEPTION_RST && - rst_info.reason != REASON_WDT_RST) + if (workaround == 0) { - return; + system_rtc_mem_read(0, &rst_info, sizeof(rst_info)); + if (rst_info.reason != REASON_SOFT_WDT_RST && + rst_info.reason != REASON_EXCEPTION_RST && + rst_info.reason != REASON_WDT_RST) + { +ets_printf("beeeh %d\n", rst_info.reason); + return; + } } + else + rst_info.reason = workaround; // TODO: ets_install_putc1 definition is wrong in ets_sys.h, need cast ets_install_putc1((void *)&uart_write_char_d); From b061868fd1cc368b916067e4620e3109f11f21b1 Mon Sep 17 00:00:00 2001 From: David Gauchard Date: Thu, 11 Jul 2019 18:56:32 +0200 Subject: [PATCH 02/10] ditto --- cores/esp8266/core_esp8266_postmortem.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/cores/esp8266/core_esp8266_postmortem.cpp b/cores/esp8266/core_esp8266_postmortem.cpp index 42969e4038..2dd70383d2 100644 --- a/cores/esp8266/core_esp8266_postmortem.cpp +++ b/cores/esp8266/core_esp8266_postmortem.cpp @@ -230,6 +230,10 @@ static void uart1_write_char_d(char c) { static void raise_exception() { __asm__ __volatile__ ("syscall"); + ets_printf_P(PSTR("exception from IRQ\n")); +workaround=7; // last REASON_EXT_SYS_RST = 6 + __wrap_system_restart_local(); + ets_printf_P(PSTR("beh\n")); while (1); // never reached, needed to satisfy "noreturn" attribute } From ad60f5b57d721b279fbedadc1565cf0eecb4e3d4 Mon Sep 17 00:00:00 2001 From: David Gauchard Date: Thu, 11 Jul 2019 19:17:13 +0200 Subject: [PATCH 03/10] wip --- cores/esp8266/core_esp8266_postmortem.cpp | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/cores/esp8266/core_esp8266_postmortem.cpp b/cores/esp8266/core_esp8266_postmortem.cpp index 2dd70383d2..1bc806a239 100644 --- a/cores/esp8266/core_esp8266_postmortem.cpp +++ b/cores/esp8266/core_esp8266_postmortem.cpp @@ -82,7 +82,10 @@ static void ets_printf_P(const char *str, ...) { } } -static int workaround = 0; +#define FAKE_REASON_NONE 255 +#define FAKE_REASON_USER 254 +static int fake_rst_reason = FAKE_REASON_NONE; + void __wrap_system_restart_local() { register uint32_t sp asm("a1"); uint32_t sp_dump = sp; @@ -98,19 +101,18 @@ void __wrap_system_restart_local() { struct rst_info rst_info; memset(&rst_info, 0, sizeof(rst_info)); - if (workaround == 0) + if (fake_rst_reason == FAKE_REASON_NONE) { system_rtc_mem_read(0, &rst_info, sizeof(rst_info)); if (rst_info.reason != REASON_SOFT_WDT_RST && rst_info.reason != REASON_EXCEPTION_RST && rst_info.reason != REASON_WDT_RST) { -ets_printf("beeeh %d\n", rst_info.reason); return; } } else - rst_info.reason = workaround; + rst_info.reason = fake_rst_reason; // TODO: ets_install_putc1 definition is wrong in ets_sys.h, need cast ets_install_putc1((void *)&uart_write_char_d); @@ -229,11 +231,10 @@ static void uart1_write_char_d(char c) { } static void raise_exception() { - __asm__ __volatile__ ("syscall"); - ets_printf_P(PSTR("exception from IRQ\n")); -workaround=7; // last REASON_EXT_SYS_RST = 6 + __asm__ __volatile__ ("syscall"); // no effect? + ets_printf_P("\nsoftware exception"); + fake_rst_reason = FAKE_REASON_USER; __wrap_system_restart_local(); - ets_printf_P(PSTR("beh\n")); while (1); // never reached, needed to satisfy "noreturn" attribute } From cd31c4e4dc9a8d22bebc558603007cc1d61fb879 Mon Sep 17 00:00:00 2001 From: David Gauchard Date: Thu, 11 Jul 2019 19:21:29 +0200 Subject: [PATCH 04/10] wip --- cores/esp8266/core_esp8266_postmortem.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cores/esp8266/core_esp8266_postmortem.cpp b/cores/esp8266/core_esp8266_postmortem.cpp index 1bc806a239..539657ec41 100644 --- a/cores/esp8266/core_esp8266_postmortem.cpp +++ b/cores/esp8266/core_esp8266_postmortem.cpp @@ -83,7 +83,7 @@ static void ets_printf_P(const char *str, ...) { } #define FAKE_REASON_NONE 255 -#define FAKE_REASON_USER 254 +#define FAKE_REASON_USER 254 static int fake_rst_reason = FAKE_REASON_NONE; void __wrap_system_restart_local() { @@ -232,7 +232,7 @@ static void uart1_write_char_d(char c) { static void raise_exception() { __asm__ __volatile__ ("syscall"); // no effect? - ets_printf_P("\nsoftware exception"); + ets_printf_P(PSTR("\nsoftware exception")); fake_rst_reason = FAKE_REASON_USER; __wrap_system_restart_local(); while (1); // never reached, needed to satisfy "noreturn" attribute From e3c477ee5fa9758344520e5ebb12bea24433ccdb Mon Sep 17 00:00:00 2001 From: David Gauchard Date: Thu, 11 Jul 2019 19:34:21 +0200 Subject: [PATCH 05/10] wip --- cores/esp8266/core_esp8266_postmortem.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/cores/esp8266/core_esp8266_postmortem.cpp b/cores/esp8266/core_esp8266_postmortem.cpp index 539657ec41..49777ac698 100644 --- a/cores/esp8266/core_esp8266_postmortem.cpp +++ b/cores/esp8266/core_esp8266_postmortem.cpp @@ -231,10 +231,13 @@ static void uart1_write_char_d(char c) { } static void raise_exception() { + //*((char*)0) = 0; // <- works but with a bad reason __asm__ __volatile__ ("syscall"); // no effect? - ets_printf_P(PSTR("\nsoftware exception")); + fake_rst_reason = FAKE_REASON_USER; + ets_printf_P(PSTR("\nUser exception (panic/abort/assert)")); __wrap_system_restart_local(); + while (1); // never reached, needed to satisfy "noreturn" attribute } From 2a24b25162373ac73b296c5e8ccc3adbbb01729f Mon Sep 17 00:00:00 2001 From: David Gauchard Date: Thu, 11 Jul 2019 19:37:34 +0200 Subject: [PATCH 06/10] wip --- cores/esp8266/core_esp8266_postmortem.cpp | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/cores/esp8266/core_esp8266_postmortem.cpp b/cores/esp8266/core_esp8266_postmortem.cpp index 49777ac698..b95a998113 100644 --- a/cores/esp8266/core_esp8266_postmortem.cpp +++ b/cores/esp8266/core_esp8266_postmortem.cpp @@ -231,13 +231,22 @@ static void uart1_write_char_d(char c) { } static void raise_exception() { - //*((char*)0) = 0; // <- works but with a bad reason +#if 0 + + // works but also showing + // "Fatal exception 29(StoreProhibitedCause)" + *((char*)0) = 0; + +#else + __asm__ __volatile__ ("syscall"); // no effect? fake_rst_reason = FAKE_REASON_USER; ets_printf_P(PSTR("\nUser exception (panic/abort/assert)")); __wrap_system_restart_local(); +#endif + while (1); // never reached, needed to satisfy "noreturn" attribute } From 68a131e7af1a4cef3932016d38e18d276357a4f0 Mon Sep 17 00:00:00 2001 From: David Gauchard Date: Fri, 12 Jul 2019 13:51:20 +0200 Subject: [PATCH 07/10] tuning for gdb --- cores/esp8266/core_esp8266_postmortem.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/cores/esp8266/core_esp8266_postmortem.cpp b/cores/esp8266/core_esp8266_postmortem.cpp index b95a998113..e53f0747d5 100644 --- a/cores/esp8266/core_esp8266_postmortem.cpp +++ b/cores/esp8266/core_esp8266_postmortem.cpp @@ -90,6 +90,7 @@ void __wrap_system_restart_local() { register uint32_t sp asm("a1"); uint32_t sp_dump = sp; +#if 0 if (gdb_present()) { /* When GDBStub is present, exceptions are handled by GDBStub, but Soft WDT will still call this function. @@ -98,6 +99,7 @@ void __wrap_system_restart_local() { break into GDB here. */ raise_exception(); } +#endif struct rst_info rst_info; memset(&rst_info, 0, sizeof(rst_info)); @@ -239,7 +241,9 @@ static void raise_exception() { #else - __asm__ __volatile__ ("syscall"); // no effect? + if (gdb_present()) + //*((char*)0) = 0; + __asm__ __volatile__ ("syscall"); // triggers GDB when enabled fake_rst_reason = FAKE_REASON_USER; ets_printf_P(PSTR("\nUser exception (panic/abort/assert)")); From 73f7fa62908f37541b94e709d0d2837261ee60ac Mon Sep 17 00:00:00 2001 From: david gauchard Date: Sat, 13 Jul 2019 23:30:47 +0200 Subject: [PATCH 08/10] remove dead code and rename defines/variables --- cores/esp8266/core_esp8266_postmortem.cpp | 35 +++++------------------ 1 file changed, 7 insertions(+), 28 deletions(-) diff --git a/cores/esp8266/core_esp8266_postmortem.cpp b/cores/esp8266/core_esp8266_postmortem.cpp index e53f0747d5..0efcbc7402 100644 --- a/cores/esp8266/core_esp8266_postmortem.cpp +++ b/cores/esp8266/core_esp8266_postmortem.cpp @@ -82,28 +82,18 @@ static void ets_printf_P(const char *str, ...) { } } -#define FAKE_REASON_NONE 255 -#define FAKE_REASON_USER 254 -static int fake_rst_reason = FAKE_REASON_NONE; +#define USER_REASON_NONE 255 +#define USER_REASON_SWEXCEPTION 254 +// using numbers different from "REASON_" in user_interface.h 0..6 +static int user_reset_reason = USER_REASON_NONE; void __wrap_system_restart_local() { register uint32_t sp asm("a1"); uint32_t sp_dump = sp; -#if 0 - if (gdb_present()) { - /* When GDBStub is present, exceptions are handled by GDBStub, - but Soft WDT will still call this function. - Trigger an exception to break into GDB. - TODO: check why gdb_do_break() or asm("break.n 0") do not - break into GDB here. */ - raise_exception(); - } -#endif - struct rst_info rst_info; memset(&rst_info, 0, sizeof(rst_info)); - if (fake_rst_reason == FAKE_REASON_NONE) + if (user_reset_reason == USER_REASON_NONE) { system_rtc_mem_read(0, &rst_info, sizeof(rst_info)); if (rst_info.reason != REASON_SOFT_WDT_RST && @@ -114,7 +104,7 @@ void __wrap_system_restart_local() { } } else - rst_info.reason = fake_rst_reason; + rst_info.reason = user_reset_reason; // TODO: ets_install_putc1 definition is wrong in ets_sys.h, need cast ets_install_putc1((void *)&uart_write_char_d); @@ -233,24 +223,13 @@ static void uart1_write_char_d(char c) { } static void raise_exception() { -#if 0 - - // works but also showing - // "Fatal exception 29(StoreProhibitedCause)" - *((char*)0) = 0; - -#else - if (gdb_present()) - //*((char*)0) = 0; __asm__ __volatile__ ("syscall"); // triggers GDB when enabled - fake_rst_reason = FAKE_REASON_USER; + user_reset_reason = USER_REASON_SWEXCEPTION; ets_printf_P(PSTR("\nUser exception (panic/abort/assert)")); __wrap_system_restart_local(); -#endif - while (1); // never reached, needed to satisfy "noreturn" attribute } From 3187bbcce19b64e16b04ddd5a59bee0fcba10d30 Mon Sep 17 00:00:00 2001 From: David Gauchard Date: Mon, 15 Jul 2019 13:59:35 +0200 Subject: [PATCH 09/10] per reviews: naming, handle "unhandled return" case --- cores/esp8266/core_esp8266_postmortem.cpp | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/cores/esp8266/core_esp8266_postmortem.cpp b/cores/esp8266/core_esp8266_postmortem.cpp index 0efcbc7402..85f88d2a8b 100644 --- a/cores/esp8266/core_esp8266_postmortem.cpp +++ b/cores/esp8266/core_esp8266_postmortem.cpp @@ -52,6 +52,13 @@ static void uart0_write_char_d(char c); static void uart1_write_char_d(char c); static void print_stack(uint32_t start, uint32_t end); +// using numbers different from "REASON_" in user_interface.h (=0..6) +enum rst_reason_sw +{ + REASON_USER_SWEXCEPTION_RST = 254 +}; +static int s_user_reset_reason = REASON_DEFAULT_RST; + // From UMM, the last caller of a malloc/realloc/calloc which failed: extern void *umm_last_fail_alloc_addr; extern int umm_last_fail_alloc_size; @@ -82,29 +89,24 @@ static void ets_printf_P(const char *str, ...) { } } -#define USER_REASON_NONE 255 -#define USER_REASON_SWEXCEPTION 254 -// using numbers different from "REASON_" in user_interface.h 0..6 -static int user_reset_reason = USER_REASON_NONE; - void __wrap_system_restart_local() { register uint32_t sp asm("a1"); uint32_t sp_dump = sp; struct rst_info rst_info; memset(&rst_info, 0, sizeof(rst_info)); - if (user_reset_reason == USER_REASON_NONE) + if (s_user_reset_reason == REASON_DEFAULT_RST) { system_rtc_mem_read(0, &rst_info, sizeof(rst_info)); if (rst_info.reason != REASON_SOFT_WDT_RST && rst_info.reason != REASON_EXCEPTION_RST && rst_info.reason != REASON_WDT_RST) { - return; + rst_info.reason = REASON_DEFAULT_RST; } } else - rst_info.reason = user_reset_reason; + rst_info.reason = s_user_reset_reason; // TODO: ets_install_putc1 definition is wrong in ets_sys.h, need cast ets_install_putc1((void *)&uart_write_char_d); @@ -129,6 +131,9 @@ void __wrap_system_restart_local() { else if (rst_info.reason == REASON_SOFT_WDT_RST) { ets_printf_P(PSTR("\nSoft WDT reset\n")); } + else { + ets_printf_P(PSTR("\nGeneric Soft Reset\n")); + } uint32_t cont_stack_start = (uint32_t) &(g_pcont->stack); uint32_t cont_stack_end = (uint32_t) g_pcont->stack_end; @@ -226,7 +231,7 @@ static void raise_exception() { if (gdb_present()) __asm__ __volatile__ ("syscall"); // triggers GDB when enabled - user_reset_reason = USER_REASON_SWEXCEPTION; + s_user_reset_reason = REASON_USER_SWEXCEPTION_RST; ets_printf_P(PSTR("\nUser exception (panic/abort/assert)")); __wrap_system_restart_local(); From d476e2e9266cb151f51595a10db67bb83158338d Mon Sep 17 00:00:00 2001 From: David Gauchard Date: Mon, 15 Jul 2019 14:33:48 +0200 Subject: [PATCH 10/10] fix reset message --- cores/esp8266/core_esp8266_postmortem.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cores/esp8266/core_esp8266_postmortem.cpp b/cores/esp8266/core_esp8266_postmortem.cpp index 85f88d2a8b..bbe67f1d4d 100644 --- a/cores/esp8266/core_esp8266_postmortem.cpp +++ b/cores/esp8266/core_esp8266_postmortem.cpp @@ -132,7 +132,7 @@ void __wrap_system_restart_local() { ets_printf_P(PSTR("\nSoft WDT reset\n")); } else { - ets_printf_P(PSTR("\nGeneric Soft Reset\n")); + ets_printf_P(PSTR("\nGeneric Reset\n")); } uint32_t cont_stack_start = (uint32_t) &(g_pcont->stack);