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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. void mcd_mini_dump(void);
  122. /**
  123. * @brief Generate coredump with all threads information
  124. *
  125. * This function generates a comprehensive coredump containing information
  126. * about all threads in the system, including their register states,
  127. * stack contents, and memory areas.
  128. *
  129. * Use this function when:
  130. * - Complete system state analysis is needed
  131. * - Multi-threading issues need to be debugged
  132. * - Full context switch information is required
  133. *
  134. * @note This function may take longer to execute and use more memory
  135. * compared to mcd_mini_dump().
  136. *
  137. * @warning Must call mcd_init() before using this function.
  138. * Should be used carefully in interrupt context due to execution time.
  139. *
  140. * @see mcd_mini_dump() for single thread coredump
  141. */
  142. void mcd_multi_dump(void);
  143. /**
  144. * @brief Generate coredump using provided thread operations
  145. *
  146. * This is the core coredump generation function that uses the provided
  147. * thread_info_ops structure to gather system information and generate
  148. * the ELF format coredump.
  149. *
  150. * @param ops Pointer to thread information operations structure.
  151. * Contains function pointers for retrieving thread and memory information.
  152. *
  153. * @note This is a low-level function typically called by mcd_mini_dump()
  154. * and mcd_multi_dump() after setting up appropriate ops structure.
  155. *
  156. * @warning The ops structure must be properly initialized with valid
  157. * function pointers before calling this function.
  158. */
  159. void mcd_gen_coredump(struct thread_info_ops *ops);
  160. /**
  161. * @brief Setup thread operations for RTOS environment
  162. *
  163. * This function initializes the thread_info_ops structure with function
  164. * pointers appropriate for the current RTOS environment (RT-Thread).
  165. *
  166. * @param ops Pointer to thread operations structure to be initialized
  167. *
  168. * @note This function is typically called internally by mcd_multi_dump()
  169. * to set up multi-threaded coredump generation.
  170. */
  171. void mcd_rtos_thread_ops(struct thread_info_ops *ops);
  172. /**
  173. * @brief Setup thread operations for single thread dump
  174. *
  175. * This function initializes the thread_info_ops structure for minimal
  176. * coredump generation (current thread only).
  177. *
  178. * @param ops Pointer to thread operations structure to be initialized
  179. *
  180. * @note This function is typically called internally by mcd_mini_dump()
  181. * to set up single-threaded coredump generation.
  182. */
  183. void mcd_mini_dump_ops(struct thread_info_ops *ops);
  184. /**
  185. * @brief Generate coredump and save to specified output.
  186. *
  187. * @param[in] output_mode Output mode (MCD_OUTPUT_SERIAL, MCD_OUTPUT_MEMORY, or MCD_OUTPUT_FILESYSTEM).
  188. *
  189. * @return Whether save operation success.
  190. * @retval MCD_OK success.
  191. * @retval MCD_ERROR failed.
  192. */
  193. int mcd_faultdump(mcd_output_mode_t output_mode);
  194. /**
  195. * @brief Print coredump memory information at startup
  196. *
  197. * This function should be called during system initialization
  198. */
  199. void mcd_print_memoryinfo(void);
  200. /**
  201. * @brief Get address of current core register set
  202. *
  203. * This function returns a pointer to the current thread's core register
  204. * set storage area. The register values are captured during fault handling.
  205. *
  206. * @return Pointer to core register set structure
  207. *
  208. * @note The returned pointer points to static storage that gets updated
  209. * each time a fault occurs or registers are captured.
  210. */
  211. core_regset_type *get_cur_core_regset_address(void);
  212. /**
  213. * @brief Get address of current floating-point register set
  214. *
  215. * This function returns a pointer to the current thread's floating-point
  216. * register set storage area. The register values are captured during fault handling.
  217. *
  218. * @return Pointer to floating-point register set structure
  219. *
  220. * @note The returned pointer points to static storage that gets updated
  221. * each time a fault occurs or registers are captured.
  222. * Returns valid data only if FPU is available and enabled.
  223. */
  224. fp_regset_type *get_cur_fp_regset_address(void);
  225. #endif /* __MCOREDUMP_H__ */