esp_debug_helpers.h 4.9 KB

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