esp_debug_helpers.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #pragma once
  15. #ifdef __cplusplus
  16. extern "C" {
  17. #endif
  18. #ifndef __ASSEMBLER__
  19. #include <stdbool.h>
  20. #include "esp_err.h"
  21. #include "soc/soc.h" // [refactor-todo] IDF-2297
  22. #include "esp_cpu.h"
  23. /*
  24. * @brief Structure used for backtracing
  25. *
  26. * This structure stores the backtrace information of a particular stack frame
  27. * (i.e. the PC and SP). This structure is used iteratively with the
  28. * esp_cpu_get_next_backtrace_frame() function to traverse each frame within a
  29. * single stack. The next_pc represents the PC of the current frame's caller, thus
  30. * a next_pc of 0 indicates that the current frame is the last frame on the stack.
  31. *
  32. * @note Call esp_backtrace_get_start() to obtain initialization values for
  33. * this structure
  34. */
  35. typedef struct {
  36. uint32_t pc; /* PC of the current frame */
  37. uint32_t sp; /* SP of the current frame */
  38. uint32_t next_pc; /* PC of the current frame's caller */
  39. const void *exc_frame; /* Pointer to the full frame data structure, if applicable */
  40. } esp_backtrace_frame_t;
  41. /**
  42. * @brief If an OCD is connected over JTAG. set breakpoint 0 to the given function
  43. * address. Do nothing otherwise.
  44. * @param fn Pointer to the target breakpoint position
  45. */
  46. void esp_set_breakpoint_if_jtag(void *fn);
  47. /**
  48. * Get the first frame of the current stack's backtrace
  49. *
  50. * Given the following function call flow (B -> A -> X -> esp_backtrace_get_start),
  51. * this function will do the following.
  52. * - Flush CPU registers and window frames onto the current stack
  53. * - Return PC and SP of function A (i.e. start of the stack's backtrace)
  54. * - Return PC of function B (i.e. next_pc)
  55. *
  56. * @note This function is implemented in assembly
  57. *
  58. * @param[out] pc PC of the first frame in the backtrace
  59. * @param[out] sp SP of the first frame in the backtrace
  60. * @param[out] next_pc PC of the first frame's caller
  61. */
  62. extern void esp_backtrace_get_start(uint32_t *pc, uint32_t *sp, uint32_t *next_pc);
  63. /**
  64. * Get the next frame on a stack for backtracing
  65. *
  66. * Given a stack frame(i), this function will obtain the next stack frame(i-1)
  67. * on the same call stack (i.e. the caller of frame(i)). This function is meant to be
  68. * called iteratively when doing a backtrace.
  69. *
  70. * Entry Conditions: Frame structure containing valid SP and next_pc
  71. * Exit Conditions:
  72. * - Frame structure updated with SP and PC of frame(i-1). next_pc now points to frame(i-2).
  73. * - If a next_pc of 0 is returned, it indicates that frame(i-1) is last frame on the stack
  74. *
  75. * @param[inout] frame Pointer to frame structure
  76. *
  77. * @return
  78. * - True if the SP and PC of the next frame(i-1) are sane
  79. * - False otherwise
  80. */
  81. bool esp_backtrace_get_next_frame(esp_backtrace_frame_t *frame);
  82. /**
  83. * @brief Print the backtrace from specified frame.
  84. *
  85. * @param depth The maximum number of stack frames to print (should be > 0)
  86. * @param frame Starting frame to print from
  87. * @param panic Indicator if backtrace print is during a system panic
  88. *
  89. * @note On the ESP32, users must call esp_backtrace_get_start() first to flush the stack.
  90. * @note If a esp_backtrace_frame_t* frame is obtained though a call to esp_backtrace_get_start()
  91. * from some example function func_a(), then frame is only valid within the frame/scope of func_a().
  92. * Users should not attempt to pass/use frame other frames within the same stack of different stacks.
  93. *
  94. * @return
  95. * - ESP_OK Backtrace successfully printed to completion or to depth limit
  96. * - ESP_FAIL Backtrace is corrupted
  97. */
  98. esp_err_t IRAM_ATTR esp_backtrace_print_from_frame(int depth, const esp_backtrace_frame_t* frame, bool panic);
  99. /**
  100. * @brief Print the backtrace of the current stack
  101. *
  102. * @param depth The maximum number of stack frames to print (should be > 0)
  103. *
  104. * @return
  105. * - ESP_OK Backtrace successfully printed to completion or to depth limit
  106. * - ESP_FAIL Backtrace is corrupted
  107. */
  108. esp_err_t esp_backtrace_print(int depth);
  109. /**
  110. * @brief Set a watchpoint to break/panic when a certain memory range is accessed.
  111. * Superseded by esp_cpu_set_watchpoint in esp_cpu.h.
  112. */
  113. static inline __attribute__((deprecated)) esp_err_t esp_set_watchpoint(int no, void *adr, int size, int flags)
  114. {
  115. return esp_cpu_set_watchpoint(no, adr, size, flags);
  116. }
  117. /**
  118. * @brief Set a watchpoint to break/panic when a certain memory range is accessed.
  119. * Superseded by esp_cpu_clear_watchpoint in esp_cpu.h.
  120. */
  121. static inline __attribute__((deprecated)) void esp_clear_watchpoint(int no)
  122. {
  123. esp_cpu_clear_watchpoint(no);
  124. }
  125. #endif
  126. #ifdef __cplusplus
  127. }
  128. #endif