panic.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include "esp_err.h"
  16. #include "esp_attr.h"
  17. #include "esp_private/system_internal.h"
  18. #include "esp_private/usb_console.h"
  19. #include "esp_ota_ops.h"
  20. #include "esp_core_dump.h"
  21. #include "soc/cpu.h"
  22. #include "soc/rtc.h"
  23. #include "hal/timer_hal.h"
  24. #include "hal/cpu_hal.h"
  25. #include "hal/wdt_types.h"
  26. #include "hal/wdt_hal.h"
  27. #include "esp_private/panic_internal.h"
  28. #include "port/panic_funcs.h"
  29. #include "sdkconfig.h"
  30. #if CONFIG_ESP32_ENABLE_COREDUMP
  31. #include "esp_core_dump.h"
  32. #endif
  33. #if CONFIG_APPTRACE_ENABLE
  34. #include "esp_app_trace.h"
  35. #if CONFIG_SYSVIEW_ENABLE
  36. #include "SEGGER_RTT.h"
  37. #endif
  38. #if CONFIG_APPTRACE_ONPANIC_HOST_FLUSH_TMO == -1
  39. #define APPTRACE_ONPANIC_HOST_FLUSH_TMO ESP_APPTRACE_TMO_INFINITE
  40. #else
  41. #define APPTRACE_ONPANIC_HOST_FLUSH_TMO (1000*CONFIG_APPTRACE_ONPANIC_HOST_FLUSH_TMO)
  42. #endif
  43. #endif // CONFIG_APPTRACE_ENABLE
  44. #if !CONFIG_ESP_SYSTEM_PANIC_SILENT_REBOOT
  45. #include "hal/uart_hal.h"
  46. #endif
  47. #if CONFIG_ESP_SYSTEM_PANIC_GDBSTUB
  48. #include "esp_gdbstub.h"
  49. #endif
  50. bool g_panic_abort = false;
  51. static char *s_panic_abort_details = NULL;
  52. static wdt_hal_context_t rtc_wdt_ctx = {.inst = WDT_RWDT, .rwdt_dev = &RTCCNTL};
  53. static wdt_hal_context_t wdt0_context = {.inst = WDT_MWDT0, .mwdt_dev = &TIMERG0};
  54. static wdt_hal_context_t wdt1_context = {.inst = WDT_MWDT1, .mwdt_dev = &TIMERG1};
  55. #if !CONFIG_ESP_SYSTEM_PANIC_SILENT_REBOOT
  56. #if CONFIG_ESP_CONSOLE_UART
  57. static uart_hal_context_t s_panic_uart = { .dev = CONFIG_ESP_CONSOLE_UART_NUM == 0 ? &UART0 : &UART1 };
  58. void panic_print_char(const char c)
  59. {
  60. uint32_t sz = 0;
  61. while(!uart_hal_get_txfifo_len(&s_panic_uart));
  62. uart_hal_write_txfifo(&s_panic_uart, (uint8_t*) &c, 1, &sz);
  63. }
  64. #endif // CONFIG_ESP_CONSOLE_UART
  65. #if CONFIG_ESP_CONSOLE_USB_CDC
  66. void panic_print_char(const char c)
  67. {
  68. esp_usb_console_write_buf(&c, 1);
  69. /* result ignored */
  70. }
  71. #endif // CONFIG_ESP_CONSOLE_USB_CDC
  72. #if CONFIG_ESP_CONSOLE_NONE
  73. void panic_print_char(const char c)
  74. {
  75. /* no-op */
  76. }
  77. #endif // CONFIG_ESP_CONSOLE_NONE
  78. void panic_print_str(const char *str)
  79. {
  80. for(int i = 0; str[i] != 0; i++) {
  81. panic_print_char(str[i]);
  82. }
  83. }
  84. void panic_print_hex(int h)
  85. {
  86. int x;
  87. int c;
  88. // Does not print '0x', only the digits (8 digits to print)
  89. for (x = 0; x < 8; x++) {
  90. c = (h >> 28) & 0xf; // extract the leftmost byte
  91. if (c < 10) {
  92. panic_print_char('0' + c);
  93. } else {
  94. panic_print_char('a' + c - 10);
  95. }
  96. h <<= 4; // move the 2nd leftmost byte to the left, to be extracted next
  97. }
  98. }
  99. void panic_print_dec(int d)
  100. {
  101. // can print at most 2 digits!
  102. int n1, n2;
  103. n1 = d % 10; // extract ones digit
  104. n2 = d / 10; // extract tens digit
  105. if (n2 == 0) {
  106. panic_print_char(' ');
  107. } else {
  108. panic_print_char(n2 + '0');
  109. }
  110. panic_print_char(n1 + '0');
  111. }
  112. #endif // CONFIG_ESP_SYSTEM_PANIC_SILENT_REBOOT
  113. /*
  114. If watchdogs are enabled, the panic handler runs the risk of getting aborted pre-emptively because
  115. an overzealous watchdog decides to reset it. On the other hand, if we disable all watchdogs, we run
  116. the risk of somehow halting in the panic handler and not resetting. That is why this routine kills
  117. all watchdogs except the timer group 0 watchdog, and it reconfigures that to reset the chip after
  118. one second.
  119. */
  120. static void reconfigure_all_wdts(void)
  121. {
  122. //Todo: Refactor to use Interrupt or Task Watchdog API, and a system level WDT context
  123. //Reconfigure TWDT (Timer Group 0)
  124. wdt_hal_init(&wdt0_context, WDT_MWDT0, MWDT0_TICK_PRESCALER, false); //Prescaler: wdt counts in ticks of TG0_WDT_TICK_US
  125. wdt_hal_write_protect_disable(&wdt0_context);
  126. wdt_hal_config_stage(&wdt0_context, 0, 1000*1000/MWDT0_TICKS_PER_US, WDT_STAGE_ACTION_RESET_SYSTEM); //1 second before reset
  127. wdt_hal_enable(&wdt0_context);
  128. wdt_hal_write_protect_enable(&wdt0_context);
  129. //Disable IWDT (Timer Group 1)
  130. wdt_hal_write_protect_disable(&wdt1_context);
  131. wdt_hal_disable(&wdt1_context);
  132. wdt_hal_write_protect_enable(&wdt1_context);
  133. }
  134. /*
  135. This disables all the watchdogs for when we call the gdbstub.
  136. */
  137. static inline void disable_all_wdts(void)
  138. {
  139. //Todo: Refactor to use Interrupt or Task Watchdog API, and a system level WDT context
  140. //Task WDT is the Main Watchdog Timer of Timer Group 0
  141. wdt_hal_write_protect_disable(&wdt0_context);
  142. wdt_hal_disable(&wdt0_context);
  143. wdt_hal_write_protect_enable(&wdt0_context);
  144. //Interupt WDT is the Main Watchdog Timer of Timer Group 1
  145. wdt_hal_write_protect_disable(&wdt1_context);
  146. wdt_hal_disable(&wdt1_context);
  147. wdt_hal_write_protect_enable(&wdt1_context);
  148. }
  149. static void print_abort_details(const void *f)
  150. {
  151. panic_print_str(s_panic_abort_details);
  152. }
  153. // Control arrives from chip-specific panic handler, environment prepared for
  154. // the 'main' logic of panic handling. This means that chip-specific stuff have
  155. // already been done, and panic_info_t has been filled.
  156. void esp_panic_handler(panic_info_t *info)
  157. {
  158. // If the exception was due to an abort, override some of the panic info
  159. if (g_panic_abort) {
  160. info->description = NULL;
  161. info->details = s_panic_abort_details ? print_abort_details : NULL;
  162. info->reason = NULL;
  163. info->exception = PANIC_EXCEPTION_ABORT;
  164. }
  165. /*
  166. * For any supported chip, the panic handler prints the contents of panic_info_t in the following format:
  167. *
  168. *
  169. * Guru Meditation Error: Core <core> (<exception>). <description>
  170. * <details>
  171. *
  172. * <state>
  173. *
  174. * <elf_info>
  175. *
  176. *
  177. * ----------------------------------------------------------------------------------------
  178. * core - core where exception was triggered
  179. * exception - what kind of exception occured
  180. * description - a short description regarding the exception that occured
  181. * details - more details about the exception
  182. * state - processor state like register contents, and backtrace
  183. * elf_info - details about the image currently running
  184. *
  185. * NULL fields in panic_info_t are not printed.
  186. *
  187. * */
  188. if (info->reason) {
  189. panic_print_str("Guru Meditation Error: Core ");
  190. panic_print_dec(info->core);
  191. panic_print_str(" panic'ed (");
  192. panic_print_str(info->reason);
  193. panic_print_str("). ");
  194. }
  195. if (info->description) {
  196. panic_print_str(info->description);
  197. }
  198. panic_print_str("\r\n");
  199. PANIC_INFO_DUMP(info, details);
  200. panic_print_str("\r\n");
  201. // If on-chip-debugger is attached, and system is configured to be aware of this,
  202. // then only print up to details. Users should be able to probe for the other information
  203. // in debug mode.
  204. if (esp_cpu_in_ocd_debug_mode()) {
  205. panic_print_str("Setting breakpoint at 0x");
  206. panic_print_hex((uint32_t)info->addr);
  207. panic_print_str(" and returning...\r\n");
  208. disable_all_wdts();
  209. #if CONFIG_APPTRACE_ENABLE
  210. #if CONFIG_SYSVIEW_ENABLE
  211. SEGGER_RTT_ESP32_FlushNoLock(CONFIG_APPTRACE_POSTMORTEM_FLUSH_THRESH, APPTRACE_ONPANIC_HOST_FLUSH_TMO);
  212. #else
  213. esp_apptrace_flush_nolock(ESP_APPTRACE_DEST_TRAX, CONFIG_APPTRACE_POSTMORTEM_FLUSH_THRESH,
  214. APPTRACE_ONPANIC_HOST_FLUSH_TMO);
  215. #endif
  216. #endif
  217. cpu_hal_set_breakpoint(0, info->addr); // use breakpoint 0
  218. return;
  219. }
  220. // start panic WDT to restart system if we hang in this handler
  221. if (!wdt_hal_is_enabled(&rtc_wdt_ctx)) {
  222. wdt_hal_init(&rtc_wdt_ctx, WDT_RWDT, 0, false);
  223. uint32_t stage_timeout_ticks = (uint32_t)(7000ULL * rtc_clk_slow_freq_get_hz() / 1000ULL);
  224. wdt_hal_write_protect_disable(&rtc_wdt_ctx);
  225. wdt_hal_config_stage(&rtc_wdt_ctx, WDT_STAGE0, stage_timeout_ticks, WDT_STAGE_ACTION_RESET_SYSTEM);
  226. // 64KB of core dump data (stacks of about 30 tasks) will produce ~85KB base64 data.
  227. // @ 115200 UART speed it will take more than 6 sec to print them out.
  228. wdt_hal_enable(&rtc_wdt_ctx);
  229. wdt_hal_write_protect_enable(&rtc_wdt_ctx);
  230. }
  231. //Feed the watchdogs, so they will give us time to print out debug info
  232. reconfigure_all_wdts();
  233. PANIC_INFO_DUMP(info, state);
  234. panic_print_str("\r\n");
  235. panic_print_str("\r\nELF file SHA256: ");
  236. char sha256_buf[65];
  237. esp_ota_get_app_elf_sha256(sha256_buf, sizeof(sha256_buf));
  238. panic_print_str(sha256_buf);
  239. panic_print_str("\r\n");
  240. panic_print_str("\r\n");
  241. #if CONFIG_APPTRACE_ENABLE
  242. disable_all_wdts();
  243. #if CONFIG_SYSVIEW_ENABLE
  244. SEGGER_RTT_ESP32_FlushNoLock(CONFIG_APPTRACE_POSTMORTEM_FLUSH_THRESH, APPTRACE_ONPANIC_HOST_FLUSH_TMO);
  245. #else
  246. esp_apptrace_flush_nolock(ESP_APPTRACE_DEST_TRAX, CONFIG_APPTRACE_POSTMORTEM_FLUSH_THRESH,
  247. APPTRACE_ONPANIC_HOST_FLUSH_TMO);
  248. #endif
  249. reconfigure_all_wdts();
  250. #endif
  251. #if CONFIG_ESP_SYSTEM_PANIC_GDBSTUB
  252. disable_all_wdts();
  253. wdt_hal_write_protect_disable(&rtc_wdt_ctx);
  254. wdt_hal_disable(&rtc_wdt_ctx);
  255. wdt_hal_write_protect_enable(&rtc_wdt_ctx);
  256. panic_print_str("Entering gdb stub now.\r\n");
  257. esp_gdbstub_panic_handler((esp_gdbstub_frame_t*)info->frame);
  258. #else
  259. #if CONFIG_ESP_COREDUMP_ENABLE
  260. static bool s_dumping_core;
  261. if (s_dumping_core) {
  262. panic_print_str("Re-entered core dump! Exception happened during core dump!\r\n");
  263. } else {
  264. disable_all_wdts();
  265. s_dumping_core = true;
  266. #if CONFIG_ESP_COREDUMP_ENABLE_TO_FLASH
  267. esp_core_dump_to_flash(info);
  268. #endif
  269. #if CONFIG_ESP_COREDUMP_ENABLE_TO_UART && !CONFIG_ESP_SYSTEM_PANIC_SILENT_REBOOT
  270. esp_core_dump_to_uart(info);
  271. #endif
  272. s_dumping_core = false;
  273. reconfigure_all_wdts();
  274. }
  275. #endif /* CONFIG_ESP_COREDUMP_ENABLE */
  276. wdt_hal_write_protect_disable(&rtc_wdt_ctx);
  277. wdt_hal_disable(&rtc_wdt_ctx);
  278. wdt_hal_write_protect_enable(&rtc_wdt_ctx);
  279. #if CONFIG_ESP_SYSTEM_PANIC_PRINT_REBOOT || CONFIG_ESP_SYSTEM_PANIC_SILENT_REBOOT
  280. if (esp_reset_reason_get_hint() == ESP_RST_UNKNOWN) {
  281. switch (info->exception)
  282. {
  283. case PANIC_EXCEPTION_IWDT:
  284. esp_reset_reason_set_hint(ESP_RST_INT_WDT);
  285. break;
  286. case PANIC_EXCEPTION_TWDT:
  287. esp_reset_reason_set_hint(ESP_RST_TASK_WDT);
  288. break;
  289. case PANIC_EXCEPTION_ABORT:
  290. case PANIC_EXCEPTION_FAULT:
  291. default:
  292. esp_reset_reason_set_hint(ESP_RST_PANIC);
  293. break; // do not touch the previously set reset reason hint
  294. }
  295. }
  296. panic_print_str("Rebooting...\r\n");
  297. panic_restart();
  298. #else
  299. disable_all_wdts();
  300. panic_print_str("CPU halted.\r\n");
  301. while (1);
  302. #endif /* CONFIG_ESP_SYSTEM_PANIC_PRINT_REBOOT || CONFIG_ESP_SYSTEM_PANIC_SILENT_REBOOT */
  303. #endif /* CONFIG_ESP_SYSTEM_PANIC_GDBSTUB */
  304. }
  305. void __attribute__((noreturn)) panic_abort(const char *details)
  306. {
  307. g_panic_abort = true;
  308. s_panic_abort_details = (char*) details;
  309. #if CONFIG_APPTRACE_ENABLE
  310. #if CONFIG_SYSVIEW_ENABLE
  311. SEGGER_RTT_ESP32_FlushNoLock(CONFIG_APPTRACE_POSTMORTEM_FLUSH_THRESH, APPTRACE_ONPANIC_HOST_FLUSH_TMO);
  312. #else
  313. esp_apptrace_flush_nolock(ESP_APPTRACE_DEST_TRAX, CONFIG_APPTRACE_POSTMORTEM_FLUSH_THRESH,
  314. APPTRACE_ONPANIC_HOST_FLUSH_TMO);
  315. #endif
  316. #endif
  317. *((int *) 0) = 0; // NOLINT(clang-analyzer-core.NullDereference) should be an invalid operation on targets
  318. while(1);
  319. }
  320. /* Weak versions of reset reason hint functions.
  321. * If these weren't provided, reset reason code would be linked into the app
  322. * even if the app never called esp_reset_reason().
  323. */
  324. void IRAM_ATTR __attribute__((weak)) esp_reset_reason_set_hint(esp_reset_reason_t hint)
  325. {
  326. }
  327. esp_reset_reason_t IRAM_ATTR __attribute__((weak)) esp_reset_reason_get_hint(void)
  328. {
  329. return ESP_RST_UNKNOWN;
  330. }