You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

coredump.h 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. * Copyright (c) 2025, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2025-08-16 Rbb666 first version
  9. */
  10. #ifndef __COREDUMP_H__
  11. #define __COREDUMP_H__
  12. #include "mcd_cfg.h"
  13. #include "mcd_arm_define.h"
  14. /**
  15. * @brief Function pointer type for coredump data output
  16. *
  17. * This function pointer type defines the interface for writing coredump data
  18. * to different output destinations (memory, serial, filesystem, etc.).
  19. *
  20. * @param data Pointer to the data buffer to be written
  21. * @param len Length of the data buffer in bytes
  22. *
  23. * @note The implementation should handle the data appropriately based on the
  24. * chosen output mode and ensure data integrity.
  25. */
  26. typedef void (*mcd_writeout_func_t)(uint8_t *, int);
  27. /**
  28. * @brief Thread information operations structure
  29. *
  30. * This structure contains function pointers for retrieving thread information
  31. * in a RTOS environment. It's designed to avoid dynamic memory allocation
  32. * during fault handling, as malloc operations may fail during hard faults.
  33. *
  34. * The structure provides a generic interface for different RTOS systems
  35. * to supply thread information for multi-threaded coredump generation.
  36. */
  37. struct thread_info_ops
  38. {
  39. /**
  40. * @brief Get total number of threads in the system
  41. * @param ops Pointer to this operations structure
  42. * @return Total thread count
  43. */
  44. int32_t (*get_threads_count)(struct thread_info_ops *);
  45. /**
  46. * @brief Get current executing thread index
  47. * @param ops Pointer to this operations structure
  48. * @return Current thread index
  49. */
  50. int32_t (*get_current_thread_idx)(struct thread_info_ops *);
  51. /**
  52. * @brief Get register set for a specific thread
  53. * @param ops Pointer to this operations structure
  54. * @param thread_idx Thread index to get registers for
  55. * @param core_regset Pointer to store ARM core registers
  56. * @param fp_regset Pointer to store FPU registers
  57. */
  58. void (*get_thread_regset)(struct thread_info_ops *, int32_t,
  59. core_regset_type *core_regset,
  60. fp_regset_type *fp_regset);
  61. /**
  62. * @brief Get number of memory areas to dump
  63. * @param ops Pointer to this operations structure
  64. * @return Number of memory areas
  65. */
  66. int32_t (*get_memarea_count)(struct thread_info_ops *);
  67. /**
  68. * @brief Get memory area information
  69. * @param ops Pointer to this operations structure
  70. * @param area_idx Memory area index
  71. * @param addr Pointer to store memory area start address
  72. * @param size Pointer to store memory area size
  73. * @return 0 on success, negative on error
  74. */
  75. int32_t (*get_memarea)(struct thread_info_ops *, int32_t,
  76. uint32_t *, uint32_t *);
  77. /** Private data for RTOS-specific implementations */
  78. void *priv;
  79. };
  80. /**
  81. * @brief Initialize the mCoreDump system
  82. *
  83. * This function initializes the mCoreDump system with automatic FPU detection.
  84. * It must be called before any coredump generation operations.
  85. *
  86. * FPU support is automatically detected based on compiler definitions:
  87. * - Enabled if __VFP_FP__ is defined and __SOFTFP__ is not defined
  88. * - Disabled if no hardware FPU support is detected
  89. *
  90. * @param func Function pointer to write the coredump data out.
  91. * This callback will be called repeatedly with chunks of coredump data.
  92. *
  93. * @note The writeout function should handle data appropriately based on the
  94. * chosen output mode (memory, serial, filesystem).
  95. *
  96. * @warning This function should be called from a safe context, preferably
  97. * during system initialization or before fault occurs.
  98. *
  99. * @see mcd_writeout_func_t for callback function requirements
  100. */
  101. void mcd_init(mcd_writeout_func_t func);
  102. /**
  103. * @brief Generate coredump for current call chain only
  104. *
  105. * This function generates a minimal coredump containing only the current
  106. * execution context (single thread). It's faster and uses less memory
  107. * compared to multi-threaded coredump generation.
  108. *
  109. * Use this function when:
  110. * - Quick fault analysis is needed
  111. * - Memory is limited
  112. * - Only current thread context is relevant
  113. *
  114. * @note This function captures the current thread's register state and
  115. * stack information at the point of call.
  116. *
  117. * @warning Must call mcd_init() before using this function.
  118. *
  119. * @see mcd_multi_dump() for full system coredump
  120. */
  121. // 不更新当前寄存器到全局变量(在异常中断中已填充)
  122. void mcd_hard_fault_exception_dump(void);
  123. // 只保存当前线程或非线程(主栈):更新当前寄存器到全局变量
  124. void mcd_mini_dump(void);
  125. // 保存RTOS所有线程:更新当前寄存器到全局变量
  126. void mcd_multi_dump(void);
  127. /**
  128. * @brief Generate coredump with all threads information
  129. *
  130. * This function generates a comprehensive coredump containing information
  131. * about all threads in the system, including their register states,
  132. * stack contents, and memory areas.
  133. *
  134. * Use this function when:
  135. * - Complete system state analysis is needed
  136. * - Multi-threading issues need to be debugged
  137. * - Full context switch information is required
  138. *
  139. * @note This function may take longer to execute and use more memory
  140. * compared to mcd_mini_dump().
  141. *
  142. * @warning Must call mcd_init() before using this function.
  143. * Should be used carefully in interrupt context due to execution time.
  144. *
  145. * @see mcd_mini_dump() for single thread coredump
  146. */
  147. //void mcd_multi_dump(void);
  148. /**
  149. * @brief Generate coredump using provided thread operations
  150. *
  151. * This is the core coredump generation function that uses the provided
  152. * thread_info_ops structure to gather system information and generate
  153. * the ELF format coredump.
  154. *
  155. * @param ops Pointer to thread information operations structure.
  156. * Contains function pointers for retrieving thread and memory information.
  157. *
  158. * @note This is a low-level function typically called by mcd_mini_dump()
  159. * and mcd_multi_dump() after setting up appropriate ops structure.
  160. *
  161. * @warning The ops structure must be properly initialized with valid
  162. * function pointers before calling this function.
  163. */
  164. void mcd_gen_coredump(struct thread_info_ops *ops);
  165. /**
  166. * @brief Setup thread operations for RTOS environment
  167. *
  168. * This function initializes the thread_info_ops structure with function
  169. * pointers appropriate for the current RTOS environment (RT-Thread).
  170. *
  171. * @param ops Pointer to thread operations structure to be initialized
  172. *
  173. * @note This function is typically called internally by mcd_multi_dump()
  174. * to set up multi-threaded coredump generation.
  175. */
  176. void mcd_rtos_thread_ops(struct thread_info_ops *ops);
  177. /**
  178. * @brief Setup thread operations for single thread dump
  179. *
  180. * This function initializes the thread_info_ops structure for minimal
  181. * coredump generation (current thread only).
  182. *
  183. * @param ops Pointer to thread operations structure to be initialized
  184. *
  185. * @note This function is typically called internally by mcd_mini_dump()
  186. * to set up single-threaded coredump generation.
  187. */
  188. void mcd_mini_dump_ops(struct thread_info_ops *ops);
  189. /**
  190. * @brief Generate coredump and save to specified output.
  191. *
  192. * @param[in] output_mode Output mode (MCD_OUTPUT_SERIAL, MCD_OUTPUT_MEMORY, or MCD_OUTPUT_FILESYSTEM).
  193. *
  194. * @return Whether save operation success.
  195. * @retval MCD_OK success.
  196. * @retval MCD_ERROR failed.
  197. */
  198. int mcd_faultdump(mcd_output_mode_t output_mode);
  199. /**
  200. * @brief Print coredump memory information at startup
  201. *
  202. * This function should be called during system initialization
  203. */
  204. void mcd_print_memoryinfo(void);
  205. /**
  206. * @brief Get address of current core register set
  207. *
  208. * This function returns a pointer to the current thread's core register
  209. * set storage area. The register values are captured during fault handling.
  210. *
  211. * @return Pointer to core register set structure
  212. *
  213. * @note The returned pointer points to static storage that gets updated
  214. * each time a fault occurs or registers are captured.
  215. */
  216. core_regset_type *get_cur_core_regset_address(void);
  217. /**
  218. * @brief Get address of current floating-point register set
  219. *
  220. * This function returns a pointer to the current thread's floating-point
  221. * register set storage area. The register values are captured during fault handling.
  222. *
  223. * @return Pointer to floating-point register set structure
  224. *
  225. * @note The returned pointer points to static storage that gets updated
  226. * each time a fault occurs or registers are captured.
  227. * Returns valid data only if FPU is available and enabled.
  228. */
  229. fp_regset_type *get_cur_fp_regset_address(void);
  230. #endif /* __MCOREDUMP_H__ */