debug_helpers.c 2.9 KB

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