Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

coredump.h 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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 "arm/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. // 打印字符串形式的coredump信息
  78. int (*print_info_string)(struct thread_info_ops *);
  79. /** Private data for RTOS-specific implementations */
  80. void *priv;
  81. };
  82. /**
  83. * @brief Initialize the mCoreDump system
  84. *
  85. * This function initializes the mCoreDump system with automatic FPU detection.
  86. * It must be called before any coredump generation operations.
  87. *
  88. * FPU support is automatically detected based on compiler definitions:
  89. * - Enabled if __VFP_FP__ is defined and __SOFTFP__ is not defined
  90. * - Disabled if no hardware FPU support is detected
  91. *
  92. * @param func Function pointer to write the coredump data out.
  93. * This callback will be called repeatedly with chunks of coredump data.
  94. *
  95. * @note The writeout function should handle data appropriately based on the
  96. * chosen output mode (memory, serial, filesystem).
  97. *
  98. * @warning This function should be called from a safe context, preferably
  99. * during system initialization or before fault occurs.
  100. *
  101. * @see mcd_writeout_func_t for callback function requirements
  102. */
  103. void mcd_init(mcd_writeout_func_t func);
  104. /**
  105. * @brief Generate coredump for current call chain only
  106. *
  107. * This function generates a minimal coredump containing only the current
  108. * execution context (single thread). It's faster and uses less memory
  109. * compared to multi-threaded coredump generation.
  110. *
  111. * Use this function when:
  112. * - Quick fault analysis is needed
  113. * - Memory is limited
  114. * - Only current thread context is relevant
  115. *
  116. * @note This function captures the current thread's register state and
  117. * stack information at the point of call.
  118. *
  119. * @warning Must call mcd_init() before using this function.
  120. *
  121. * @see mcd_multi_dump() for full system coredump
  122. */
  123. // 不更新当前寄存器到全局变量(在异常中断中已填充)
  124. void mcd_hard_fault_exception_dump(void);
  125. // 只保存当前线程或非线程(主栈):更新当前寄存器到全局变量
  126. void mcd_mini_dump(void);
  127. // 保存RTOS所有线程:更新当前寄存器到全局变量
  128. void mcd_multi_dump(void);
  129. /**
  130. * @brief Generate coredump with all threads information
  131. *
  132. * This function generates a comprehensive coredump containing information
  133. * about all threads in the system, including their register states,
  134. * stack contents, and memory areas.
  135. *
  136. * Use this function when:
  137. * - Complete system state analysis is needed
  138. * - Multi-threading issues need to be debugged
  139. * - Full context switch information is required
  140. *
  141. * @note This function may take longer to execute and use more memory
  142. * compared to mcd_mini_dump().
  143. *
  144. * @warning Must call mcd_init() before using this function.
  145. * Should be used carefully in interrupt context due to execution time.
  146. *
  147. * @see mcd_mini_dump() for single thread coredump
  148. */
  149. //void mcd_multi_dump(void);
  150. /**
  151. * @brief Generate coredump using provided thread operations
  152. *
  153. * This is the core coredump generation function that uses the provided
  154. * thread_info_ops structure to gather system information and generate
  155. * the ELF format coredump.
  156. *
  157. * @param ops Pointer to thread information operations structure.
  158. * Contains function pointers for retrieving thread and memory information.
  159. *
  160. * @note This is a low-level function typically called by mcd_mini_dump()
  161. * and mcd_multi_dump() after setting up appropriate ops structure.
  162. *
  163. * @warning The ops structure must be properly initialized with valid
  164. * function pointers before calling this function.
  165. */
  166. void mcd_gen_coredump(struct thread_info_ops *ops);
  167. /**
  168. * @brief Setup thread operations for RTOS environment
  169. *
  170. * This function initializes the thread_info_ops structure with function
  171. * pointers appropriate for the current RTOS environment (RT-Thread).
  172. *
  173. * @param ops Pointer to thread operations structure to be initialized
  174. *
  175. * @note This function is typically called internally by mcd_multi_dump()
  176. * to set up multi-threaded coredump generation.
  177. */
  178. void mcd_rtos_thread_ops(struct thread_info_ops *ops);
  179. /**
  180. * @brief Setup thread operations for single thread dump
  181. *
  182. * This function initializes the thread_info_ops structure for minimal
  183. * coredump generation (current thread only).
  184. *
  185. * @param ops Pointer to thread operations structure to be initialized
  186. *
  187. * @note This function is typically called internally by mcd_mini_dump()
  188. * to set up single-threaded coredump generation.
  189. */
  190. void mcd_mini_dump_ops(struct thread_info_ops *ops);
  191. /**
  192. * @brief Generate coredump and save to specified output.
  193. *
  194. * @param[in] output_mode Output mode (MCD_OUTPUT_SERIAL, MCD_OUTPUT_MEMORY, or MCD_OUTPUT_FILESYSTEM).
  195. *
  196. * @return Whether save operation success.
  197. * @retval MCD_OK success.
  198. * @retval MCD_ERROR failed.
  199. */
  200. int mcd_faultdump(mcd_output_mode_t output_mode);
  201. /**
  202. * @brief Print coredump memory information at startup
  203. *
  204. * This function should be called during system initialization
  205. */
  206. void mcd_print_memoryinfo(void);
  207. /**
  208. * @brief Get address of current core register set
  209. *
  210. * This function returns a pointer to the current thread's core register
  211. * set storage area. The register values are captured during fault handling.
  212. *
  213. * @return Pointer to core register set structure
  214. *
  215. * @note The returned pointer points to static storage that gets updated
  216. * each time a fault occurs or registers are captured.
  217. */
  218. core_regset_type *get_cur_core_regset_address(void);
  219. /**
  220. * @brief Get address of current floating-point register set
  221. *
  222. * This function returns a pointer to the current thread's floating-point
  223. * register set storage area. The register values are captured during fault handling.
  224. *
  225. * @return Pointer to floating-point register set structure
  226. *
  227. * @note The returned pointer points to static storage that gets updated
  228. * each time a fault occurs or registers are captured.
  229. * Returns valid data only if FPU is available and enabled.
  230. */
  231. fp_regset_type *get_cur_fp_regset_address(void);
  232. #endif /* __MCOREDUMP_H__ */