From af9b1131a3685d0aad61a7bfbe0ce54d2d895bcf Mon Sep 17 00:00:00 2001 From: Renz Christian Bagaporo Date: Tue, 4 Feb 2020 22:16:37 +0800 Subject: [PATCH] esp_system: share abort panic with port layer --- components/esp_system/component.mk | 2 +- components/esp_system/panic.c | 18 +++++++++--------- .../port/include/port/panic_funcs.h | 2 ++ components/esp_system/port/panic_handler.c | 18 ++++++++++++------ .../private_include/panic_internal.h | 4 ++++ components/newlib/abort.c | 19 ++++++++++--------- 6 files changed, 38 insertions(+), 25 deletions(-) diff --git a/components/esp_system/component.mk b/components/esp_system/component.mk index e7f5eb293..7979a2664 100644 --- a/components/esp_system/component.mk +++ b/components/esp_system/component.mk @@ -2,7 +2,7 @@ SOC_NAME := $(IDF_TARGET) COMPONENT_SRCDIRS := . COMPONENT_ADD_INCLUDEDIRS := include -COMPONENT_PRIV_INCLUDEDIRS := private_include +COMPONENT_PRIV_INCLUDEDIRS := private_include port/include COMPONENT_ADD_LDFRAGMENTS += linker.lf -include $(COMPONENT_PATH)/port/$(SOC_NAME)/component.mk \ No newline at end of file diff --git a/components/esp_system/panic.c b/components/esp_system/panic.c index 87d579786..7418a4b44 100644 --- a/components/esp_system/panic.c +++ b/components/esp_system/panic.c @@ -41,6 +41,7 @@ #endif #include "panic_internal.h" +#include "port/panic_funcs.h" #include "sdkconfig.h" @@ -50,8 +51,8 @@ #define APPTRACE_ONPANIC_HOST_FLUSH_TMO (1000*CONFIG_APPTRACE_ONPANIC_HOST_FLUSH_TMO) #endif -static bool s_abort = false; -static char *s_abort_details = NULL; +bool g_panic_abort = false; +static char *s_panic_abort_details = NULL; #if !CONFIG_ESP_SYSTEM_PANIC_SILENT_REBOOT @@ -144,7 +145,7 @@ static inline void disable_all_wdts(void) static void print_abort_details(const void *f) { - panic_print_str(s_abort_details); + panic_print_str(s_panic_abort_details); } // Control arrives from chip-specific panic handler, environment prepared for @@ -153,10 +154,9 @@ static void print_abort_details(const void *f) void esp_panic_handler(panic_info_t *info) { // If the exception was due to an abort, override some of the panic info - if (s_abort) { + if (g_panic_abort) { info->description = NULL; - info->details = s_abort_details ? print_abort_details : NULL; - info->state = NULL; // do not display state, since it is not a 'real' crash + info->details = s_panic_abort_details ? print_abort_details : NULL; info->reason = "SoftwareAbort"; info->exception = PANIC_EXCEPTION_ABORT; } @@ -304,7 +304,7 @@ void esp_panic_handler(panic_info_t *info) } panic_print_str("Rebooting...\r\n"); - esp_restart_noos(); + panic_restart(); #else disable_all_wdts(); panic_print_str("CPU halted.\r\n"); @@ -315,8 +315,8 @@ void esp_panic_handler(panic_info_t *info) void __attribute__((noreturn)) panic_abort(const char *details) { - s_abort = true; - s_abort_details = (char*) details; + g_panic_abort = true; + s_panic_abort_details = (char*) details; #if CONFIG_APPTRACE_ENABLE #if CONFIG_SYSVIEW_ENABLE diff --git a/components/esp_system/port/include/port/panic_funcs.h b/components/esp_system/port/include/port/panic_funcs.h index 28ab5ba24..6eafc70c4 100644 --- a/components/esp_system/port/include/port/panic_funcs.h +++ b/components/esp_system/port/include/port/panic_funcs.h @@ -12,4 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. +#pragma once + void __attribute__((noreturn)) panic_restart(void); \ No newline at end of file diff --git a/components/esp_system/port/panic_handler.c b/components/esp_system/port/panic_handler.c index b0423c368..f1f410609 100644 --- a/components/esp_system/port/panic_handler.c +++ b/components/esp_system/port/panic_handler.c @@ -233,6 +233,15 @@ static void print_registers(const void *f, int core) } } +static void print_state_for_core(const void *f, int core) +{ + if (!g_panic_abort) { + print_registers(f, core); + panic_print_str("\r\n"); + } + print_backtrace(f, core); +} + static void print_state(const void* f) { #if !CONFIG_FREERTOS_UNICORE @@ -241,9 +250,8 @@ static void print_state(const void* f) int err_core = 0; #endif - print_registers(f, err_core); - panic_print_str("\r\n"); - print_backtrace(f, err_core); + print_state_for_core(f, err_core); + panic_print_str("\r\n"); #if !CONFIG_FREERTOS_UNICORE @@ -251,10 +259,8 @@ static void print_state(const void* f) for (int i = 0; i < SOC_CPU_CORES_NUM; i++) { // `f` is the frame for the offending core, see note above. if (err_core != i && xt_exc_frames[i] != NULL) { + print_state_for_core(xt_exc_frames[i], i); panic_print_str("\r\n"); - print_registers(xt_exc_frames[i], i); - panic_print_str("\r\n"); - print_backtrace(xt_exc_frames[i], i); } } #endif diff --git a/components/esp_system/private_include/panic_internal.h b/components/esp_system/private_include/panic_internal.h index 5656ac487..fb9accfff 100644 --- a/components/esp_system/private_include/panic_internal.h +++ b/components/esp_system/private_include/panic_internal.h @@ -16,8 +16,12 @@ #include +#include "port/panic_funcs.h" + #include "sdkconfig.h" +extern bool g_panic_abort; + // Function to print longer amounts of information such as the details // and backtrace field of panic_info_t. These functions should limit themselves // to printing to the console and should do other more involved processing, diff --git a/components/newlib/abort.c b/components/newlib/abort.c index 4137d74fc..7f9f7075d 100644 --- a/components/newlib/abort.c +++ b/components/newlib/abort.c @@ -21,25 +21,26 @@ void __attribute__((noreturn)) abort(void) { - #define ERR_STR1 "abort() was called at PC 0x" - #define ERR_STR2 " on core " - + #define ERR_STR1 "abort() was called at PC 0x" + #define ERR_STR2 " on core " + _Static_assert(UINTPTR_MAX == 0xffffffff, "abort() assumes 32-bit addresses"); _Static_assert(SOC_CPU_CORES_NUM < 10, "abort() assumes number of cores is 1 to 9"); - + char addr_buf[9] = { 0 }; char core_buf[2] = { 0 }; - + char buf[sizeof(ERR_STR1) + sizeof(addr_buf) + sizeof(core_buf) + sizeof(ERR_STR2) + 1 /* null char */] = { 0 }; - + itoa((uint32_t)(__builtin_return_address(0) - 3), addr_buf, 16); itoa(cpu_ll_get_core_id(), core_buf, 10); - + const char *str[] = { ERR_STR1, addr_buf, ERR_STR2, core_buf }; + + char *dest = buf; - char *dest = buf; for (int i = 0; i < sizeof(str) / sizeof(str[0]); i++) { - strcat(dest, str[i]); + strcat(dest, str[i]); } esp_system_abort(buf);