debug_helpers.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <string.h>
  7. #include "sdkconfig.h"
  8. #include "esp_types.h"
  9. #include "esp_attr.h"
  10. #include "esp_err.h"
  11. #include "esp_debug_helpers.h"
  12. #include "soc/soc_memory_layout.h"
  13. #include "esp_cpu_utils.h"
  14. #include "esp_private/panic_internal.h"
  15. #include "xtensa/xtensa_context.h"
  16. #include "sdkconfig.h"
  17. #include "esp_rom_sys.h"
  18. bool IRAM_ATTR esp_backtrace_get_next_frame(esp_backtrace_frame_t *frame)
  19. {
  20. //Use frame(i-1)'s BS area located below frame(i)'s sp to get frame(i-1)'s sp and frame(i-2)'s pc
  21. void *base_save = (void *)frame->sp; //Base save area consists of 4 words under SP
  22. frame->pc = frame->next_pc;
  23. frame->next_pc = *((uint32_t *)(base_save - 16)); //If next_pc = 0, indicates frame(i-1) is the last frame on the stack
  24. frame->sp = *((uint32_t *)(base_save - 12));
  25. //Return true if both sp and pc of frame(i-1) are sane, false otherwise
  26. return (esp_stack_ptr_is_sane(frame->sp) && esp_ptr_executable((void*)esp_cpu_process_stack_pc(frame->pc)));
  27. }
  28. static void IRAM_ATTR print_entry(uint32_t pc, uint32_t sp, bool panic)
  29. {
  30. if (panic) {
  31. panic_print_str(" 0x");
  32. panic_print_hex(pc);
  33. panic_print_str(":0x");
  34. panic_print_hex(sp);
  35. } else {
  36. esp_rom_printf(" 0x%08X:0x%08X", pc, sp);
  37. }
  38. }
  39. static void IRAM_ATTR print_str(const char* str, bool panic)
  40. {
  41. if (panic) {
  42. panic_print_str(str);
  43. } else {
  44. esp_rom_printf(str);
  45. }
  46. }
  47. esp_err_t IRAM_ATTR esp_backtrace_print_from_frame(int depth, const esp_backtrace_frame_t* frame, bool panic)
  48. {
  49. //Check arguments
  50. if (depth <= 0) {
  51. return ESP_ERR_INVALID_ARG;
  52. }
  53. //Initialize stk_frame with first frame of stack
  54. esp_backtrace_frame_t stk_frame = { 0 };
  55. memcpy(&stk_frame, frame, sizeof(esp_backtrace_frame_t));
  56. print_str("\r\n\r\nBacktrace:", panic);
  57. print_entry(esp_cpu_process_stack_pc(stk_frame.pc), stk_frame.sp, panic);
  58. //Check if first frame is valid
  59. bool corrupted = !(esp_stack_ptr_is_sane(stk_frame.sp) &&
  60. (esp_ptr_executable((void *)esp_cpu_process_stack_pc(stk_frame.pc)) ||
  61. /* Ignore the first corrupted PC in case of InstrFetchProhibited */
  62. (stk_frame.exc_frame && ((XtExcFrame *)stk_frame.exc_frame)->exccause == EXCCAUSE_INSTR_PROHIBITED)));
  63. uint32_t i = (depth <= 0) ? INT32_MAX : depth;
  64. while (i-- > 0 && stk_frame.next_pc != 0 && !corrupted) {
  65. if (!esp_backtrace_get_next_frame(&stk_frame)) { //Get previous stack frame
  66. corrupted = true;
  67. }
  68. print_entry(esp_cpu_process_stack_pc(stk_frame.pc), stk_frame.sp, panic);
  69. }
  70. //Print backtrace termination marker
  71. esp_err_t ret = ESP_OK;
  72. if (corrupted) {
  73. print_str(" |<-CORRUPTED", panic);
  74. ret = ESP_FAIL;
  75. } else if (stk_frame.next_pc != 0) { //Backtrace continues
  76. print_str(" |<-CONTINUES", panic);
  77. }
  78. print_str("\r\n\r\n", panic);
  79. return ret;
  80. }
  81. esp_err_t IRAM_ATTR esp_backtrace_print(int depth)
  82. {
  83. //Initialize stk_frame with first frame of stack
  84. esp_backtrace_frame_t start = { 0 };
  85. esp_backtrace_get_start(&(start.pc), &(start.sp), &(start.next_pc));
  86. return esp_backtrace_print_from_frame(depth, &start, false);
  87. }