| 
									
										
										
										
											2021-11-06 17:24:45 +08:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * SPDX-License-Identifier: Apache-2.0 | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2020-02-02 23:23:16 +08:00
										 |  |  | #include <stdlib.h>
 | 
					
						
							| 
									
										
										
										
											2020-10-02 11:17:38 +08:00
										 |  |  | #include <string.h>
 | 
					
						
							| 
									
										
										
										
											2020-02-02 23:23:16 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include "esp_err.h"
 | 
					
						
							|  |  |  | #include "esp_attr.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "esp_private/system_internal.h"
 | 
					
						
							| 
									
										
										
										
											2020-04-30 16:48:49 +02:00
										 |  |  | #include "esp_private/usb_console.h"
 | 
					
						
							| 
									
										
										
										
											2020-02-02 23:23:16 +08:00
										 |  |  | #include "esp_ota_ops.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "soc/cpu.h"
 | 
					
						
							| 
									
										
										
										
											2019-12-26 16:30:03 +08:00
										 |  |  | #include "soc/rtc.h"
 | 
					
						
							| 
									
										
										
										
											2020-02-02 23:23:16 +08:00
										 |  |  | #include "hal/timer_hal.h"
 | 
					
						
							|  |  |  | #include "hal/cpu_hal.h"
 | 
					
						
							| 
									
										
										
										
											2019-12-26 16:30:03 +08:00
										 |  |  | #include "hal/wdt_types.h"
 | 
					
						
							|  |  |  | #include "hal/wdt_hal.h"
 | 
					
						
							| 
									
										
										
										
											2020-02-02 23:23:16 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-08 19:32:35 +03:00
										 |  |  | #include "esp_private/panic_internal.h"
 | 
					
						
							| 
									
										
										
										
											2020-02-04 22:16:37 +08:00
										 |  |  | #include "port/panic_funcs.h"
 | 
					
						
							| 
									
										
										
										
											2021-04-19 12:03:43 +03:00
										 |  |  | #include "esp_rom_sys.h"
 | 
					
						
							| 
									
										
										
										
											2020-02-02 23:23:16 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include "sdkconfig.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-26 13:03:14 +08:00
										 |  |  | #if CONFIG_ESP_COREDUMP_ENABLE
 | 
					
						
							| 
									
										
										
										
											2020-10-08 13:18:16 +08:00
										 |  |  | #include "esp_core_dump.h"
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #if CONFIG_APPTRACE_ENABLE
 | 
					
						
							|  |  |  | #include "esp_app_trace.h"
 | 
					
						
							| 
									
										
										
										
											2020-12-21 20:17:42 +03:00
										 |  |  | #if CONFIG_APPTRACE_SV_ENABLE
 | 
					
						
							| 
									
										
										
										
											2020-10-08 13:18:16 +08:00
										 |  |  | #include "SEGGER_RTT.h"
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 23:23:16 +08:00
										 |  |  | #if CONFIG_APPTRACE_ONPANIC_HOST_FLUSH_TMO == -1
 | 
					
						
							|  |  |  | #define APPTRACE_ONPANIC_HOST_FLUSH_TMO   ESP_APPTRACE_TMO_INFINITE
 | 
					
						
							|  |  |  | #else
 | 
					
						
							|  |  |  | #define APPTRACE_ONPANIC_HOST_FLUSH_TMO   (1000*CONFIG_APPTRACE_ONPANIC_HOST_FLUSH_TMO)
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2020-10-08 13:18:16 +08:00
										 |  |  | #endif // CONFIG_APPTRACE_ENABLE
 | 
					
						
							| 
									
										
										
										
											2020-02-02 23:23:16 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-02 11:17:38 +08:00
										 |  |  | #if !CONFIG_ESP_SYSTEM_PANIC_SILENT_REBOOT
 | 
					
						
							|  |  |  | #include "hal/uart_hal.h"
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-06 15:00:07 +11:00
										 |  |  | #if CONFIG_ESP_SYSTEM_PANIC_GDBSTUB
 | 
					
						
							| 
									
										
										
										
											2020-10-02 11:17:38 +08:00
										 |  |  | #include "esp_gdbstub.h"
 | 
					
						
							| 
									
										
										
										
											2020-11-06 15:00:07 +11:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-19 12:03:43 +03:00
										 |  |  | #if CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG
 | 
					
						
							|  |  |  | #include "hal/usb_serial_jtag_ll.h"
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-04 22:16:37 +08:00
										 |  |  | bool g_panic_abort = false; | 
					
						
							|  |  |  | static char *s_panic_abort_details = NULL; | 
					
						
							| 
									
										
										
										
											2020-02-02 23:23:16 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-26 16:30:03 +08:00
										 |  |  | static wdt_hal_context_t rtc_wdt_ctx = {.inst = WDT_RWDT, .rwdt_dev = &RTCCNTL}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 23:23:16 +08:00
										 |  |  | #if !CONFIG_ESP_SYSTEM_PANIC_SILENT_REBOOT
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-30 16:48:49 +02:00
										 |  |  | #if CONFIG_ESP_CONSOLE_UART
 | 
					
						
							| 
									
										
										
										
											2021-04-19 12:03:43 +03:00
										 |  |  | static uart_hal_context_t s_panic_uart = { .dev = CONFIG_ESP_CONSOLE_UART_NUM == 0 ? &UART0 :&UART1 }; | 
					
						
							| 
									
										
										
										
											2020-02-02 23:23:16 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | void panic_print_char(const char c) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     uint32_t sz = 0; | 
					
						
							| 
									
										
										
										
											2021-04-19 12:03:43 +03:00
										 |  |  |     while (!uart_hal_get_txfifo_len(&s_panic_uart)); | 
					
						
							|  |  |  |     uart_hal_write_txfifo(&s_panic_uart, (uint8_t *) &c, 1, &sz); | 
					
						
							| 
									
										
										
										
											2020-02-02 23:23:16 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2020-04-30 16:48:49 +02:00
										 |  |  | #endif // CONFIG_ESP_CONSOLE_UART
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #if CONFIG_ESP_CONSOLE_USB_CDC
 | 
					
						
							|  |  |  | void panic_print_char(const char c) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     esp_usb_console_write_buf(&c, 1); | 
					
						
							|  |  |  |     /* result ignored */ | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | #endif // CONFIG_ESP_CONSOLE_USB_CDC
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-19 12:03:43 +03:00
										 |  |  | #if CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG
 | 
					
						
							|  |  |  | //Timeout; if there's no host listening, the txfifo won't ever
 | 
					
						
							|  |  |  | //be writable after the first packet.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #define USBSERIAL_TIMEOUT_MAX_US 50000
 | 
					
						
							|  |  |  | static int s_usbserial_timeout = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void panic_print_char(const char c) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     while (!usb_serial_jtag_ll_txfifo_writable() && s_usbserial_timeout < (USBSERIAL_TIMEOUT_MAX_US / 100)) { | 
					
						
							|  |  |  |         esp_rom_delay_us(100); | 
					
						
							|  |  |  |         s_usbserial_timeout++; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (usb_serial_jtag_ll_txfifo_writable()) { | 
					
						
							|  |  |  |         usb_serial_jtag_ll_write_txfifo((const uint8_t *)&c, 1); | 
					
						
							|  |  |  |         s_usbserial_timeout = 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | #endif //CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-30 16:48:49 +02:00
										 |  |  | #if CONFIG_ESP_CONSOLE_NONE
 | 
					
						
							|  |  |  | void panic_print_char(const char c) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     /* no-op */ | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | #endif // CONFIG_ESP_CONSOLE_NONE
 | 
					
						
							| 
									
										
										
										
											2020-02-02 23:23:16 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | void panic_print_str(const char *str) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2021-04-19 12:03:43 +03:00
										 |  |  |     for (int i = 0; str[i] != 0; i++) { | 
					
						
							| 
									
										
										
										
											2020-02-02 23:23:16 +08:00
										 |  |  |         panic_print_char(str[i]); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void panic_print_hex(int h) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     int x; | 
					
						
							|  |  |  |     int c; | 
					
						
							|  |  |  |     // Does not print '0x', only the digits (8 digits to print)
 | 
					
						
							|  |  |  |     for (x = 0; x < 8; x++) { | 
					
						
							|  |  |  |         c = (h >> 28) & 0xf; // extract the leftmost byte
 | 
					
						
							|  |  |  |         if (c < 10) { | 
					
						
							|  |  |  |             panic_print_char('0' + c); | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             panic_print_char('a' + c - 10); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         h <<= 4; // move the 2nd leftmost byte to the left, to be extracted next
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void panic_print_dec(int d) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     // can print at most 2 digits!
 | 
					
						
							|  |  |  |     int n1, n2; | 
					
						
							|  |  |  |     n1 = d % 10; // extract ones digit
 | 
					
						
							|  |  |  |     n2 = d / 10; // extract tens digit
 | 
					
						
							|  |  |  |     if (n2 == 0) { | 
					
						
							|  |  |  |         panic_print_char(' '); | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         panic_print_char(n2 + '0'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     panic_print_char(n1 + '0'); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | #endif  // CONFIG_ESP_SYSTEM_PANIC_SILENT_REBOOT
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |   If watchdogs are enabled, the panic handler runs the risk of getting aborted pre-emptively because | 
					
						
							|  |  |  |   an overzealous watchdog decides to reset it. On the other hand, if we disable all watchdogs, we run | 
					
						
							|  |  |  |   the risk of somehow halting in the panic handler and not resetting. That is why this routine kills | 
					
						
							|  |  |  |   all watchdogs except the timer group 0 watchdog, and it reconfigures that to reset the chip after | 
					
						
							|  |  |  |   one second. | 
					
						
							| 
									
										
										
										
											2021-06-24 18:02:28 +10:00
										 |  |  | 
 | 
					
						
							|  |  |  |   We have to do this before we do anything that might cause issues in the WDT interrupt handlers, | 
					
						
							|  |  |  |   for example stalling the other core on ESP32 may cause the ESP32_ECO3_CACHE_LOCK_FIX | 
					
						
							|  |  |  |   handler to get stuck. | 
					
						
							| 
									
										
										
										
											2020-02-02 23:23:16 +08:00
										 |  |  | */ | 
					
						
							| 
									
										
										
										
											2021-06-24 18:02:28 +10:00
										 |  |  | void esp_panic_handler_reconfigure_wdts(void) | 
					
						
							| 
									
										
										
										
											2020-02-02 23:23:16 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-06-24 18:02:28 +10:00
										 |  |  |     wdt_hal_context_t wdt0_context = {.inst = WDT_MWDT0, .mwdt_dev = &TIMERG0}; | 
					
						
							| 
									
										
										
										
											2021-11-06 17:24:45 +08:00
										 |  |  | #if SOC_TIMER_GROUPS >= 2
 | 
					
						
							|  |  |  | 	// IDF-3825
 | 
					
						
							| 
									
										
										
										
											2021-06-24 18:02:28 +10:00
										 |  |  |     wdt_hal_context_t wdt1_context = {.inst = WDT_MWDT1, .mwdt_dev = &TIMERG1}; | 
					
						
							| 
									
										
										
										
											2021-11-06 17:24:45 +08:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2021-06-24 18:02:28 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-26 16:30:03 +08:00
										 |  |  |     //Todo: Refactor to use Interrupt or Task Watchdog API, and a system level WDT context
 | 
					
						
							|  |  |  |     //Reconfigure TWDT (Timer Group 0)
 | 
					
						
							|  |  |  |     wdt_hal_init(&wdt0_context, WDT_MWDT0, MWDT0_TICK_PRESCALER, false); //Prescaler: wdt counts in ticks of TG0_WDT_TICK_US
 | 
					
						
							|  |  |  |     wdt_hal_write_protect_disable(&wdt0_context); | 
					
						
							| 
									
										
										
										
											2021-04-19 12:03:43 +03:00
										 |  |  |     wdt_hal_config_stage(&wdt0_context, 0, 1000 * 1000 / MWDT0_TICKS_PER_US, WDT_STAGE_ACTION_RESET_SYSTEM); //1 second before reset
 | 
					
						
							| 
									
										
										
										
											2019-12-26 16:30:03 +08:00
										 |  |  |     wdt_hal_enable(&wdt0_context); | 
					
						
							|  |  |  |     wdt_hal_write_protect_enable(&wdt0_context); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-06 17:24:45 +08:00
										 |  |  | #if SOC_TIMER_GROUPS >= 2
 | 
					
						
							| 
									
										
										
										
											2019-12-26 16:30:03 +08:00
										 |  |  |     //Disable IWDT (Timer Group 1)
 | 
					
						
							|  |  |  |     wdt_hal_write_protect_disable(&wdt1_context); | 
					
						
							|  |  |  |     wdt_hal_disable(&wdt1_context); | 
					
						
							|  |  |  |     wdt_hal_write_protect_enable(&wdt1_context); | 
					
						
							| 
									
										
										
										
											2021-11-06 17:24:45 +08:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2020-02-02 23:23:16 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |   This disables all the watchdogs for when we call the gdbstub. | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | static inline void disable_all_wdts(void) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2021-06-24 18:02:28 +10:00
										 |  |  |     wdt_hal_context_t wdt0_context = {.inst = WDT_MWDT0, .mwdt_dev = &TIMERG0}; | 
					
						
							| 
									
										
										
										
											2021-11-06 17:24:45 +08:00
										 |  |  | #if SOC_TIMER_GROUPS >= 2
 | 
					
						
							| 
									
										
										
										
											2021-06-24 18:02:28 +10:00
										 |  |  |     wdt_hal_context_t wdt1_context = {.inst = WDT_MWDT1, .mwdt_dev = &TIMERG1}; | 
					
						
							| 
									
										
										
										
											2021-11-06 17:24:45 +08:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2021-06-24 18:02:28 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-26 16:30:03 +08:00
										 |  |  |     //Todo: Refactor to use Interrupt or Task Watchdog API, and a system level WDT context
 | 
					
						
							|  |  |  |     //Task WDT is the Main Watchdog Timer of Timer Group 0
 | 
					
						
							|  |  |  |     wdt_hal_write_protect_disable(&wdt0_context); | 
					
						
							|  |  |  |     wdt_hal_disable(&wdt0_context); | 
					
						
							|  |  |  |     wdt_hal_write_protect_enable(&wdt0_context); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-06 17:24:45 +08:00
										 |  |  | #if SOC_TIMER_GROUPS >= 2
 | 
					
						
							| 
									
										
										
										
											2019-12-26 16:30:03 +08:00
										 |  |  |     //Interupt WDT is the Main Watchdog Timer of Timer Group 1
 | 
					
						
							|  |  |  |     wdt_hal_write_protect_disable(&wdt1_context); | 
					
						
							|  |  |  |     wdt_hal_disable(&wdt1_context); | 
					
						
							|  |  |  |     wdt_hal_write_protect_enable(&wdt1_context); | 
					
						
							| 
									
										
										
										
											2021-11-06 17:24:45 +08:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2020-02-02 23:23:16 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void print_abort_details(const void *f) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2020-02-04 22:16:37 +08:00
										 |  |  |     panic_print_str(s_panic_abort_details); | 
					
						
							| 
									
										
										
										
											2020-02-02 23:23:16 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Control arrives from chip-specific panic handler, environment prepared for
 | 
					
						
							|  |  |  | // the 'main' logic of panic handling. This means that chip-specific stuff have
 | 
					
						
							|  |  |  | // already been done, and panic_info_t has been filled.
 | 
					
						
							|  |  |  | void esp_panic_handler(panic_info_t *info) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2021-06-24 18:02:28 +10:00
										 |  |  |     // The port-level panic handler has already called this, but call it again
 | 
					
						
							|  |  |  |     // to reset the TG0WDT period
 | 
					
						
							|  |  |  |     esp_panic_handler_reconfigure_wdts(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 23:23:16 +08:00
										 |  |  |     // If the exception was due to an abort, override some of the panic info
 | 
					
						
							| 
									
										
										
										
											2020-02-04 22:16:37 +08:00
										 |  |  |     if (g_panic_abort) { | 
					
						
							| 
									
										
										
										
											2020-02-02 23:23:16 +08:00
										 |  |  |         info->description = NULL; | 
					
						
							| 
									
										
										
										
											2020-02-04 22:16:37 +08:00
										 |  |  |         info->details = s_panic_abort_details ? print_abort_details : NULL; | 
					
						
							| 
									
										
										
										
											2020-03-04 10:25:49 +08:00
										 |  |  |         info->reason = NULL; | 
					
						
							| 
									
										
										
										
											2020-02-02 23:23:16 +08:00
										 |  |  |         info->exception = PANIC_EXCEPTION_ABORT; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-19 12:03:43 +03:00
										 |  |  |     /*
 | 
					
						
							|  |  |  |       * For any supported chip, the panic handler prints the contents of panic_info_t in the following format: | 
					
						
							|  |  |  |       * | 
					
						
							|  |  |  |       * | 
					
						
							|  |  |  |       * Guru Meditation Error: Core <core> (<exception>). <description> | 
					
						
							|  |  |  |       * <details> | 
					
						
							|  |  |  |       * | 
					
						
							|  |  |  |       * <state> | 
					
						
							|  |  |  |       * | 
					
						
							|  |  |  |       * <elf_info> | 
					
						
							|  |  |  |       * | 
					
						
							|  |  |  |       * | 
					
						
							|  |  |  |       * ---------------------------------------------------------------------------------------- | 
					
						
							|  |  |  |       * core - core where exception was triggered | 
					
						
							|  |  |  |       * exception - what kind of exception occured | 
					
						
							|  |  |  |       * description - a short description regarding the exception that occured | 
					
						
							|  |  |  |       * details - more details about the exception | 
					
						
							|  |  |  |       * state - processor state like register contents, and backtrace | 
					
						
							|  |  |  |       * elf_info - details about the image currently running | 
					
						
							|  |  |  |       * | 
					
						
							|  |  |  |       * NULL fields in panic_info_t are not printed. | 
					
						
							|  |  |  |       * | 
					
						
							|  |  |  |       * */ | 
					
						
							| 
									
										
										
										
											2020-03-04 10:25:49 +08:00
										 |  |  |     if (info->reason) { | 
					
						
							|  |  |  |         panic_print_str("Guru Meditation Error: Core "); | 
					
						
							|  |  |  |         panic_print_dec(info->core); | 
					
						
							|  |  |  |         panic_print_str(" panic'ed ("); | 
					
						
							|  |  |  |         panic_print_str(info->reason); | 
					
						
							|  |  |  |         panic_print_str("). "); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-02-02 23:23:16 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (info->description) { | 
					
						
							|  |  |  |         panic_print_str(info->description); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     panic_print_str("\r\n"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     PANIC_INFO_DUMP(info, details); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     panic_print_str("\r\n"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // If on-chip-debugger is attached, and system is configured to be aware of this,
 | 
					
						
							|  |  |  |     // then only print up to details. Users should be able to probe for the other information
 | 
					
						
							|  |  |  |     // in debug mode.
 | 
					
						
							|  |  |  |     if (esp_cpu_in_ocd_debug_mode()) { | 
					
						
							|  |  |  |         panic_print_str("Setting breakpoint at 0x"); | 
					
						
							|  |  |  |         panic_print_hex((uint32_t)info->addr); | 
					
						
							|  |  |  |         panic_print_str(" and returning...\r\n"); | 
					
						
							|  |  |  |         disable_all_wdts(); | 
					
						
							|  |  |  | #if CONFIG_APPTRACE_ENABLE
 | 
					
						
							| 
									
										
										
										
											2020-12-21 20:17:42 +03:00
										 |  |  | #if CONFIG_APPTRACE_SV_ENABLE
 | 
					
						
							|  |  |  |         SEGGER_RTT_ESP_FlushNoLock(CONFIG_APPTRACE_POSTMORTEM_FLUSH_THRESH, APPTRACE_ONPANIC_HOST_FLUSH_TMO); | 
					
						
							| 
									
										
										
										
											2020-02-02 23:23:16 +08:00
										 |  |  | #else
 | 
					
						
							|  |  |  |         esp_apptrace_flush_nolock(ESP_APPTRACE_DEST_TRAX, CONFIG_APPTRACE_POSTMORTEM_FLUSH_THRESH, | 
					
						
							|  |  |  |                                   APPTRACE_ONPANIC_HOST_FLUSH_TMO); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         cpu_hal_set_breakpoint(0, info->addr); // use breakpoint 0
 | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // start panic WDT to restart system if we hang in this handler
 | 
					
						
							| 
									
										
										
										
											2019-12-26 16:30:03 +08:00
										 |  |  |     if (!wdt_hal_is_enabled(&rtc_wdt_ctx)) { | 
					
						
							|  |  |  |         wdt_hal_init(&rtc_wdt_ctx, WDT_RWDT, 0, false); | 
					
						
							|  |  |  |         uint32_t stage_timeout_ticks = (uint32_t)(7000ULL * rtc_clk_slow_freq_get_hz() / 1000ULL); | 
					
						
							|  |  |  |         wdt_hal_write_protect_disable(&rtc_wdt_ctx); | 
					
						
							|  |  |  |         wdt_hal_config_stage(&rtc_wdt_ctx, WDT_STAGE0, stage_timeout_ticks, WDT_STAGE_ACTION_RESET_SYSTEM); | 
					
						
							| 
									
										
										
										
											2020-02-02 23:23:16 +08:00
										 |  |  |         // 64KB of core dump data (stacks of about 30 tasks) will produce ~85KB base64 data.
 | 
					
						
							|  |  |  |         // @ 115200 UART speed it will take more than 6 sec to print them out.
 | 
					
						
							| 
									
										
										
										
											2019-12-26 16:30:03 +08:00
										 |  |  |         wdt_hal_enable(&rtc_wdt_ctx); | 
					
						
							|  |  |  |         wdt_hal_write_protect_enable(&rtc_wdt_ctx); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 23:23:16 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-24 18:02:28 +10:00
										 |  |  |     esp_panic_handler_reconfigure_wdts(); // Restart WDT again
 | 
					
						
							| 
									
										
										
										
											2020-02-02 23:23:16 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     PANIC_INFO_DUMP(info, state); | 
					
						
							|  |  |  |     panic_print_str("\r\n"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     panic_print_str("\r\nELF file SHA256: "); | 
					
						
							|  |  |  |     char sha256_buf[65]; | 
					
						
							|  |  |  |     esp_ota_get_app_elf_sha256(sha256_buf, sizeof(sha256_buf)); | 
					
						
							|  |  |  |     panic_print_str(sha256_buf); | 
					
						
							|  |  |  |     panic_print_str("\r\n"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     panic_print_str("\r\n"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #if CONFIG_APPTRACE_ENABLE
 | 
					
						
							|  |  |  |     disable_all_wdts(); | 
					
						
							| 
									
										
										
										
											2020-12-21 20:17:42 +03:00
										 |  |  | #if CONFIG_APPTRACE_SV_ENABLE
 | 
					
						
							|  |  |  |     SEGGER_RTT_ESP_FlushNoLock(CONFIG_APPTRACE_POSTMORTEM_FLUSH_THRESH, APPTRACE_ONPANIC_HOST_FLUSH_TMO); | 
					
						
							| 
									
										
										
										
											2020-02-02 23:23:16 +08:00
										 |  |  | #else
 | 
					
						
							|  |  |  |     esp_apptrace_flush_nolock(ESP_APPTRACE_DEST_TRAX, CONFIG_APPTRACE_POSTMORTEM_FLUSH_THRESH, | 
					
						
							|  |  |  |                               APPTRACE_ONPANIC_HOST_FLUSH_TMO); | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2021-06-24 18:02:28 +10:00
										 |  |  |     esp_panic_handler_reconfigure_wdts(); // restore WDT config
 | 
					
						
							|  |  |  | #endif // CONFIG_APPTRACE_ENABLE
 | 
					
						
							| 
									
										
										
										
											2020-02-02 23:23:16 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | #if CONFIG_ESP_SYSTEM_PANIC_GDBSTUB
 | 
					
						
							|  |  |  |     disable_all_wdts(); | 
					
						
							| 
									
										
										
										
											2019-12-26 16:30:03 +08:00
										 |  |  |     wdt_hal_write_protect_disable(&rtc_wdt_ctx); | 
					
						
							|  |  |  |     wdt_hal_disable(&rtc_wdt_ctx); | 
					
						
							|  |  |  |     wdt_hal_write_protect_enable(&rtc_wdt_ctx); | 
					
						
							| 
									
										
										
										
											2020-02-02 23:23:16 +08:00
										 |  |  |     panic_print_str("Entering gdb stub now.\r\n"); | 
					
						
							| 
									
										
										
										
											2021-04-19 12:03:43 +03:00
										 |  |  |     esp_gdbstub_panic_handler((void *)info->frame); | 
					
						
							| 
									
										
										
										
											2020-02-02 23:23:16 +08:00
										 |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2020-09-29 21:02:17 +05:30
										 |  |  | #if CONFIG_ESP_COREDUMP_ENABLE
 | 
					
						
							| 
									
										
										
										
											2020-02-02 23:23:16 +08:00
										 |  |  |     static bool s_dumping_core; | 
					
						
							|  |  |  |     if (s_dumping_core) { | 
					
						
							|  |  |  |         panic_print_str("Re-entered core dump! Exception happened during core dump!\r\n"); | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         disable_all_wdts(); | 
					
						
							|  |  |  |         s_dumping_core = true; | 
					
						
							| 
									
										
										
										
											2020-09-29 21:02:17 +05:30
										 |  |  | #if CONFIG_ESP_COREDUMP_ENABLE_TO_FLASH
 | 
					
						
							| 
									
										
										
										
											2020-06-08 19:32:35 +03:00
										 |  |  |         esp_core_dump_to_flash(info); | 
					
						
							| 
									
										
										
										
											2020-02-02 23:23:16 +08:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2020-09-29 21:02:17 +05:30
										 |  |  | #if CONFIG_ESP_COREDUMP_ENABLE_TO_UART && !CONFIG_ESP_SYSTEM_PANIC_SILENT_REBOOT
 | 
					
						
							| 
									
										
										
										
											2020-06-08 19:32:35 +03:00
										 |  |  |         esp_core_dump_to_uart(info); | 
					
						
							| 
									
										
										
										
											2020-02-02 23:23:16 +08:00
										 |  |  | #endif
 | 
					
						
							|  |  |  |         s_dumping_core = false; | 
					
						
							| 
									
										
										
										
											2021-06-24 18:02:28 +10:00
										 |  |  | 
 | 
					
						
							|  |  |  |         esp_panic_handler_reconfigure_wdts(); | 
					
						
							| 
									
										
										
										
											2020-02-02 23:23:16 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-09-29 21:02:17 +05:30
										 |  |  | #endif /* CONFIG_ESP_COREDUMP_ENABLE */
 | 
					
						
							| 
									
										
										
										
											2019-12-26 16:30:03 +08:00
										 |  |  |     wdt_hal_write_protect_disable(&rtc_wdt_ctx); | 
					
						
							|  |  |  |     wdt_hal_disable(&rtc_wdt_ctx); | 
					
						
							|  |  |  |     wdt_hal_write_protect_enable(&rtc_wdt_ctx); | 
					
						
							| 
									
										
										
										
											2020-02-02 23:23:16 +08:00
										 |  |  | #if CONFIG_ESP_SYSTEM_PANIC_PRINT_REBOOT || CONFIG_ESP_SYSTEM_PANIC_SILENT_REBOOT
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (esp_reset_reason_get_hint() == ESP_RST_UNKNOWN) { | 
					
						
							| 
									
										
										
										
											2021-04-19 12:03:43 +03:00
										 |  |  |         switch (info->exception) { | 
					
						
							| 
									
										
										
										
											2020-02-02 23:23:16 +08:00
										 |  |  |         case PANIC_EXCEPTION_IWDT: | 
					
						
							|  |  |  |             esp_reset_reason_set_hint(ESP_RST_INT_WDT); | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         case PANIC_EXCEPTION_TWDT: | 
					
						
							|  |  |  |             esp_reset_reason_set_hint(ESP_RST_TASK_WDT); | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         case PANIC_EXCEPTION_ABORT: | 
					
						
							|  |  |  |         case PANIC_EXCEPTION_FAULT: | 
					
						
							|  |  |  |         default: | 
					
						
							|  |  |  |             esp_reset_reason_set_hint(ESP_RST_PANIC); | 
					
						
							|  |  |  |             break; // do not touch the previously set reset reason hint
 | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-06-08 19:32:35 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 23:23:16 +08:00
										 |  |  |     panic_print_str("Rebooting...\r\n"); | 
					
						
							| 
									
										
										
										
											2020-02-04 22:16:37 +08:00
										 |  |  |     panic_restart(); | 
					
						
							| 
									
										
										
										
											2020-02-02 23:23:16 +08:00
										 |  |  | #else
 | 
					
						
							|  |  |  |     disable_all_wdts(); | 
					
						
							|  |  |  |     panic_print_str("CPU halted.\r\n"); | 
					
						
							|  |  |  |     while (1); | 
					
						
							|  |  |  | #endif /* CONFIG_ESP_SYSTEM_PANIC_PRINT_REBOOT || CONFIG_ESP_SYSTEM_PANIC_SILENT_REBOOT */
 | 
					
						
							|  |  |  | #endif /* CONFIG_ESP_SYSTEM_PANIC_GDBSTUB */
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-08 11:19:23 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-28 17:48:43 +05:30
										 |  |  | void IRAM_ATTR __attribute__((noreturn, no_sanitize_undefined)) panic_abort(const char *details) | 
					
						
							| 
									
										
										
										
											2020-02-02 23:23:16 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-02-04 22:16:37 +08:00
										 |  |  |     g_panic_abort = true; | 
					
						
							| 
									
										
										
										
											2021-04-19 12:03:43 +03:00
										 |  |  |     s_panic_abort_details = (char *) details; | 
					
						
							| 
									
										
										
										
											2020-02-02 23:23:16 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | #if CONFIG_APPTRACE_ENABLE
 | 
					
						
							| 
									
										
										
										
											2020-12-21 20:17:42 +03:00
										 |  |  | #if CONFIG_APPTRACE_SV_ENABLE
 | 
					
						
							|  |  |  |     SEGGER_RTT_ESP_FlushNoLock(CONFIG_APPTRACE_POSTMORTEM_FLUSH_THRESH, APPTRACE_ONPANIC_HOST_FLUSH_TMO); | 
					
						
							| 
									
										
										
										
											2020-02-02 23:23:16 +08:00
										 |  |  | #else
 | 
					
						
							|  |  |  |     esp_apptrace_flush_nolock(ESP_APPTRACE_DEST_TRAX, CONFIG_APPTRACE_POSTMORTEM_FLUSH_THRESH, | 
					
						
							|  |  |  |                               APPTRACE_ONPANIC_HOST_FLUSH_TMO); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-10 19:29:05 +02:00
										 |  |  |     *((volatile int *) 0) = 0; // NOLINT(clang-analyzer-core.NullDereference) should be an invalid operation on targets
 | 
					
						
							| 
									
										
										
										
											2021-04-19 12:03:43 +03:00
										 |  |  |     while (1); | 
					
						
							| 
									
										
										
										
											2020-05-04 06:20:47 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Weak versions of reset reason hint functions.
 | 
					
						
							|  |  |  |  * If these weren't provided, reset reason code would be linked into the app | 
					
						
							|  |  |  |  * even if the app never called esp_reset_reason(). | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | void IRAM_ATTR __attribute__((weak)) esp_reset_reason_set_hint(esp_reset_reason_t hint) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | esp_reset_reason_t IRAM_ATTR  __attribute__((weak)) esp_reset_reason_get_hint(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return ESP_RST_UNKNOWN; | 
					
						
							|  |  |  | } |