debug_helpers.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // Copyright 2015-2019 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 "sdkconfig.h"
  14. #include "esp_types.h"
  15. #include "esp_attr.h"
  16. #include "esp_err.h"
  17. #include "esp_debug_helpers.h"
  18. #include "soc/soc_memory_layout.h"
  19. #include "soc/cpu.h"
  20. #if CONFIG_IDF_TARGET_ESP32
  21. #include "esp32/rom/ets_sys.h"
  22. #elif CONFIG_IDF_TARGET_ESP32S2
  23. #include "esp32s2/rom/ets_sys.h"
  24. #endif
  25. bool IRAM_ATTR esp_backtrace_get_next_frame(esp_backtrace_frame_t *frame)
  26. {
  27. //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
  28. void *base_save = (void *)frame->sp; //Base save area consists of 4 words under SP
  29. frame->pc = frame->next_pc;
  30. frame->next_pc = *((uint32_t *)(base_save - 16)); //If next_pc = 0, indicates frame(i-1) is the last frame on the stack
  31. frame->sp = *((uint32_t *)(base_save - 12));
  32. //Return true if both sp and pc of frame(i-1) are sane, false otherwise
  33. return (esp_stack_ptr_is_sane(frame->sp) && esp_ptr_executable((void*)esp_cpu_process_stack_pc(frame->pc)));
  34. }
  35. esp_err_t IRAM_ATTR esp_backtrace_print(int depth)
  36. {
  37. //Check arguments
  38. if (depth <= 0) {
  39. return ESP_ERR_INVALID_ARG;
  40. }
  41. //Initialize stk_frame with first frame of stack
  42. esp_backtrace_frame_t stk_frame;
  43. esp_backtrace_get_start(&(stk_frame.pc), &(stk_frame.sp), &(stk_frame.next_pc));
  44. //esp_cpu_get_backtrace_start(&stk_frame);
  45. ets_printf("\r\n\r\nBacktrace:");
  46. ets_printf("0x%08X:0x%08X ", esp_cpu_process_stack_pc(stk_frame.pc), stk_frame.sp);
  47. //Check if first frame is valid
  48. bool corrupted = (esp_stack_ptr_is_sane(stk_frame.sp) &&
  49. esp_ptr_executable((void*)esp_cpu_process_stack_pc(stk_frame.pc))) ?
  50. false : true;
  51. uint32_t i = (depth <= 0) ? INT32_MAX : depth;
  52. while (i-- > 0 && stk_frame.next_pc != 0 && !corrupted) {
  53. if (!esp_backtrace_get_next_frame(&stk_frame)) { //Get previous stack frame
  54. corrupted = true;
  55. }
  56. ets_printf("0x%08X:0x%08X ", esp_cpu_process_stack_pc(stk_frame.pc), stk_frame.sp);
  57. }
  58. //Print backtrace termination marker
  59. esp_err_t ret = ESP_OK;
  60. if (corrupted) {
  61. ets_printf(" |<-CORRUPTED");
  62. ret = ESP_FAIL;
  63. } else if (stk_frame.next_pc != 0) { //Backtrace continues
  64. ets_printf(" |<-CONTINUES");
  65. }
  66. ets_printf("\r\n\r\n");
  67. return ret;
  68. }