debug_helpers.c 2.8 KB

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