debug_helpers.c 2.8 KB

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