From a1fd207aee9da2b5044e2974497d82290d476601 Mon Sep 17 00:00:00 2001 From: Darian Leung Date: Fri, 27 Mar 2020 17:58:12 +0800 Subject: [PATCH] Add Task Watchdog backtrace printing This commit makes the Task Watchdog print the backtrace of both cores when it times out. --- components/esp32/crosscore_int.c | 9 +++++++++ components/esp32/include/esp_crosscore_int.h | 10 ++++++++++ components/esp32/task_wdt.c | 15 ++++++++++++++- 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/components/esp32/crosscore_int.c b/components/esp32/crosscore_int.c index 407157e4a1..1130c28de1 100644 --- a/components/esp32/crosscore_int.c +++ b/components/esp32/crosscore_int.c @@ -18,6 +18,7 @@ #include "esp_err.h" #include "esp_intr.h" #include "esp_intr_alloc.h" +#include "esp_panic.h" #include "rom/ets_sys.h" #include "rom/uart.h" @@ -36,6 +37,7 @@ #define REASON_YIELD BIT(0) #define REASON_FREQ_SWITCH BIT(1) +#define REASON_PRINT_BACKTRACE BIT(2) static portMUX_TYPE reason_spinlock = portMUX_INITIALIZER_UNLOCKED; static volatile uint32_t reason[ portNUM_PROCESSORS ]; @@ -76,6 +78,9 @@ static void IRAM_ATTR esp_crosscore_isr(void *arg) { * to allow DFS features without the extra latency of the ISR hook. */ } + if (my_reason_val & REASON_PRINT_BACKTRACE) { + esp_backtrace_print(100); + } } //Initialize the crosscore interrupt on this core. Call this once @@ -117,3 +122,7 @@ void IRAM_ATTR esp_crosscore_int_send_freq_switch(int core_id) esp_crosscore_int_send(core_id, REASON_FREQ_SWITCH); } +void IRAM_ATTR esp_crosscore_int_send_print_backtrace(int core_id) +{ + esp_crosscore_int_send(core_id, REASON_PRINT_BACKTRACE); +} diff --git a/components/esp32/include/esp_crosscore_int.h b/components/esp32/include/esp_crosscore_int.h index 2f1c5b3bec..41ddd7a707 100644 --- a/components/esp32/include/esp_crosscore_int.h +++ b/components/esp32/include/esp_crosscore_int.h @@ -51,4 +51,14 @@ void esp_crosscore_int_send_yield(int core_id); */ void esp_crosscore_int_send_freq_switch(int core_id); +/** + * Send an interrupt to a CPU indicating it should print its current backtrace + * + * This is use internally by the Task Watchdog to dump the backtrace of the + * opposite core and should not be called from application code. + * + * @param core_id Core that should print its backtrace + */ +void esp_crosscore_int_send_print_backtrace(int core_id); + #endif diff --git a/components/esp32/task_wdt.c b/components/esp32/task_wdt.c index 0c302082a4..abb4868f57 100644 --- a/components/esp32/task_wdt.c +++ b/components/esp32/task_wdt.c @@ -28,6 +28,7 @@ #include "esp_intr.h" #include "esp_intr_alloc.h" #include "esp_attr.h" +#include "esp_panic.h" #include "esp_freertos_hooks.h" #include "soc/timer_group_struct.h" #include "soc/timer_group_reg.h" @@ -36,6 +37,7 @@ #include "driver/periph_ctrl.h" #include "esp_task_wdt.h" #include "esp_system_internal.h" +#include "esp_crosscore_int.h" static const char *TAG = "task_wdt"; @@ -167,13 +169,24 @@ static void task_wdt_isr(void *arg) } esp_task_wdt_isr_user_handler(); + if (twdt_config->panic){ //Trigger Panic if configured to do so ESP_EARLY_LOGE(TAG, "Aborting."); portEXIT_CRITICAL_ISR(&twdt_spinlock); esp_reset_reason_set_hint(ESP_RST_TASK_WDT); abort(); + } else { + int current_core = xPortGetCoreID(); + //Print backtrace of current core + ESP_EARLY_LOGE(TAG, "Print CPU %d (current core) backtrace", current_core); + esp_backtrace_print(100); + #if !CONFIG_FREERTOS_UNICORE + //Print backtrace of other core + ESP_EARLY_LOGE(TAG, "Print CPU %d backtrace", !current_core); + esp_crosscore_int_send_print_backtrace(!current_core); + #endif } - + portEXIT_CRITICAL_ISR(&twdt_spinlock); }