From 500b9c279533f532c361ecce3158563e0692d27f Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Fri, 9 Mar 2018 11:42:19 +0800 Subject: [PATCH 1/2] gdbstub, postmortem: clean up - Move GDB stub hooks into a separate file, provide header for it - Use syscall instruction raise user mode exception - Remove unused code in postmortem.c fixup fixup --- cores/esp8266/core_esp8266_main.cpp | 7 +-- cores/esp8266/core_esp8266_postmortem.c | 59 ++++++++++-------------- cores/esp8266/gdb_hooks.c | 36 +++++++++++++++ cores/esp8266/gdb_hooks.h | 57 +++++++++++++++++++++++ libraries/GDBStub/src/GDBStub.h | 2 - libraries/GDBStub/src/internal/gdbstub.c | 10 +++- 6 files changed, 126 insertions(+), 45 deletions(-) create mode 100644 cores/esp8266/gdb_hooks.c create mode 100644 cores/esp8266/gdb_hooks.h diff --git a/cores/esp8266/core_esp8266_main.cpp b/cores/esp8266/core_esp8266_main.cpp index 4a96eeec51..cb7401366e 100644 --- a/cores/esp8266/core_esp8266_main.cpp +++ b/cores/esp8266/core_esp8266_main.cpp @@ -33,6 +33,7 @@ extern "C" { #include "cont.h" } #include +#include "gdb_hooks.h" #define LOOP_TASK_PRIORITY 1 #define LOOP_QUEUE_SIZE 1 @@ -137,12 +138,6 @@ static void do_global_ctors(void) { (*--p)(); } -extern "C" void __gdb_init() {} -extern "C" void gdb_init(void) __attribute__ ((weak, alias("__gdb_init"))); - -extern "C" void __gdb_do_break(){} -extern "C" void gdb_do_break(void) __attribute__ ((weak, alias("__gdb_do_break"))); - void init_done() { system_set_os_print(1); gdb_init(); diff --git a/cores/esp8266/core_esp8266_postmortem.c b/cores/esp8266/core_esp8266_postmortem.c index bfb0c06ac3..0a3876537d 100644 --- a/cores/esp8266/core_esp8266_postmortem.c +++ b/cores/esp8266/core_esp8266_postmortem.c @@ -28,9 +28,9 @@ #include "esp8266_peri.h" #include "cont.h" #include "pgmspace.h" +#include "gdb_hooks.h" extern void __real_system_restart_local(); -extern void gdb_do_break(); extern cont_t g_cont; @@ -38,16 +38,14 @@ 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); +void abort() __attribute__((noreturn)); +static void uart_write_char_d(char c); 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); -//static void print_pcs(uint32_t start, uint32_t end); - -bool __attribute((weak)) crash_for_gdb = 0; +static void raise_exception() __attribute__((noreturn)); extern void __custom_crash_callback( struct rst_info * rst_info, uint32_t stack, uint32_t stack_end ) { (void) rst_info; @@ -66,9 +64,17 @@ static void ets_puts_P(const char *romString) { } void __wrap_system_restart_local() { - if (crash_for_gdb) *((int*)0) = 0; register uint32_t sp asm("a1"); + 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(); + } + struct rst_info rst_info = {0}; system_rtc_mem_read(0, &rst_info, sizeof(rst_info)); if (rst_info.reason != REASON_SOFT_WDT_RST && @@ -129,7 +135,6 @@ void __wrap_system_restart_local() { ets_printf("sp: %08x end: %08x offset: %04x\n", sp, stack_end, offset); - // print_pcs(sp + offset, stack_end); print_stack(sp + offset, stack_end); custom_crash_callback( &rst_info, sp + offset, stack_end ); @@ -153,24 +158,7 @@ static void print_stack(uint32_t start, uint32_t end) { ets_puts_P(PSTR("<<>>pc>>>\n"); - for (uint32_t pos = start; pos < end; pos += 16, ++n) { - uint32_t* sf = (uint32_t*) pos; - - uint32_t pc_ret = sf[3]; - uint32_t sp_ret = sf[2]; - if (pc_ret < 0x40000000 || pc_ret > 0x40f00000 || sp_ret != pos + 16) - continue; - ets_printf("%08x\n", pc_ret); - } - ets_printf("<< Date: Fri, 9 Mar 2018 11:42:48 +0800 Subject: [PATCH 2/2] postmortem: raise exception when assert happens Fixes https://github.com/esp8266/Arduino/issues/4480 --- cores/esp8266/core_esp8266_postmortem.c | 1 + 1 file changed, 1 insertion(+) diff --git a/cores/esp8266/core_esp8266_postmortem.c b/cores/esp8266/core_esp8266_postmortem.c index 0a3876537d..9712429795 100644 --- a/cores/esp8266/core_esp8266_postmortem.c +++ b/cores/esp8266/core_esp8266_postmortem.c @@ -197,6 +197,7 @@ void __assert_func(const char *file, int line, const char *func, const char *wha s_panic_line = line; s_panic_func = func; gdb_do_break(); /* if GDB is not present, this is a no-op */ + raise_exception(); } void __panic_func(const char* file, int line, const char* func) {