panic.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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 "esp_err.h"
  15. #include "esp_attr.h"
  16. #include "esp_spi_flash.h"
  17. #include "esp_private/system_internal.h"
  18. #include "esp_private/gdbstub.h"
  19. #include "esp_ota_ops.h"
  20. #if CONFIG_APPTRACE_ENABLE
  21. #include "esp_app_trace.h"
  22. #if CONFIG_SYSVIEW_ENABLE
  23. #include "SEGGER_RTT.h"
  24. #endif
  25. #endif // CONFIG_APPTRACE_ENABLE
  26. #include "esp_core_dump.h"
  27. #include "soc/rtc_wdt.h"
  28. #include "soc/cpu.h"
  29. #include "hal/timer_hal.h"
  30. #include "hal/cpu_hal.h"
  31. #if !CONFIG_ESP_SYSTEM_PANIC_SILENT_REBOOT
  32. #include <string.h>
  33. #include "hal/uart_hal.h"
  34. #endif
  35. #include "panic_internal.h"
  36. #include "port/panic_funcs.h"
  37. #include "sdkconfig.h"
  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. bool g_panic_abort = false;
  44. static char *s_panic_abort_details = NULL;
  45. #if !CONFIG_ESP_SYSTEM_PANIC_SILENT_REBOOT
  46. static uart_hal_context_t s_panic_uart = { .dev = CONFIG_ESP_CONSOLE_UART_NUM == 0 ? &UART0 : &UART1 };
  47. void panic_print_char(const char c)
  48. {
  49. uint32_t sz = 0;
  50. while(!uart_hal_get_txfifo_len(&s_panic_uart));
  51. uart_hal_write_txfifo(&s_panic_uart, (uint8_t*) &c, 1, &sz);
  52. }
  53. void panic_print_str(const char *str)
  54. {
  55. for(int i = 0; str[i] != 0; i++) {
  56. panic_print_char(str[i]);
  57. }
  58. }
  59. void panic_print_hex(int h)
  60. {
  61. int x;
  62. int c;
  63. // Does not print '0x', only the digits (8 digits to print)
  64. for (x = 0; x < 8; x++) {
  65. c = (h >> 28) & 0xf; // extract the leftmost byte
  66. if (c < 10) {
  67. panic_print_char('0' + c);
  68. } else {
  69. panic_print_char('a' + c - 10);
  70. }
  71. h <<= 4; // move the 2nd leftmost byte to the left, to be extracted next
  72. }
  73. }
  74. void panic_print_dec(int d)
  75. {
  76. // can print at most 2 digits!
  77. int n1, n2;
  78. n1 = d % 10; // extract ones digit
  79. n2 = d / 10; // extract tens digit
  80. if (n2 == 0) {
  81. panic_print_char(' ');
  82. } else {
  83. panic_print_char(n2 + '0');
  84. }
  85. panic_print_char(n1 + '0');
  86. }
  87. #endif // CONFIG_ESP_SYSTEM_PANIC_SILENT_REBOOT
  88. /*
  89. If watchdogs are enabled, the panic handler runs the risk of getting aborted pre-emptively because
  90. an overzealous watchdog decides to reset it. On the other hand, if we disable all watchdogs, we run
  91. the risk of somehow halting in the panic handler and not resetting. That is why this routine kills
  92. all watchdogs except the timer group 0 watchdog, and it reconfigures that to reset the chip after
  93. one second.
  94. */
  95. static void reconfigure_all_wdts(void)
  96. {
  97. timer_ll_wdt_set_protect(&TIMERG0, false);
  98. timer_ll_wdt_feed(&TIMERG0);
  99. timer_ll_wdt_init(&TIMERG0);
  100. timer_ll_wdt_set_tick(&TIMERG0, TG0_WDT_TICK_US); //Prescaler: wdt counts in ticks of TG0_WDT_TICK_US
  101. //1st stage timeout: reset system
  102. timer_ll_wdt_set_timeout_behavior(&TIMERG0, 0, TIMER_WDT_RESET_SYSTEM);
  103. //1 second before reset
  104. timer_ll_wdt_set_timeout(&TIMERG0, 0, 1000*1000/TG0_WDT_TICK_US);
  105. timer_ll_wdt_set_enable(&TIMERG0, true);
  106. timer_ll_wdt_set_protect(&TIMERG0, true);
  107. //Disable wdt 1
  108. timer_ll_wdt_set_protect(&TIMERG1, false);
  109. timer_ll_wdt_set_enable(&TIMERG1, false);
  110. timer_ll_wdt_set_protect(&TIMERG1, true);
  111. }
  112. /*
  113. This disables all the watchdogs for when we call the gdbstub.
  114. */
  115. static inline void disable_all_wdts(void)
  116. {
  117. timer_ll_wdt_set_protect(&TIMERG0, false);
  118. timer_ll_wdt_set_enable(&TIMERG0, false);
  119. timer_ll_wdt_set_protect(&TIMERG0, true);
  120. timer_ll_wdt_set_protect(&TIMERG1, false);
  121. timer_ll_wdt_set_enable(&TIMERG1, false);
  122. timer_ll_wdt_set_protect(&TIMERG1, true);
  123. }
  124. static void print_abort_details(const void *f)
  125. {
  126. panic_print_str(s_panic_abort_details);
  127. }
  128. // Control arrives from chip-specific panic handler, environment prepared for
  129. // the 'main' logic of panic handling. This means that chip-specific stuff have
  130. // already been done, and panic_info_t has been filled.
  131. void esp_panic_handler(panic_info_t *info)
  132. {
  133. // If the exception was due to an abort, override some of the panic info
  134. if (g_panic_abort) {
  135. info->description = NULL;
  136. info->details = s_panic_abort_details ? print_abort_details : NULL;
  137. info->reason = NULL;
  138. info->exception = PANIC_EXCEPTION_ABORT;
  139. }
  140. /*
  141. * For any supported chip, the panic handler prints the contents of panic_info_t in the following format:
  142. *
  143. *
  144. * Guru Meditation Error: Core <core> (<exception>). <description>
  145. * <details>
  146. *
  147. * <state>
  148. *
  149. * <elf_info>
  150. *
  151. *
  152. * ----------------------------------------------------------------------------------------
  153. * core - core where exception was triggered
  154. * exception - what kind of exception occured
  155. * description - a short description regarding the exception that occured
  156. * details - more details about the exception
  157. * state - processor state like register contents, and backtrace
  158. * elf_info - details about the image currently running
  159. *
  160. * NULL fields in panic_info_t are not printed.
  161. *
  162. * */
  163. if (info->reason) {
  164. panic_print_str("Guru Meditation Error: Core ");
  165. panic_print_dec(info->core);
  166. panic_print_str(" panic'ed (");
  167. panic_print_str(info->reason);
  168. panic_print_str("). ");
  169. }
  170. if (info->description) {
  171. panic_print_str(info->description);
  172. }
  173. panic_print_str("\r\n");
  174. PANIC_INFO_DUMP(info, details);
  175. panic_print_str("\r\n");
  176. // If on-chip-debugger is attached, and system is configured to be aware of this,
  177. // then only print up to details. Users should be able to probe for the other information
  178. // in debug mode.
  179. if (esp_cpu_in_ocd_debug_mode()) {
  180. panic_print_str("Setting breakpoint at 0x");
  181. panic_print_hex((uint32_t)info->addr);
  182. panic_print_str(" and returning...\r\n");
  183. disable_all_wdts();
  184. #if CONFIG_APPTRACE_ENABLE
  185. #if CONFIG_SYSVIEW_ENABLE
  186. SEGGER_RTT_ESP32_FlushNoLock(CONFIG_APPTRACE_POSTMORTEM_FLUSH_THRESH, APPTRACE_ONPANIC_HOST_FLUSH_TMO);
  187. #else
  188. esp_apptrace_flush_nolock(ESP_APPTRACE_DEST_TRAX, CONFIG_APPTRACE_POSTMORTEM_FLUSH_THRESH,
  189. APPTRACE_ONPANIC_HOST_FLUSH_TMO);
  190. #endif
  191. #endif
  192. cpu_hal_set_breakpoint(0, info->addr); // use breakpoint 0
  193. return;
  194. }
  195. // start panic WDT to restart system if we hang in this handler
  196. if (!rtc_wdt_is_on()) {
  197. rtc_wdt_protect_off();
  198. rtc_wdt_disable();
  199. rtc_wdt_set_length_of_reset_signal(RTC_WDT_SYS_RESET_SIG, RTC_WDT_LENGTH_3_2us);
  200. rtc_wdt_set_length_of_reset_signal(RTC_WDT_CPU_RESET_SIG, RTC_WDT_LENGTH_3_2us);
  201. rtc_wdt_set_stage(RTC_WDT_STAGE0, RTC_WDT_STAGE_ACTION_RESET_SYSTEM);
  202. // 64KB of core dump data (stacks of about 30 tasks) will produce ~85KB base64 data.
  203. // @ 115200 UART speed it will take more than 6 sec to print them out.
  204. rtc_wdt_set_time(RTC_WDT_STAGE0, 7000);
  205. rtc_wdt_enable();
  206. rtc_wdt_protect_on();
  207. }
  208. //Feed the watchdogs, so they will give us time to print out debug info
  209. reconfigure_all_wdts();
  210. PANIC_INFO_DUMP(info, state);
  211. panic_print_str("\r\n");
  212. panic_print_str("\r\nELF file SHA256: ");
  213. char sha256_buf[65];
  214. esp_ota_get_app_elf_sha256(sha256_buf, sizeof(sha256_buf));
  215. panic_print_str(sha256_buf);
  216. panic_print_str("\r\n");
  217. panic_print_str("\r\n");
  218. #if CONFIG_APPTRACE_ENABLE
  219. disable_all_wdts();
  220. #if CONFIG_SYSVIEW_ENABLE
  221. SEGGER_RTT_ESP32_FlushNoLock(CONFIG_APPTRACE_POSTMORTEM_FLUSH_THRESH, APPTRACE_ONPANIC_HOST_FLUSH_TMO);
  222. #else
  223. esp_apptrace_flush_nolock(ESP_APPTRACE_DEST_TRAX, CONFIG_APPTRACE_POSTMORTEM_FLUSH_THRESH,
  224. APPTRACE_ONPANIC_HOST_FLUSH_TMO);
  225. #endif
  226. reconfigure_all_wdts();
  227. #endif
  228. #if CONFIG_ESP_SYSTEM_PANIC_GDBSTUB
  229. disable_all_wdts();
  230. rtc_wdt_disable();
  231. panic_print_str("Entering gdb stub now.\r\n");
  232. esp_gdbstub_panic_handler((XtExcFrame*) info->frame);
  233. #else
  234. #if CONFIG_ESP32_ENABLE_COREDUMP
  235. static bool s_dumping_core;
  236. if (s_dumping_core) {
  237. panic_print_str("Re-entered core dump! Exception happened during core dump!\r\n");
  238. } else {
  239. disable_all_wdts();
  240. s_dumping_core = true;
  241. #if CONFIG_ESP32_ENABLE_COREDUMP_TO_FLASH
  242. esp_core_dump_to_flash((XtExcFrame*) info->frame);
  243. #endif
  244. #if CONFIG_ESP32_ENABLE_COREDUMP_TO_UART && !CONFIG_ESP_SYSTEM_PANIC_SILENT_REBOOT
  245. esp_core_dump_to_uart((XtExcFrame*) info->frame);
  246. #endif
  247. s_dumping_core = false;
  248. reconfigure_all_wdts();
  249. }
  250. #endif /* CONFIG_ESP32_ENABLE_COREDUMP */
  251. rtc_wdt_disable();
  252. #if CONFIG_ESP_SYSTEM_PANIC_PRINT_REBOOT || CONFIG_ESP_SYSTEM_PANIC_SILENT_REBOOT
  253. if (esp_reset_reason_get_hint() == ESP_RST_UNKNOWN) {
  254. switch (info->exception)
  255. {
  256. case PANIC_EXCEPTION_IWDT:
  257. esp_reset_reason_set_hint(ESP_RST_INT_WDT);
  258. break;
  259. case PANIC_EXCEPTION_TWDT:
  260. esp_reset_reason_set_hint(ESP_RST_TASK_WDT);
  261. break;
  262. case PANIC_EXCEPTION_ABORT:
  263. case PANIC_EXCEPTION_FAULT:
  264. default:
  265. esp_reset_reason_set_hint(ESP_RST_PANIC);
  266. break; // do not touch the previously set reset reason hint
  267. }
  268. }
  269. panic_print_str("Rebooting...\r\n");
  270. panic_restart();
  271. #else
  272. disable_all_wdts();
  273. panic_print_str("CPU halted.\r\n");
  274. while (1);
  275. #endif /* CONFIG_ESP_SYSTEM_PANIC_PRINT_REBOOT || CONFIG_ESP_SYSTEM_PANIC_SILENT_REBOOT */
  276. #endif /* CONFIG_ESP_SYSTEM_PANIC_GDBSTUB */
  277. }
  278. void __attribute__((noreturn)) panic_abort(const char *details)
  279. {
  280. g_panic_abort = true;
  281. s_panic_abort_details = (char*) details;
  282. #if CONFIG_APPTRACE_ENABLE
  283. #if CONFIG_SYSVIEW_ENABLE
  284. SEGGER_RTT_ESP32_FlushNoLock(CONFIG_APPTRACE_POSTMORTEM_FLUSH_THRESH, APPTRACE_ONPANIC_HOST_FLUSH_TMO);
  285. #else
  286. esp_apptrace_flush_nolock(ESP_APPTRACE_DEST_TRAX, CONFIG_APPTRACE_POSTMORTEM_FLUSH_THRESH,
  287. APPTRACE_ONPANIC_HOST_FLUSH_TMO);
  288. #endif
  289. #endif
  290. *((int *) 0) = 0; // should be an invalid operation on targets
  291. while(1);
  292. }