esp_panic.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. {
  18. #endif
  19. #define PANIC_RSN_NONE 0
  20. #define PANIC_RSN_DEBUGEXCEPTION 1
  21. #define PANIC_RSN_DOUBLEEXCEPTION 2
  22. #define PANIC_RSN_KERNELEXCEPTION 3
  23. #define PANIC_RSN_COPROCEXCEPTION 4
  24. #define PANIC_RSN_INTWDT_CPU0 5
  25. #define PANIC_RSN_INTWDT_CPU1 6
  26. #define PANIC_RSN_CACHEERR 7
  27. #define PANIC_RSN_MAX 7
  28. #ifndef __ASSEMBLER__
  29. #include "esp_err.h"
  30. #include "soc/soc.h"
  31. #include "soc/soc_memory_layout.h"
  32. /*
  33. * @brief Structure used for backtracing
  34. *
  35. * This structure stores the backtrace information of a particular stack frame
  36. * (i.e. the PC and SP). This structure is used iteratively with the
  37. * esp_cpu_get_next_backtrace_frame() function to traverse each frame within a
  38. * single stack. The next_pc represents the PC of the current frame's caller, thus
  39. * a next_pc of 0 indicates that the current frame is the last frame on the stack.
  40. *
  41. * @note Call esp_backtrace_get_start() to obtain initialization values for
  42. * this structure
  43. */
  44. typedef struct {
  45. uint32_t pc; /* PC of the current frame */
  46. uint32_t sp; /* SP of the current frame */
  47. uint32_t next_pc; /* PC of the current frame's caller */
  48. } esp_backtrace_frame_t;
  49. /**
  50. * @brief If an OCD is connected over JTAG. set breakpoint 0 to the given function
  51. * address. Do nothing otherwise.
  52. * @param data Pointer to the target breakpoint position
  53. */
  54. void esp_set_breakpoint_if_jtag(void *fn);
  55. #define ESP_WATCHPOINT_LOAD 0x40000000
  56. #define ESP_WATCHPOINT_STORE 0x80000000
  57. #define ESP_WATCHPOINT_ACCESS 0xC0000000
  58. /**
  59. * @brief Set a watchpoint to break/panic when a certain memory range is accessed.
  60. *
  61. * @param no Watchpoint number. On the ESP32, this can be 0 or 1.
  62. * @param adr Base address to watch
  63. * @param size Size of the region, starting at the base address, to watch. Must
  64. * be one of 2^n, with n in [0..6].
  65. * @param flags One of ESP_WATCHPOINT_* flags
  66. *
  67. * @return ESP_ERR_INVALID_ARG on invalid arg, ESP_OK otherwise
  68. *
  69. * @warning The ESP32 watchpoint hardware watches a region of bytes by effectively
  70. * masking away the lower n bits for a region with size 2^n. If adr does
  71. * not have zero for these lower n bits, you may not be watching the
  72. * region you intended.
  73. */
  74. esp_err_t esp_set_watchpoint(int no, void *adr, int size, int flags);
  75. /**
  76. * @brief Clear a watchpoint
  77. *
  78. * @param no Watchpoint to clear
  79. *
  80. */
  81. void esp_clear_watchpoint(int no);
  82. /**
  83. * @brief Checks stack pointer in dram
  84. */
  85. inline static bool esp_stack_ptr_in_dram(uint32_t sp)
  86. {
  87. //Check if stack ptr is in between SOC_DRAM_LOW and SOC_DRAM_HIGH, and 16 byte aligned.
  88. return !(sp < SOC_DRAM_LOW + 0x10 || sp > SOC_DRAM_HIGH - 0x10 || ((sp & 0xF) != 0));
  89. }
  90. #if CONFIG_SPIRAM_ALLOW_STACK_EXTERNAL_MEMORY
  91. /**
  92. * @brief Checks stack pointer in external ram
  93. */
  94. inline static bool esp_stack_ptr_in_extram(uint32_t sp)
  95. {
  96. //Check if stack ptr is in between SOC_EXTRAM_DATA_LOW and SOC_EXTRAM_DATA_HIGH, and 16 byte aligned.
  97. return !(sp < SOC_EXTRAM_DATA_LOW + 0x10 || sp > SOC_EXTRAM_DATA_HIGH - 0x10 || ((sp & 0xF) != 0));
  98. }
  99. #endif
  100. /*
  101. * Get the first frame of the current stack's backtrace
  102. *
  103. * Given the following function call flow (B -> A -> X -> esp_backtrace_get_start),
  104. * this function will do the following.
  105. * - Flush CPU registers and window frames onto the current stack
  106. * - Return PC and SP of function A (i.e. start of the stack's backtrace)
  107. * - Return PC of function B (i.e. next_pc)
  108. *
  109. * @note This function is implemented in assembly
  110. *
  111. * @param[out] pc PC of the first frame in the backtrace
  112. * @param[out] sp SP of the first frame in the backtrace
  113. * @param[out] next_pc PC of the first frame's caller
  114. */
  115. extern void esp_backtrace_get_start(uint32_t *pc, uint32_t *sp, uint32_t *next_pc);
  116. /**
  117. * Get the next frame on a stack for backtracing
  118. *
  119. * Given a stack frame(i), this function will obtain the next stack frame(i-1)
  120. * on the same call stack (i.e. the caller of frame(i)). This function is meant to be
  121. * called iteratively when doing a backtrace.
  122. *
  123. * Entry Conditions: Frame structure containing valid SP and next_pc
  124. * Exit Conditions:
  125. * - Frame structure updated with SP and PC of frame(i-1). next_pc now points to frame(i-2).
  126. * - If a next_pc of 0 is returned, it indicates that frame(i-1) is last frame on the stack
  127. *
  128. * @param[inout] frame Pointer to frame structure
  129. *
  130. * @return
  131. * - True if the SP and PC of the next frame(i-1) are sane
  132. * - False otherwise
  133. */
  134. bool esp_backtrace_get_next_frame(esp_backtrace_frame_t *frame);
  135. /**
  136. * @brief Print the backtrace of the current stack
  137. *
  138. * @param depth The maximum number of stack frames to print (should be > 0)
  139. *
  140. * @return
  141. * - ESP_OK Backtrace successfully printed to completion or to depth limit
  142. * - ESP_FAIL Backtrace is corrupted
  143. */
  144. esp_err_t esp_backtrace_print(int depth);
  145. #endif
  146. #ifdef __cplusplus
  147. }
  148. #endif